Performance: Reducing compiled CSS size
This affects page load speed by reducing CSS file size and parsing time, improving Largest Contentful Paint (LCP).
Jump into concepts and practice - no test required
$primary-color: #3498db; .common-text { color: $primary-color; } .button, .card, .alert { @extend .common-text; }
$primary-color: #3498db; .button { color: $primary-color; } .card { color: $primary-color; } .alert { color: $primary-color; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated CSS properties | No extra DOM nodes | No extra reflows | Higher paint cost due to larger CSS | [X] Bad |
| Shared styles with @extend | No extra DOM nodes | No extra reflows | Lower paint cost due to smaller CSS | [OK] Good |
@mixin name { ... }.@include is used to use a mixin, not define it. @function defines functions, not mixins. @extend is for inheritance, not mixin definition.$color: blue;
.button {
color: $color;
@include rounded-corners;
}
@mixin rounded-corners {
border-radius: 0.5rem;
border: 1px solid $color;
}
@include rounded-corners adds border-radius and border styles inside .button, these styles are duplicated in CSS for each use..card {
.header {
color: red;
.title {
font-weight: bold;
}
}
}
@extend behavior@extend shares selectors in compiled CSS, avoiding duplication by merging rules.@extend reuses styles without duplication.@extend to share styles between selectors -> Option A