@extend versus @mixin?@mixin button-style { padding: 1rem 2rem; border-radius: 0.5rem; background-color: blue; color: white; } .button1 { @extend .base-button; } .button2 { @include button-style; }
@extend merges selectors in the output CSS.@extend merges selectors sharing the same styles into one CSS rule, which reduces duplication and CSS size. @mixin copies the styles into every selector that includes it, increasing CSS size if used multiple times.
$color in Sass?@mixin colored-text($color) { color: $color; } .text { /* include mixin here */ }
@include is used to call mixins.You use @include followed by the mixin name and parentheses with arguments to include a mixin with parameters.
.button and not on .link?// Using @extend .button { @extend %hover-style; } .link { @extend %hover-style; } %hover-style { &:hover { color: red; } } // Using @mixin @mixin hover-style { &:hover { color: red; } } .button { @include hover-style; } .link { // no hover style included }
@extend merges selectors that extend the same placeholder, so both .button and .link share the hover style. The @mixin version applies hover only where included.
.alert after extending .message, .notification?.message, .notification { border: 1px solid black; } .alert { @extend .message, .notification; }
@extend adds the extending selector to all selectors it extends, combining them in one rule.
@extend versus @mixin for focus styles on interactive elements?@extend merges selectors, so focus styles might apply to elements that share selectors but are not intended to have focus styles, confusing users relying on keyboard navigation. @mixin applies styles only where included, keeping focus styles scoped and clear.