@extend reduces duplication in the compiled CSS?%button-base {
padding: 1rem 2rem;
border-radius: 0.5rem;
font-weight: bold;
}
.btn-primary {
@extend %button-base;
background-color: blue;
color: white;
}The @extend directive in Sass allows selectors to share styles by merging them into a single CSS rule. This means the styles are written once, and multiple selectors point to that rule, reducing duplication in the final CSS.
%alert {
padding: 1rem;
border: 1px solid red;
}
.error {
@extend %alert;
background-color: pink;
}
.warning {
@extend %alert;
background-color: yellow;
}The @extend directive merges selectors that share the same styles into a single CSS rule. The placeholder %alert does not appear in the output. The background colors remain separate for each class.
%card {
box-shadow: 0 0 5px rgba(0,0,0,0.3);
padding: 1rem;
}
.profile-card {
@extend %card;
border-radius: 0.5rem;
}
.product-card {
@extend %card;
border-radius: 1rem;
}The @extend directive merges selectors that use the placeholder %card into a combined selector list. The placeholder itself does not appear in the output.
@extend on CSS file size and browser layout performance?By merging selectors with shared styles, @extend reduces the total CSS code size. Smaller CSS files load faster and browsers can render pages more efficiently.
@extend might unintentionally harm accessibility?If you extend a class that hides content visually (like .sr-only or similar) into an element that should be visible, it can hide important content from screen readers and users, harming accessibility.