Performance: Content blocks with @content
This affects CSS preprocessing time and the final CSS bundle size, indirectly influencing page load speed.
@mixin button-style($color) { background-color: $color; border-radius: 0.5rem; padding: 1rem 2rem; font-weight: bold; @content; } .button-primary { @include button-style(blue) { color: white; text-transform: uppercase; } } .button-secondary { @include button-style(gray) { color: white; text-transform: uppercase; } }
@mixin button-style($color) { background-color: $color; border-radius: 0.5rem; padding: 1rem 2rem; font-weight: bold; // Repeating styles without @content color: white; text-transform: uppercase; } .button-primary { @include button-style(blue); color: white; text-transform: uppercase; } .button-secondary { @include button-style(gray); color: white; text-transform: uppercase; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeating styles without @content | No impact | No impact | No impact | [X] Bad - larger CSS bundle |
| Using @content to inject styles | No impact | No impact | No impact | [OK] Good - smaller CSS bundle |
@content in Sass?@content does@content allows you to insert custom CSS blocks inside a mixin, making it flexible.@content.@content inside a mixin?@content; is used to insert the passed block.@content; correctly with the semicolon.@mixin card {
border: 1px solid black;
padding: 1rem;
@content;
}
.box {
@include card {
background-color: yellow;
}
}card adds border and padding, then inserts the passed block with @content.@include card { ... } adds background-color: yellow; inside the mixin's content.@content:@mixin highlight {
background: yellow;
@content
}
.text {
@include highlight {
color: red;
}
}@content inside mixin@content must end with a semicolon to be valid.@content. Which Sass code correctly achieves this?@content.@content inside and inside &:hover, then includes it with a block that passes normal and hover styles. This correctly applies custom hover styles.@content twice 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 @content outside mixin context.Pass the test and this pattern moves unknown