Verwendung von Mixins in SASS
Nachdem wir ein Mixin deklariert haben,
rufen wir es mit der Direktive @include auf,
die unbedingt den Namen des Mixins
enthalten muss. Optional können dort auch
Parameter und Styles stehen.
Betrachten wir ein Beispiel:
@mixin mix{
width: 100px;
height: 100px;
}
div {
@include mix;
padding: 4px;
margin-top: 10px;
}
Ergebnis der Kompilation:
div {
width: 100px;
height: 100px;
padding: 4px;
margin-top: 10px;
}
Mixins können auch im Dokumentstamm aufgerufen werden, aber nur in dem Fall, wenn sie keine Regeln beschreiben und keinen Verweis auf das Elternelement enthalten. Sehen Sie sich das folgende Beispiel an:
@mixin active {
p {
color: #161618;
background-color: red;
}
}
@include active;
Nach der Kompilation sehen wir:
p {
color: #161618;
background-color: red;
}
Erklären Sie, wie das Ergebnis der Kompilation des folgenden Codes aussehen wird:
@mixin font {
font-family:'Courier New', Courier, monospace;
font-size: 12px;
}
#product-card {
@include font;
color: #090957;
font-weight: 600px;
}
Erklären Sie, wie das Ergebnis der Kompilation des folgenden Codes aussehen wird:
@mixin style {
color:#090957;
}
button, link {
@include style;
font-size: 10px;
padding: 4px;
}
Erklären Sie, wie das Ergebnis der Kompilation des folgenden Codes aussehen wird:
@mixin active-text {
font-size: 14px;
font-weight: bold;
text-decoration: underline;
color: blue;
}
#product-card, .active, .content {
@include active-text;
padding: 4px;
}
Erklären Sie, wie das Ergebnis der Kompilation des folgenden Codes aussehen wird:
@mixin size {
font-size: 14px;
padding: 10px;
margin: 4px;
}
@mixin color {
color: #580909;
background-color: #e9e5ab;
}
#product-card {
@include size;
@include color;
width: 600px;
}