Academy / Advanced Level: HTML and CSS
CSS Preprocessors: SASS/SCSS
Mixins for Reusable Code Blocks
Mixins in SASS/SCSS are blocks of reusable styles that can be included in multiple selectors. This is particularly useful for avoiding repetition and can include dynamic arguments to make the mixins more flexible.
Example of a Mixin:
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
border-radius: $radius;
}
.button {
@include border-radius(10px); /* Applying the mixin with a 10px radius */
}
In this example:
- The
@mixindirective defines a mixin namedborder-radius, which accepts a parameter$radius. - The mixin is then included in the
.buttonclass using the@includedirective, passing in a radius of10px.