What if you could fix a style once and see it change everywhere instantly?
Why Including mixins with @include in SASS? - Purpose & Use Cases
Imagine you want to style many buttons on your website with the same shadow, border, and padding. You write the same CSS rules over and over for each button.
If you need to change the shadow later, you must find and update every button style manually. This wastes time and can cause mistakes or inconsistent styles.
Mixins let you write a group of styles once and reuse them anywhere by including them with @include. Change the mixin once, and all buttons update automatically.
button1 {
box-shadow: 2px 2px 5px gray;
border-radius: 5px;
padding: 10px;
}
button2 {
box-shadow: 2px 2px 5px gray;
border-radius: 5px;
padding: 10px;
}@mixin button-style {
box-shadow: 2px 2px 5px gray;
border-radius: 5px;
padding: 10px;
}
button1 {
@include button-style;
}
button2 {
@include button-style;
}You can keep your styles neat, update them quickly, and avoid repeating code everywhere.
A website with many buttons, cards, or alerts that share the same look can use mixins to keep styles consistent and easy to maintain.
Writing styles once and reusing them saves time.
Mixins with @include help avoid mistakes from repeated code.
Updating a mixin updates all places it is included automatically.
Practice
@include directive do in Sass?Solution
Step 1: Understand the role of
The@include@includedirective is used to add the styles from a mixin into a selector.Step 2: Differentiate from other Sass features
Defining mixins uses@mixin, importing files uses@import, and variables use$syntax.Final Answer:
It inserts the styles defined in a mixin into the current selector. -> Option AQuick Check:
@includeadds mixin styles [OK]
- Confusing @include with @mixin which defines mixins
- Thinking @include imports files
- Mixing up variables with mixins
button-style in Sass?Solution
Step 1: Recall the syntax for including mixins
The correct syntax to include a mixin is@include mixin-name();with parentheses even if no arguments are passed.Step 2: Eliminate incorrect options
@mixindefines a mixin, not includes it.@useis for modules. Omitting parentheses is invalid syntax.Final Answer:
@include button-style(); -> Option AQuick Check:
Include mixin with @include name() [OK]
- Forgetting parentheses after mixin name
- Using @mixin instead of @include to call mixin
- Confusing @use with @include
@mixin border-radius($radius) {
border-radius: $radius;
}
.box {
@include border-radius(10px);
background: blue;
}What CSS will this produce?
Solution
Step 1: Understand mixin with argument
The mixinborder-radiustakes a parameter$radiusand setsborder-radiusto that value.Step 2: Apply mixin with 10px argument
Including@include border-radius(10px);replaces$radiuswith10pxin the output CSS.Final Answer:
.box { border-radius: 10px; background: blue; } -> Option CQuick Check:
Mixin argument replaces variable in output [OK]
- Leaving variable name $radius in output CSS
- Omitting units like px in argument
- Ignoring mixin styles in output
@mixin text-style {
font-size: 1.2rem;
color: black;
}
.title {
@include text-style;
font-weight: bold;
}Solution
Step 1: Check @include syntax
When including a mixin, parentheses are required even if no arguments are passed.Step 2: Verify other parts
Mixin definition without parentheses is allowed if no arguments. Mixin names do not start with$. @include is valid inside selectors.Final Answer:
Missing parentheses after mixin name in @include. -> Option DQuick Check:
@include needs parentheses even if empty [OK]
- Omitting parentheses in @include
- Thinking mixin names start with $
- Believing @include can't be 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?Solution
Step 1: Define mixin with parameter for border radius
The mixincardmust accept$radiusto customize border radius.Step 2: Include mixin with argument
When including, pass the desired radius value like@include card(1rem);.Step 3: Check other options for errors
@mixin card { background: white; box-shadow: 0 2px 5px rgba(0,0,0,0.1); border-radius: $radius; } .container { @include card(1rem); } uses$radiuswithout 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.Final Answer:
@mixin card($radius) { background: white; box-shadow: 0 2px 5px rgba(0,0,0,0.1); border-radius: $radius; } .container { @include card(1rem); } -> Option BQuick Check:
Define mixin with param, include with argument [OK]
- Using variables inside mixin without parameters
- Forgetting parentheses in @include
- Not passing required arguments when including
Ready to prove it?
Pass the test and this pattern moves unknown
