Content blocks with @content let you pass a chunk of styles into a mixin. This helps you reuse code and keep your styles organized.
Content blocks with @content in SASS
@mixin mixin-name { /* common styles here */ @content; }
The @content directive is a placeholder for styles passed when you include the mixin.
You use @include mixin-name { ... } to pass the styles inside the curly braces.
@mixin box { border: 1px solid black; padding: 1rem; @content; } .container { @include box { background-color: lightblue; } }
@mixin button-style { font-weight: bold; @content; } .button { @include button-style { color: white; background-color: blue; } }
This example creates a card mixin with border, padding, and background color. The @content lets you add extra styles when you include it. The .article and .note classes use the mixin but add their own unique styles inside the block.
@use 'sass:color'; @mixin card { border: 2px solid black; padding: 1.5rem; border-radius: 0.5rem; background-color: color.scale(white, $lightness: -10%); @content; } .article { @include card { box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.1); } } .note { @include card { background-color: color.scale(yellow, $lightness: -20%); font-style: italic; } }
You can only use @content inside mixins, not in regular selectors.
If you don't pass any content when including the mixin, @content is simply ignored.
Using @content helps keep your Sass modular and easier to maintain.
@content lets you pass custom styles into a mixin.
Use it to create flexible, reusable style blocks.
It keeps your code clean and avoids repetition.
Practice
@content in Sass?Solution
Step 1: Understand what
@contentdoes@contentallows you to insert custom CSS blocks inside a mixin, making it flexible.Step 2: Compare with other options
Variables, imports, and loops are different Sass features unrelated to@content.Final Answer:
To pass custom styles into a mixin for flexible reuse -> Option AQuick Check:
@content = custom styles in mixin [OK]
- Confusing @content with variables or imports
- Thinking @content creates loops
- Assuming @content is for defining selectors
@content inside a mixin?Solution
Step 1: Recall the syntax for content blocks
Inside a mixin,@content;is used to insert the passed block.Step 2: Check each option
Only @mixin example { @content; } uses@content;correctly with the semicolon.Final Answer:
@mixin example { @content; } -> Option CQuick Check:
Correct syntax uses @content; inside mixin [OK]
- Omitting the @ symbol before content
- Using parentheses like a function call
- Forgetting the semicolon after @content
@mixin card {
border: 1px solid black;
padding: 1rem;
@content;
}
.box {
@include card {
background-color: yellow;
}
}Solution
Step 1: Understand mixin with @content
The mixincardadds border and padding, then inserts the passed block with@content.Step 2: Check how .box includes the mixin
The block inside@include card { ... }addsbackground-color: yellow;inside the mixin's content.Final Answer:
.box { border: 1px solid black; padding: 1rem; background-color: yellow; } -> Option BQuick Check:
Mixin + @content block = combined styles [OK]
- Thinking @content styles appear outside the selector
- Expecting separate CSS blocks for mixin and content
- Assuming syntax error due to block usage
@content:@mixin highlight {
background: yellow;
@content
}
.text {
@include highlight {
color: red;
}
}Solution
Step 1: Check syntax of
@contentinside mixin@contentmust end with a semicolon to be valid.Step 2: Verify other parts
Mixin name and block usage are correct; background property is valid CSS.Final Answer:
Missing semicolon after @content -> Option AQuick Check:
@content requires semicolon [OK]
- Omitting semicolon after @content
- Confusing mixin and content names
- Thinking block usage with @include is invalid
@content. Which Sass code correctly achieves this?Solution
Step 1: Understand the goal
Create a mixin that sets base button styles and allows custom styles including hover via@content.Step 2: Analyze each option
@mixin button { padding: 1rem; border: none; @content; &:hover { @content; } } .btn { @include button { color: blue; &:hover { color: red; } } } defines the mixin with@contentinside and inside &:hover, then includes it with a block that passes normal and hover styles. This correctly applies custom hover styles.Step 3: Why others fail
@mixin button { padding: 1rem; border: none; @content; &:hover { @content; } } tries to use@contenttwice in the mixin without passing blocks properly. @mixin button { padding: 1rem; border: none; @content; } .btn:hover { @content; } applies hover styles outside the mixin, losing reusability. @mixin button { padding: 1rem; border: none; } .btn { @include button; &:hover { @content; } } misuses@contentoutside mixin context.Final Answer:
@mixin button { padding: 1rem; border: none; @content; &:hover { @content; } } .btn { @include button { color: blue; &:hover { color: red; } } } -> Option DQuick Check:
Mixin with @content block for hover = @mixin button { padding: 1rem; border: none; @content; &:hover { @content; } } .btn { @include button { color: blue; &:hover { color: red; } } } [OK]
- Using @content outside mixin
- Trying to call @content multiple times without blocks
- Defining hover styles outside mixin losing flexibility
Ready to prove it?
Pass the test and this pattern moves unknown
