現場で役立つ実践Sass(4)MixinとExtendの使いどころ
現場で役立つ実践Sass
第4回目の
先日、
※今記事は
Mixinの使いどころ
Mixinの基本(引数は必須?)
最初に
//コンパイル前(SCSS)
@mixin clearfix {
&::after {
content: "";
display: block;
clear: both;
}
}
.card {
@include clearfix;
}
//コンパイル後(CSS)
.card::after {
content: "";
display: block;
clear: both;
}
Mixinは
引数の初期値
では、
//コンパイル前(SCSS)
@mixin heading($color:#333, $fz:18px) {
color: $color;
border-left: 2px solid $color;
font-size: $fz;
}
.news-heading {
@include heading;
}
.blog-heading {
@include heading(green);
}
.about-heading {
@include heading(red, 24px);
}
//コンパイル後(CSS)
.news-heading {
color: #333;
border-left: 2px solid #333;
font-size: 18px;
}
.blog-heading {
color: green;
border-left: 2px solid green;
font-size: 18px;
}
.about-heading {
color: red;
border-left: 2px solid red;
font-size: 24px;
}
引数はコロン(:)で
初期値が
複数の初期値の単一変更
Mixinで、
//コンパイル前(SCSS)
.about-heading {
@include heading($fz:24px);
}
//コンパイル後(CSS)
.about-heading {
color: #333;
border-left: 2px solid #333;
font-size: 24px;
}<br>
この例では、$fzのみ
可長変引数
引数の
//コンパイル前(SCSS)
@mixin v-gradient($color) {
background-image: linear-gradient(to bottom, $color);
}
.box {
@include v-gradient(#ff0084, #33001b);
}
//コンパイル後(エラー)
//Mixin v-gradient takes 1 argument but 2 were passed.
上のような
そこで、カンマ3つ(...)を
//コンパイル前(SCSS)
@mixin v-gradient($color...) {
background-image: linear-gradient(to bottom, $color);
}
.box {
@include v-gradient(#ff0084, #33001b);
}
.box2 {
@include v-gradient(#ff0084, #33001b, #000);
}
//コンパイル後(CSS)
.box {
background-image: linear-gradient(to bottom, #ff0084, #33001b);
}
.box2 {
background-image: linear-gradient(to bottom, #ff0084, #33001b, #000);
}
これで
@content と 次期At-Rules
本連載第2@contentを@contentを
@supports
ブラウザの@supports は
$flex: "display:flex";
@mixin unsupports($property) {
$property: "(#{$property})"; //これがないと丸括弧がなくなってしまうので二重で囲う
@supports not (#{$property}) {
@content;
}
}
.card {
display: flex;
@include unsupports($flex) {
display: block;
}
}
display:flex を
上記の@supports を
//コンパイル前(SCSS)
@mixin not-flex {
@supports not (display:flex) {
@content;
}
}
.card2 {
display: flex;
@include not-flex {
display: block;
}
}
//コンパイル後(CSS)
.card {
display: flex;
}
@supports not (display:flex) {
.card {
display: block;
}
}
@document
CSS4に@document と
//コンパイル前(SCSS)
body {
color: #000;
@document url(https://blogs.adobe.com/) {
color: #444;
}
}
//コンパイル後(CSS)
body {
color: #000;
}
@document url(https://blogs.adobe.com/) {
body {
color: #444;
}
}
blogs.adobe.com の
@document も
$acs: "https://blogs.adobe.com/creativestation/";
@mixin url($url){
@document url(#{$url}) {
@content;
}
}
body {
color: #000;
@include url($acs){
color: #444;
}
}
ブラウザ
CSS @apply Rule
CSSでも@apply Rule と
https://tabatkins.github.io/specs/css-apply-rule/
Extendの使いどころ
Extendの使い方を見直そう
スタイルを
Extendで
// コンパイル前(SCSS)
%clearfix {
&::after {
content: "";
display: block;
clear: both;
}
}
よく見る
// コンパイル後(CSS)
.wrapper::after, .header::after, .nav::after, .box::after, .wrap::after, .card::after, .card .card__item::after, .dl__block::after, .dl__block dt::after,
.dl__block dd::after, .list::after, .list > li::after, .toolbar::after {
content: "";
display: block;
clear: both;
}
大量の
しかし、
下のコードをdisplay: table; で
.box {
@extend %clearfix
&::after {
display: table;
}
}
しかし、
.box::after {
display: table;
}
.box::after {
content: "";
display: block;
clear: both;
}
CSSはdisplay: table; は
それ以外にも、
ですので、
@mixin clearfix {
&::after {
content: "";
display: block;
clear: both;
}
}
加えて
@mixin clearfix($display: block) {
&::after {
content: "";
display: $display;
clear: both;
}
}
Extendを使うべきパターン
Extendを
%card {
width: 33%;
padding: 1em;
border: 2px solid #333;
border-radius: 5px;
}
.card {
@extend %card;
background-color: #fff;
}
.card--primary {
@extend %card;
border-color: #ccc;
background-color: #00f;
color: #fff;
}
.card--secondary {
@extend %card;
border-color: #000;
background-color: #000;
color: #fff;
}
.card--caution {
@extend %card;
border-color: #f33;
background-color: #ff3;
}
Extendが