SASSでのミックスインの使用
ミックスインを宣言した後、
ディレクティブ @include を使用して呼び出します。
このディレクティブには必ずミックスインの名前を含める必要があり、
オプションでパラメータやスタイルを含めることもできます。
例を見てみましょう:
@mixin mix{
width: 100px;
height: 100px;
}
div {
@include mix;
padding: 4px;
margin-top: 10px;
}
コンパイル結果:
div {
width: 100px;
height: 100px;
padding: 4px;
margin-top: 10px;
}
ミックスインはドキュメントのルートでも呼び出すことができますが、 ルールを記述しておらず、親への参照が含まれていない場合に限ります。 次の例を見てください:
@mixin active {
p {
color: #161618;
background-color: red;
}
}
@include active;
コンパイル後、次のようになります:
p {
color: #161618;
background-color: red;
}
次のコードのコンパイル結果がどのようになるか説明してください:
@mixin font {
font-family:'Courier New', Courier, monospace;
font-size: 12px;
}
#product-card {
@include font;
color: #090957;
font-weight: 600px;
}
次のコードのコンパイル結果がどのようになるか説明してください:
@mixin style {
color:#090957;
}
button, link {
@include style;
font-size: 10px;
padding: 4px;
}
次のコードのコンパイル結果がどのようになるか説明してください:
@mixin active-text {
font-size: 14px;
font-weight: bold;
text-decoration: underline;
color: blue;
}
#product-card, .active, .content {
@include active-text;
padding: 4px;
}
次のコードのコンパイル結果がどのようになるか説明してください:
@mixin size {
font-size: 14px;
padding: 10px;
margin: 4px;
}
@mixin color {
color: #580909;
background-color: #e9e5ab;
}
#product-card {
@include size;
@include color;
width: 600px;
}