For our examples, there are two types of Sass mixins we're going to use in this book: a mobile-first mixin that uses the min-width property and a desktop-first mixin that uses the max-width property. We already saw the following mixins and how they worked in Chapter 1, Harness the Power of Sass for Responsive Web Design, but here's a refresher.
We're going to use the following mobile-first mixin:
@mixin forLargeScreens($media) {
@media (min-width: $media/16+em) { @content; }
}This is how we use it:
header {
//Properties for small screens
width: 50%;
background: red;
@include forLargeScreens(640) {
//Properties for large screens
width: 100%;
background: blue;
}
}This compiles to the following:
header {
width: 50%;
background: red;
}
@media (min-width: 40em) {
header {
width: 100%;
background: blue;
}
}Here's the desktop-first mixin we're going to use:
@mixin forSmallScreens($media) {
@media (max-width: $media/16+em) { @content; }
}This is how we use it:
header {
//Properties for large screens
width: 100%;
background: purple;
@include forSmallScreens(640) {
//Properties for small screens
width: 50%;
background: yellow;
}
}
@include forSmallScreens
This compiles to the following:
header {
width: 100%;
background: purple;
}
@media (max-width: 40em) {
header {
width: 50%;
background: yellow;
}
}The great thing about using these mixins is that it's incredibly easy to find out which approach is being used, because we can see either the term forLargeScreens or forSmallScreens is repeated all over our SCSS file. If someone else were to edit any of the work we initially did, they will get a clear idea of which approach we used to build our site/app just by scanning the SCSS file.