@extend with multiple selectors. What is the main effect on the compiled CSS selectors?@mixin button-style { background: blue; color: white; } .btn-primary { @extend .btn, .btn-large; @include button-style; }
Using @extend with multiple selectors merges all selectors into one rule in the compiled CSS. This can cause long combined selectors, which is called selector bloat.
Using a placeholder selector (starting with %) and extending it avoids selector bloat because placeholders do not output selectors themselves, only styles.
%alert-base {
padding: 1rem;
border-radius: 0.5rem;
}
.alert {
@extend %alert-base;
background: yellow;
}
.alert-error {
@extend %alert-base;
background: red;
}The placeholder %alert-base does not output CSS by itself. Both .alert and .alert-error extend it, so their selectors are combined with the shared styles. Then each adds its own background color.
@extend?Extending a selector with many comma-separated selectors merges all those selectors with the extending selector, causing large combined selectors and bloat.
.sr-only for screen readers. What is the best practice when using @extend with these selectors to avoid unintended style merging that could harm accessibility?Extending accessibility selectors like .sr-only can merge them with other selectors, causing styles to apply where not intended, harming accessibility. Using mixins or separate classes keeps styles isolated and safe.