Performance: Placeholder selectors with %
This affects CSS bundle size and rendering speed by reducing duplicated styles and minimizing CSS rules sent to the browser.
%common-style { color: red; font-weight: bold; }
.button { @extend %common-style; }
.alert { @extend %common-style; }.button { color: red; font-weight: bold; } .alert { color: red; font-weight: bold; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeating styles in multiple selectors | N/A | 0 | Low | [X] Bad |
| Using % placeholder selectors with @extend | N/A | 0 | Low | [OK] Good |
%?% and are used to hold styles that are not output directly but can be reused.@extend to apply the stored styles without duplicating CSS.%button-style inside a Sass rule?@extend %placeholder-name; inside a selector.@extend %button-style; correctly. The other options use incorrect keywords or syntax.%alert { color: red; font-weight: bold; }
.warning { @extend %alert; background: yellow; }What CSS will be generated?.warning class extends %alert, so it gets the styles from %alert plus its own background style.%card-style {
border: 1px solid black;
}
.card {
extend %card-style;
padding: 1rem;
}@ symbol before extend to work properly.extend %card-style; without @, causing a syntax error..primary-btn and .secondary-btn, sharing common styles but with different colors. Which Sass code correctly uses placeholder selectors to avoid repeating common styles?% and hold common styles to be extended by other selectors.%btn-common {
padding: 1rem;
border-radius: 0.5rem;
}
.primary-btn {
@extend %btn-common;
background-color: blue;
color: white;
}
.secondary-btn {
@extend %btn-common;
background-color: gray;
color: black;
} correctly defines %btn-common with shared styles and extends it in both buttons, adding different colors separately. .btn-common {
padding: 1rem;
border-radius: 0.5rem;
}
.primary-btn {
@extend .btn-common;
background-color: blue;
color: white;
}
.secondary-btn {
@extend .btn-common;
background-color: gray;
color: black;
} incorrectly uses a class instead of a placeholder. %btn-common {
padding: 1rem;
border-radius: 0.5rem;
background-color: blue;
}
.primary-btn {
@extend %btn-common;
color: white;
}
.secondary-btn {
@extend %btn-common;
background-color: gray;
color: black;
} mixes shared and specific colors in the placeholder, causing unwanted style inheritance. %btn-common {
padding: 1rem;
border-radius: 0.5rem;
}
.primary-btn {
background-color: blue;
color: white;
}
.secondary-btn {
background-color: gray;
color: black;
} does not use placeholders at all, repeating styles.Pass the test and this pattern moves unknown