Performance: Extending vs mixing comparison
This affects CSS file size and browser rendering speed by changing how styles are reused and combined.
%button-style {
padding: 1rem 2rem;
border-radius: 0.5rem;
background-color: blue;
color: white;
}
.primary-button {
@extend %button-style;
}
.secondary-button {
@extend %button-style;
background-color: gray;
}@mixin button-style { padding: 1rem 2rem; border-radius: 0.5rem; background-color: blue; color: white; } .primary-button { @include button-style; } .secondary-button { @include button-style; background-color: gray; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| @mixin reuse | No change | No change | Higher due to larger CSS | [!] OK |
| @extend reuse | No change | No change | Lower due to smaller CSS | [OK] Good |
@extend and @mixin in Sass?@extend behavior@extend merges selectors that share the same styles, reducing CSS size by combining rules.@mixin behavior@mixin copies the style block wherever it is included, allowing flexible reuse but increasing CSS size.@extend shares styles by combining selectors, @mixin copies styles into selectors. -> Option A@extend combines, @mixin copies [OK]button-style in Sass?@include mixin-name; syntax.@extend is for extending selectors, @mixin defines mixins, and @use imports modules.@include to add mixins [OK]@mixin red-text { color: red; }
.button { @include red-text; }What CSS will be generated?red-text defines color: red;. Including it copies this style into .button..button { color: red; } because the mixin's styles are copied inside the selector..alert { color: red; }
.error { @extend .alert; }
.error { background: yellow; }@extend merges selectors with the same styles. Extending .alert inside .error is valid..error are allowed and combined in CSS output.@extend merges selectors but cannot accept parameters. @mixin can accept parameters to customize styles.@mixin with a color parameter copies styles with different colors without duplicating selectors.@mixin with a color parameter to copy styles with different colors. -> Option CPass the test and this pattern moves unknown