Introduction
Mixins let you reuse styles easily. Using @include adds those styles where you want.
Mixins let you reuse styles easily. Using @include adds those styles where you want.
@include mixin-name; @include mixin-name(arguments);
Use @include inside a CSS selector to add mixin styles.
If the mixin needs values, pass them inside parentheses.
.button class using a mixin without arguments.@mixin rounded-corners { border-radius: 10px; } .button { @include rounded-corners; }
@mixin text-color($color) { color: $color; } .title { @include text-color(red); }
This example creates a box-shadow mixin with four parameters. The .card class uses @include to add a shadow with specific values.
@mixin box-shadow($x, $y, $blur, $color) { box-shadow: $x $y $blur $color; } .card { width: 200px; height: 100px; background-color: #eee; @include box-shadow(2px, 2px, 5px, rgba(0, 0, 0, 0.3)); }
Mixins help avoid repeating code and make updates easier.
You can include multiple mixins in one selector.
Passing arguments lets you customize mixins for different uses.
@include adds mixin styles inside selectors.
Mixins can have arguments to customize styles.
Using mixins keeps your CSS clean and reusable.
@include directive do in Sass?@include@include directive is used to add the styles from a mixin into a selector.@mixin, importing files uses @import, and variables use $ syntax.@include adds mixin styles [OK]button-style in Sass?@include mixin-name(); with parentheses even if no arguments are passed.@mixin defines a mixin, not includes it. @use is for modules. Omitting parentheses is invalid syntax.@mixin border-radius($radius) {
border-radius: $radius;
}
.box {
@include border-radius(10px);
background: blue;
}border-radius takes a parameter $radius and sets border-radius to that value.@include border-radius(10px); replaces $radius with 10px in the output CSS.@mixin text-style {
font-size: 1.2rem;
color: black;
}
.title {
@include text-style;
font-weight: bold;
}$. @include is valid inside selectors.card that sets a white background, shadow, and border radius. You also want to customize the border radius when including it. Which Sass code correctly defines and uses this mixin?card must accept $radius to customize border radius.@include card(1rem);.$radius without parameter, causing error. @mixin card($radius) {
background: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
border-radius: 1rem;
}
.container {
@include card;
} hardcodes radius and omits parentheses in include. @mixin card($radius) {
background: white;
box-shadow: 0 2px 5px rgba(0,0,0,0.1);
border-radius: $radius;
}
.container {
@include card;
} omits argument when including.Pass the test and this pattern moves unknown