今回は、僕がコーディングするときにたまに使う自作ミックスインを紹介します。
マウスオーバー時のリンクテキストカラーの変化とかはcompassですでに定義されているので、無理に自作するよりはそちらを参考にするのが得策かと思います!
なのでこのページに掲載されているのは「結構使い時に困るもの」ばかりかと思いますが…
ミックスインのつくりかたの参考程度に見ていただけると少しはお役に立てると思います^^
Webデザイナーにおすすめ?僕がよく使う自作mixin
それでは以下に綴っていきます。デモページヘのリンクも張っておいたのでぜひご覧ください!
ブラウザチェックは深くしていないので動作に関しては保証しません。
なお、ここにあるソースは改変商用利用に関して一切問わないのでご自由に!改変したよ!という場合はアドバイスをいただけると喜びます。
アンダーライン(下線)を引く
HTML
1 2 3 | <p> ここが<strong class=“underline”>スーパー重要</strong>です </p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | ////////labelBaloon ////吹き出し風ラベル // $padding 親要素パディング // $dir 矢印方向 // $arrow-color 矢印色 背景色に連動 // $arrow-width 矢印幅 // $position : ポジション指定 , // $posy : 矢印縦位置 , // $posx : 矢印横位置 , // $radius : 親要素角丸 , // $background-color : 親要素背景色 , // $color : テキストカラー @mixin labelBaloon( $dir : left , $arrowColor : ” , $arrowWidth : 5px , $padding : $arrowWidth , $position : absolute , $pos : $arrowWidth * 2 *–1, $radius : 2px , $background–color : $arrowColor , $color : #fff ){ @if($padding){ padding : $padding; } position: relative; color: $color; background–color: $background–color; border–radius: $radius; z–index: 3; @if($dir == left or $dir == top){ &:before{ width: 0; height: 0; content : “”; margin: auto; position: $position; border : solid $arrowWidth transparent; z–index: 1; } @if($dir == left){ &:before{ top : 0; bottom: 0; left : $pos; border–right : solid $arrowWidth $arrowColor; } }@else if($dir == top){ &:before{ left : 0; right: 0; top : $pos; border–bottom : solid $arrowWidth $arrowColor; } } }@else if($dir == right or $dir == bottom){ &:after{ width: 0; height: 0; content : “”; margin: auto; position: $position; border : solid $arrowWidth transparent; border–left : solid $arrowWidth $arrowColor; z–index: 1; } @if($dir == right){ &:after{ top : 0; bottom: 0; right : $pos; border–left : solid $arrowWidth $arrowColor; } }@else if($dir == bottom){ &:after{ left : 0; right: 0; bottom : $pos; border–top : solid $arrowWidth $arrowColor; } } } } .underline{ @include underline($background–color : red); } |
Css
1 2 3 4 5 6 7 8 | .underline { background–image: –webkit–gradient(linear, left top, left bottom, color–stop(0, rgba(255, 255, 255, 0)), color–stop(75%, rgba(255, 255, 255, 0)), color–stop(75% red), color–stop(100% red)); background–image: –webkit–linear–gradient(top, rgba(255, 255, 255, 0) 0%, rgba(255, 255, 255, 0) 75%, red 75%, red 100%); background–image: linear–gradient(transparent 75%, red 30%); } .ie .underline { text–decoration: undeline; } |
未対応ブラウザへの対策として、IEの古いバージョンである場合に.ieというクラスを付与されると仮定した記述があります。
文字を縁どりする
HTML
1 2 3 | <p class=“textOutline”> 縁取り文字 </p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | ////////テキスト縁取り // $outline-width シャドウ幅 // $outline-color シャドウ色 // $blur ぼかし // $color テキストカラー @mixin textOutline( $outline–width : 2px , $outline–color : #fff , $blur : 0 , $color : #323232 ){ @include text–shadow( 0 $outline–width $blur $outline–color , 0 –1*$outline–width $blur $outline–color , $outline–width $outline–width $blur $outline–color , –1*$outline–width $outline–width $blur $outline–color , –1*$outline–width –1*$outline–width $blur $outline–color , $outline–width 0px $blur $outline–color , –1*$outline–width 0px $blur $outline–color , 1px 1px 0 $outline–color , 1px –1px 0 $outline–color , –1px 1px 0 $outline–color , –1px –1px 0 $outline–color , 0px 3px 3px $outline–color ); } .textOutline{ font–size: 2em; font–weight: bold; @include textOutline( $outline–color : #fcf ); } |
Css
1 2 3 4 5 | .textOutline { font–size: 2em; font–weight: bold; text–shadow: 0 2px 0 #fcf, 0 -2px 0 #fcf, 2px 2px 0 #fcf, -2px 2px 0 #fcf, -2px -2px 0 #fcf, 2px 0px 0 #fcf, -2px 0px 0 #fcf, 1px 1px 0 #fcf, 1px -1px 0 #fcf, -1px 1px 0 #fcf, -1px -1px 0 #fcf, 0px 3px 3px #fcf; } |
グラデーション
HTML
1 2 3 | <p class=“singlegradation”> </p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | ///////singleGradation ////始点・終点のみのグラデーション // $type : linear / radial // $background-color 背景色・始点 // $end-color 終点 // $dir グラデーション方向($type が linearのときのみ有効) // $g 終点の位置 @mixin singleGradation( $type : linear , $background–color : #ededed, $end–color : #fff, $dir : bottom , $g : 100% ){ @if($type == radial){ $wlgStart : top; @if($dir == top){ $wlgStart : bottom;} @else if($dir == right){ $wlgStart : left;} @else if($dir == left){ $wlgStart : right;} background–color: $background–color; background–image: –webkit–gradient($type , center center , 0px , center center , 100% , color–stop(0% , $background–color) , color–stop($g , $end–color)); background–image: –webkit–radial–gradient(center , ellipse cover , $background–color 0%, $end–color $g); background–image: radial–gradient(ellipse at center , $background–color 0%, $end–color $g); }@else{ $wlgStart : top; @if($dir == top){ $wlgStart : bottom;} @else if($dir == right){ $wlgStart : left;} @else if($dir == left){ $wlgStart : right;} background–color: $background–color; background–image: –webkit–gradient($type , $dir , color–stop(0% , $background–color) , color–stop($g , $end–color)); background–image: –webkit–linear–gradient($wlgStart , $background–color 0%, $end–color $g); background–image: linear–gradient(to #{$dir} , $background-color 0%, $end-color $g); } } .singlegradation{ width: 200px; height: 200px; display: block; @include singleGradation( $background–color : #ffeeff , $end–color : #ff99ff ); } |
Css
1 2 3 4 5 6 7 8 9 | .singlegradation { width: 200px; height: 200px; display: block; background–color: #ffeeff; background–image: –webkit–gradient(linear, bottom, color–stop(0%, #ffeeff), color-stop(100%, #ff99ff)); background–image: –webkit–linear–gradient(top, #ffeeff 0%, #ff99ff 100%); background–image: linear–gradient(to bottom, #ffeeff 0%, #ff99ff 100%); } |
フラットデザインのWebサイトの見出しで使える、折り返し付きのリボン
HTML
1 | <p class=“ribbon”>INDEX</p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 | ////////orikaeshi ////リボンぽい影 // $pos beforeかafterか // $dir 折り返しの方向(基本的にはいじらないでいいと思う) // $width 折り返しの幅と高さ // $color 折り返し部分の色 // $posx 縦位置(bottom) // $posy 横位置 @mixin orikaeshi( $pos : before , $dir : bottom , // $dirx : right , $width : 10px , $color : darken($gray , 50%) , $posx : 0, $posy : 0 ){ position: relative; @if($pos == before){ &:before{ content : “”; position: absolute; } @if($dir == bottom){ &:before{ bottom: $posy; left: $posx; border: solid $width transparent; border–top : solid $width $color; border–right : solid $width $color; } }@else if($dir==top){ &:before{ top : $posy; left: $posx; border: solid $width transparent; border–bottom : solid $width $color; border–right : solid $width $color; } } } @else if($pos == after){ &:after{ content : “”; position: absolute; } @if($dir == bottom){ &:after{ bottom : $posy; right: $posx; border: solid $width transparent; border–top : solid $width $color; border–left : solid $width $color; } }@else if($dir==top){ &:after{ top : $posy; right: $posx; border: solid $width transparent; border–bottom : solid $width $color; border–left : solid $width $color; } } } } .ribbon{ padding: 1em; text–align: center; background–color: #fdf; //左の折り返し @include orikaeshi( $pos : before , $color : darken(#fdf , 50%) , $posy : –20px ); //右の折り返し @include orikaeshi( $pos : after , $color : darken(#fdf , 50%) , $posy : –20px ); } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | .ribbon { padding: 1em; text–align: center; background–color: #fdf; position: relative; position: relative; } .ribbon:before { content: “”; position: absolute; } .ribbon:before { bottom: –20px; left: 0; border: solid 10px transparent; border–top: solid 10px #dd00dd; border–right: solid 10px #dd00dd; } .ribbon:after { content: “”; position: absolute; } .ribbon:after { bottom: –20px; right: 0; border: solid 10px transparent; border–top: solid 10px #dd00dd; border–left: solid 10px #dd00dd; } |
彫り込まれたっぽい立体感のあるボーダー
HTML
1 2 3 4 5 | <ul class=“horikomiBorder”> <li>1</li> <li>2</li> <li>3</li> </ul> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | ////////umekomiBorder ////彫り込まれたようなボーダー // $border-color ボーダー色 // $dir ボーダーの位置 // $padding ボーダー側の余白 // $blur 明るい部分のぼかし // $last-chlid 末端の要素にボーダーを書くか @mixin umekomiBorder( $border–color : $lightgray*.55 , $dir : left , $padding: 1em , $blur : 0 , $last–child : true ){ padding–#{$dir} : $padding; border–#{$dir} : solid $border-color 1px ; @if($dir == left){ –webkit–box–shadow: –1px 0 $blur lighten($border–color , 50%); box–shadow: inset 1px 0 $blur lighten($border–color , 50%); }@else if($dir == right){ –webkit–box–shadow: 1px 0 $blur lighten($border–color , 50%); box–shadow: 1px 0 $blur lighten($border–color , 50%); }@else if($dir == bottom){ –webkit–box–shadow:0 1px $blur lighten($border–color , 50%); box–shadow:0 1px $blur lighten($border–color , 50%); }@else if($dir == top){ –webkit–box–shadow:0 1px $blur lighten($border–color , 50%); box–shadow:inset 0 1px $blur lighten($border–color , 50%); } @if($last–child == true){ @if($dir == right or $dir == bottom){ &:last–child{ border–color: none; border–width: 0px; –webkit–box–shadow: none; box–shadow: none; } }@else{ &:first–child{ border–color: none; border–width: 0px; –webkit–box–shadow: none; box–shadow: none; } } } } .demo–horikomi{ overflow: hidden; } .horikomiBorder { li{ width: 50px; height: 30px; float: left; list–style: none; padding: 1em 0px; text–align: center; background–color: #ddd; @include umekomiBorder( $dir : right ); } } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .demo–horikomi { overflow: hidden; } .horikomiBorder li { width: 50px; height: 30px; float: left; list–style: none; padding: 1em 0px; text–align: center; background–color: #ddd; padding–right: 1em; border–right: solid #848484 1px; –webkit–box–shadow: 1px 0 0 white; box–shadow: 1px 0 0 white; } .horikomiBorder li:last–child { border–color: none; border–width: 0px; –webkit–box–shadow: none; box–shadow: none; } |
sassだからこそできる!ロングシャドウ
HTML
1 2 3 4 5 | <div class=“demo_demo demo-longshadow”> <p> longshadow </p> </div> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | @function makelongshadow($color) { $val: 0px 0px $color; @for $i from 1 through 200 { $val: #{$val}, #{$i}px #{$i}px #{$color}; } @return $val; } @mixin longshadow($color) { text–shadow: makelongshadow($color); } $shadowColor: #ededed; .demo–longshadow { font–weight: bold; text–align: center; overflow: hidden; p { font–size: $large; @include longshadow(darken($shadowColor, 5% )); } } |
Css
1 | .demo–longshadow{font–weight:bold;text–align:center;overflow:hidden}.demo–longshadow p{font–size:1.414em;text–shadow:0px 0px #e0e0e0, 1px 1px #e0e0e0, 2px 2px #e0e0e0, 3px 3px #e0e0e0, 4px 4px #e0e0e0, 5px 5px #e0e0e0, 6px 6px #e0e0e0, 7px 7px #e0e0e0, 8px 8px #e0e0e0, 9px 9px #e0e0e0, 10px 10px #e0e0e0, 11px 11px #e0e0e0, 12px 12px #e0e0e0, 13px 13px #e0e0e0, 14px 14px #e0e0e0, 15px 15px #e0e0e0, 16px 16px #e0e0e0, 17px 17px #e0e0e0, 18px 18px #e0e0e0, 19px 19px #e0e0e0, 20px 20px #e0e0e0, 21px 21px #e0e0e0, 22px 22px #e0e0e0, 23px 23px #e0e0e0, 24px 24px #e0e0e0, 25px 25px #e0e0e0, 26px 26px #e0e0e0, 27px 27px #e0e0e0, 28px 28px #e0e0e0, 29px 29px #e0e0e0, 30px 30px #e0e0e0, 31px 31px #e0e0e0, 32px 32px #e0e0e0, 33px 33px #e0e0e0, 34px 34px #e0e0e0, 35px 35px #e0e0e0, 36px 36px #e0e0e0, 37px 37px #e0e0e0, 38px 38px #e0e0e0, 39px 39px #e0e0e0, 40px 40px #e0e0e0, 41px 41px #e0e0e0, 42px 42px #e0e0e0, 43px 43px #e0e0e0, 44px 44px #e0e0e0, 45px 45px #e0e0e0, 46px 46px #e0e0e0, 47px 47px #e0e0e0, 48px 48px #e0e0e0, 49px 49px #e0e0e0, 50px 50px #e0e0e0, 51px 51px #e0e0e0, 52px 52px #e0e0e0, 53px 53px #e0e0e0, 54px 54px #e0e0e0, 55px 55px #e0e0e0, 56px 56px #e0e0e0, 57px 57px #e0e0e0, 58px 58px #e0e0e0, 59px 59px #e0e0e0, 60px 60px #e0e0e0, 61px 61px #e0e0e0, 62px 62px #e0e0e0, 63px 63px #e0e0e0, 64px 64px #e0e0e0, 65px 65px #e0e0e0, 66px 66px #e0e0e0, 67px 67px #e0e0e0, 68px 68px #e0e0e0, 69px 69px #e0e0e0, 70px 70px #e0e0e0, 71px 71px #e0e0e0, 72px 72px #e0e0e0, 73px 73px #e0e0e0, 74px 74px #e0e0e0, 75px 75px #e0e0e0, 76px 76px #e0e0e0, 77px 77px #e0e0e0, 78px 78px #e0e0e0, 79px 79px #e0e0e0, 80px 80px #e0e0e0, 81px 81px #e0e0e0, 82px 82px #e0e0e0, 83px 83px #e0e0e0, 84px 84px #e0e0e0, 85px 85px #e0e0e0, 86px 86px #e0e0e0, 87px 87px #e0e0e0, 88px 88px #e0e0e0, 89px 89px #e0e0e0, 90px 90px #e0e0e0, 91px 91px #e0e0e0, 92px 92px #e0e0e0, 93px 93px #e0e0e0, 94px 94px #e0e0e0, 95px 95px #e0e0e0, 96px 96px #e0e0e0, 97px 97px #e0e0e0, 98px 98px #e0e0e0, 99px 99px #e0e0e0, 100px 100px #e0e0e0, 101px 101px #e0e0e0, 102px 102px #e0e0e0, 103px 103px #e0e0e0, 104px 104px #e0e0e0, 105px 105px #e0e0e0, 106px 106px #e0e0e0, 107px 107px #e0e0e0, 108px 108px #e0e0e0, 109px 109px #e0e0e0, 110px 110px #e0e0e0, 111px 111px #e0e0e0, 112px 112px #e0e0e0, 113px 113px #e0e0e0, 114px 114px #e0e0e0, 115px 115px #e0e0e0, 116px 116px #e0e0e0, 117px 117px #e0e0e0, 118px 118px #e0e0e0, 119px 119px #e0e0e0, 120px 120px #e0e0e0, 121px 121px #e0e0e0, 122px 122px #e0e0e0, 123px 123px #e0e0e0, 124px 124px #e0e0e0, 125px 125px #e0e0e0, 126px 126px #e0e0e0, 127px 127px #e0e0e0, 128px 128px #e0e0e0, 129px 129px #e0e0e0, 130px 130px #e0e0e0, 131px 131px #e0e0e0, 132px 132px #e0e0e0, 133px 133px #e0e0e0, 134px 134px #e0e0e0, 135px 135px #e0e0e0, 136px 136px #e0e0e0, 137px 137px #e0e0e0, 138px 138px #e0e0e0, 139px 139px #e0e0e0, 140px 140px #e0e0e0, 141px 141px #e0e0e0, 142px 142px #e0e0e0, 143px 143px #e0e0e0, 144px 144px #e0e0e0, 145px 145px #e0e0e0, 146px 146px #e0e0e0, 147px 147px #e0e0e0, 148px 148px #e0e0e0, 149px 149px #e0e0e0, 150px 150px #e0e0e0, 151px 151px #e0e0e0, 152px 152px #e0e0e0, 153px 153px #e0e0e0, 154px 154px #e0e0e0, 155px 155px #e0e0e0, 156px 156px #e0e0e0, 157px 157px #e0e0e0, 158px 158px #e0e0e0, 159px 159px #e0e0e0, 160px 160px #e0e0e0, 161px 161px #e0e0e0, 162px 162px #e0e0e0, 163px 163px #e0e0e0, 164px 164px #e0e0e0, 165px 165px #e0e0e0, 166px 166px #e0e0e0, 167px 167px #e0e0e0, 168px 168px #e0e0e0, 169px 169px #e0e0e0, 170px 170px #e0e0e0, 171px 171px #e0e0e0, 172px 172px #e0e0e0, 173px 173px #e0e0e0, 174px 174px #e0e0e0, 175px 175px #e0e0e0, 176px 176px #e0e0e0, 177px 177px #e0e0e0, 178px 178px #e0e0e0, 179px 179px #e0e0e0, 180px 180px #e0e0e0, 181px 181px #e0e0e0, 182px 182px #e0e0e0, 183px 183px #e0e0e0, 184px 184px #e0e0e0, 185px 185px #e0e0e0, 186px 186px #e0e0e0, 187px 187px #e0e0e0, 188px 188px #e0e0e0, 189px 189px #e0e0e0, 190px 190px #e0e0e0, 191px 191px #e0e0e0, 192px 192px #e0e0e0, 193px 193px #e0e0e0, 194px 194px #e0e0e0, 195px 195px #e0e0e0, 196px 196px #e0e0e0, 197px 197px #e0e0e0, 198px 198px #e0e0e0, 199px 199px #e0e0e0,200px 200px #e0e0e0} |
正直、ほとんど使わないです。
見出しとかメインヴィジュアルのキャッチコピーとかサイトロゴにWebフォントと併せて使える程度でしょうか?
画像のキャプションやコメントシステムに使える、「吹き出し」
HTML
1 2 3 | <p class=“labelBaloon”> Comment </p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | ////////labelBaloon ////吹き出し風ラベル // $padding 親要素パディング // $dir 矢印方向 // $arrow-color 矢印色 背景色に連動 // $arrow-width 矢印幅 // $position : ポジション指定 , // $posy : 矢印縦位置 , // $posx : 矢印横位置 , // $radius : 親要素角丸 , // $background-color : 親要素背景色 , // $color : テキストカラー @mixin labelBaloon( $dir : left , $arrowColor : ” , $arrowWidth : 5px , $padding : $arrowWidth , $position : absolute , $pos : $arrowWidth * 2 *–1, $radius : 2px , $background–color : $arrowColor , $color : #fff ){ @if($padding){ padding : $padding; } position: relative; color: $color; background–color: $background–color; border–radius: $radius; z–index: 3; @if($dir == left or $dir == top){ &:before{ width: 0; height: 0; content : “”; margin: auto; position: $position; border : solid $arrowWidth transparent; z–index: 1; } @if($dir == left){ &:before{ top : 0; bottom: 0; left : $pos; border–right : solid $arrowWidth $arrowColor; } }@else if($dir == top){ &:before{ left : 0; right: 0; top : $pos; border–bottom : solid $arrowWidth $arrowColor; } } }@else if($dir == right or $dir == bottom){ &:after{ width: 0; height: 0; content : “”; margin: auto; position: $position; border : solid $arrowWidth transparent; border–left : solid $arrowWidth $arrowColor; z–index: 1; } @if($dir == right){ &:after{ top : 0; bottom: 0; right : $pos; border–left : solid $arrowWidth $arrowColor; } }@else if($dir == bottom){ &:after{ left : 0; right: 0; bottom : $pos; border–top : solid $arrowWidth $arrowColor; } } } } .labelBaloon{ @include labelBaloon($arrowColor : #fdd); } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | .labelBaloon { padding: 5px; position: relative; color: #fff; background–color: #fdd; border–radius: 2px; z–index: 3; } .labelBaloon:before { width: 0; height: 0; content: “”; margin: auto; position: absolute; border: solid 5px transparent; z–index: 1; } .labelBaloon:before { top: 0; bottom: 0; left: –10px; border–right: solid 5px #fdd; } |
リスト表示でテキストの前にアイコン画像をつかう
HTML
1 2 | <p class=“iconImg”>アイコンがツキます。<br> $centeringをtrueにすると複数行にまたがっても縦位置中央に配置できて便利です。</p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | ///////iconImg ////画像をアイコンとして表示 // $pos 要素の前か後ろか // $content ファイル // $posy 縦ポジション // $posx 横ポジション // $position position : xx // $scale スマートフォン表示時の収縮 // $centering true/false 縦中央寄せ @mixin iconImg( $pos : before , $content : ”, $icon–margin–h : ”, $posy : 2px , $posx : 0 , $position : relative , $scale : .7 , $centering : false ){ @if($pos == after){ &:after{ content : url($content); margin–left : $icon–margin–h ; top : $posy; right: $posx; } @if($position == absolute){ position : relative; &:after{ position: absolute; } }@else{ &:after{ position: relative; } } @media screen and ($bpSmpMax){ &:after{ display: inline–block; @include scale($scale) } } }@else{ &:before{ content : url($content); margin–right : $icon–margin–h ; top : $posy; left : $posx; } @if($position == absolute){ position : relative; &:before{ position: absolute; } }@else{ &:before{ position: relative; } } /////mediaQuery sizing @media screen and ($bpSmpMax){ &:before{ display: inline–block; @include scale($scale) } } } @if ($centering == true and $position == absolute){ $iconWidth : image–width($content); $iconHeight : image–height($content); @if($pos == after){ padding–right: $iconWidth + 10px ; }@else{ padding–left: $iconWidth + 10px ; } &:#{$pos}{ height : image–height($content); margin–top: auto; margin–bottom: auto; top: 0; bottom: 0; } } } .iconImg{ @include iconImg( $content : ‘../img/icon.png’ , $position : absolute , $centering : true ); } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | .iconImg { position: relative; padding–left: 25px; } .iconImg:before { content: url(“../img/icon.png”); margin–right: “”; top: 2px; left: 0; } .iconImg:before { position: absolute; } @media screen and (max–width: 480px) { .iconImg:before { display: inline–block; –moz–transform: scale(0.7, 0.7); –ms–transform: scale(0.7, 0.7); –webkit–transform: scale(0.7, 0.7); transform: scale(0.7, 0.7); } } .iconImg:before { height: 15px; margin–top: auto; margin–bottom: auto; top: 0; bottom: 0; } |
画像へのパスは適宜変更してください!
mixinの引数$positionをabsoluteに、$centeringをtrueにすると、要素縦位置中央にアイコンを表示できます。
アイコンフォント(fontawesome)を要素の前に配置する
HTML
1 2 | <link rel=“stylesheet” href=“//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css”> <p class=“iconfont”>fontawesomeのアイコンフォント</p> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | ////////iconfont (font-awesome) ////fontawesomeを読み込んで使う、アイコン付与用 // $content fonwawesomeのコード // $margin-right 右マージン // $color 文字色 // $font-size 文字サイズ // $top 上からの位置 // $left 左からの位置 // $dir 文字の右につくか左につくか // $hover マウスオーバーした時のアニメーション @mixin iconfont( $content , $icon–margin–right: 3px, $color : $basefontcolor , $font–size : 1em , $top : 0px , $left : 0 , $dir : left , $hover: false ){ @if($dir == right){ &:after{ content: $content; margin–left: $icon–margin–right; display: inline–block; position: relative; top : $top; right: $left; –webkit–font–smoothing: antialiased; –moz–osx–font–smoothing: grayscale; transform: translate(0, 0); vertical–align: middle; font: normal normal normal $font–size/1 FontAwesome; font–size: $font–size; text–rendering: auto; color : $color; } html.ie8 &:after { content: “>”; font–family: inherit; } }@else{ &:before{ content: $content; margin–right: $icon–margin–right; display: inline–block; position: relative; top : $top; left: $left; –webkit–font–smoothing: antialiased; –moz–osx–font–smoothing: grayscale; transform: translate(0, 0); vertical–align: middle; font: normal normal normal $font–size/1 FontAwesome; font–size: inherit; text–rendering: auto; color : $color; } html.ie8 &:before{ content: “>”; font–family: inherit; } } @if($hover == true){ &:hover , &:active , &:focus , { &:before, &:after{ @include transform(rotate(–45deg)); –webkit–transition: transform 0.2s; –o–transition: transform 0.2s; transition: transform 0.2s; } } } } .iconfont{ @include iconfont( $content : ‘\f10b’ ); } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | .iconfont:before { content: “”; margin–right: 3px; display: inline–block; position: relative; top: 0px; left: 0; –webkit–font–smoothing: antialiased; –moz–osx–font–smoothing: grayscale; transform: translate(0, 0); vertical–align: middle; font: normal normal normal 1em FontAwesome; font–size: inherit; text–rendering: auto; color: #232323; } .ie .iconfont:before { content: “>”; font–family: inherit; } |
こちらも同様に、アイコンフォント未対応ブラウザに対し、コンディショナルコメントでbodyタグとかに.ieというクラスが付与された場合の表示を考慮しています。アイコンフォントの代わりに「>」を表示します。
IE9.jsでは、IE8でアイコンフォントをサポートしてくれないため、残念ですね…
box-shadowで、紙がめくれたような自然な影をつける
HTML
1 2 3 4 5 6 | <div class=“message”> <p>これはメッセージです。</p> <p>紙がひらっとなったようなシャドウの付き方がポイントです。</p> <p>これはメッセージです。</p> <p>紙がひらっとなったようなシャドウの付き方がポイントです。</p> </div> |
Sass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | ////////papermassage ////紙がぺろっとめくれたような感じ // $padding 余白 // $shadowColor box-shadowの色 // $shadow box-shadow // $shadowWidth 影の幅 // $top 影の縦位置(top) // $bottom 影の縦位置(bottom) // $right 影の横位置(right) // $right 影の横位置(left) // $rotate 影の傾き // $blur 影のぼかし @mixin paperMessage( $padding : 1em , $shadowColor: rgba(#000 , .2) , $shadow : 0 0 10px $shadowColor , $background–color : #fff , $shadowWidth : 83% , $top : 83% , $right : 15px , $left : auto , $bottom : 15px , $rotate : 1deg , $blur : 10px ){ padding: $padding; –webkit–box–shadow: $shadow; box–shadow: $shadow; position: relative; background–color: $background–color; ////rotate shadow &:after{ content : “”; width: $shadowWidth; max–width:300px; position: absolute; top: $top; right: $right; bottom: $bottom; left: $left; z–index: –1; background: #777; @include box–shadow(0 15px $blur $shadowColor); @include transform(rotate($rotate)); } } .message{ @include paperMessage(); } |
Css
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | .message { padding: 1em; –webkit–box–shadow: 0 0 10px rgba(0, 0, 0, 0.2); box–shadow: 0 0 10px rgba(0, 0, 0, 0.2); position: relative; background–color: #fff; } .message:after { content: “”; width: 83%; max–width: 300px; position: absolute; top: 83%; right: 15px; bottom: 15px; left: auto; z–index: –1; background: #777; –moz–box–shadow: 0 15px 10px rgba(0, 0, 0, 0.2); –webkit–box–shadow: 0 15px 10px rgba(0, 0, 0, 0.2); box–shadow: 0 15px 10px rgba(0, 0, 0, 0.2); –moz–transform: rotate(1deg); –ms–transform: rotate(1deg); –webkit–transform: rotate(1deg); transform: rotate(1deg); } |
これは正直な話、気に入っているだけで使いどころは少ないです。
擬似要素で矩形をつくり、それにbox-shadowを使ってrotateで回転させ、要素の下にもぐりこませて、ぼかしの部分をチラ見せすることで紙がペロッとめくれているような表現をします。
影が解りづらい場合はmixinの$shadowColorの値を調節しましょう!ほかの値は基本的にそんなに変えなくても良いと思います。
これは「[CSS]box-shadowを使って、紙がふわりと浮かんだようなエフェクトをつけるスタイルシートのまとめ | コリス」を参考にしています!
押した感覚がある、フラットデザインのボタン
HTML
1 2 3 4 5 6 7 8 9 10 | <div class=“button-2”> <button><a >button1</a></button> <button><a >button2</a></button> </div> <div class=“button-single”> <button><a >button3</a></button> </div> <div class=“button-single-40”> <button><a >button4</a></button> </div> |
Sass