When building websites and apps, weâll often use media queries to help ensure our layoutsâ responsivity. This way, they can adapt to suit the multitudes of devices accessing them, as we need to ensure all of our users are catered for when engaging with our products.
CSS Media Queries
If you have experience with CSS, youâll likely know that a media query is a CSS3 module which allows content to render and adapt to conditions. Such as screen resolution, for example:
@media (max-width: 599px) {
font-size: 1rem;
}
Here, weâve set the font size to 1rem
when the viewport size is <= 599px
.
This is simple enough, and of course weâll need multiple media queries to have a fully responsive site.
However, inevitably, the time will come where weâll need to edit the rules for the actual breakpoints, and searching through all of your code to change every line affected by a rule change is far from ideal. There has to be a better way!
Modern layout specifications have improved greatly over the past few years, Grid and Flexbox are responsive by default. This helps us to cut down on the amount of media queries required in our projects & makes for cleaner code.
However, media queries still have a place in modern web development. And as our projects grow bigger, we need a better way of managing them.
Enter SASS mixins!
Mixins
As we know (see mixins), SASS mixins give us the ability to create reusable chunks of code â they reduce repetition, promote dry code and allow for ease of maintenance.
So writing media queries as mixins to inject into your stylesheets, wherever theyâre required makes a whole lot of sense!
Letâs now look at an example..
Setting up your mixins
@mixin for-phone-only {
@media (max-width: 599px) { @content; }
}
@mixin for-tablet-portrait-up {
@media (min-width: 600px) { @content; }
}
@mixin for-tablet-landscape-up {
@media (min-width: 900px) { @content; }
}
@mixin for-desktop-up {
@media (min-width: 1200px) { @content; }
}
@mixin for-big-desktop-up {
@media (min-width: 1800px) { @content; }
}
Here, weâve written up 5 common breakpoints into @mixin
blocks.
The @content
syntax is a directive of SASS which allows for the insertion of content later on, as you likely wonât be writing all of your queries at once.
Using a mixin
Say we want to use a mixin to reduce the font size of our header text on mobile devices.
We add the mixin (nested under the appropriate class) as an include, like so:
.header-title {
font-size: 2rem;
@include for-phone-only {
font-size: 1rem;
}
}
When we compile our project, our @include will translate into:
@media (max-width: 599px) {
font-size: 1rem;
}
We now have the ability to easily insert our media queries wherever we like throughout our projects!
Additionally, we wonât need to memorize our breakpoints, as we have them pre-defined. If we need to alter them, we can simply edit the mixins.
Another way to setup our mixins
If you want to take it a step further, you could use conditionals to setup your mixins.
You create just one @mixin
block and set the parameters to be passed in â which we use to select our breakpoints, like so:
@mixin for-size($size) {
@if $size == phone-only {
@media (max-width: 599px) { @content; }
} @else if $size == tablet-portrait-up {
@media (min-width: 600px) { @content; }
} @else if $size == tablet-landscape-up {
@media (min-width: 900px) { @content; }
} @else if $size == desktop-up {
@media (min-width: 1200px) { @content; }
} @else if $size == big-desktop-up {
@media (min-width: 1800px) { @content; }
}
}
Then, to use our mixins in this manner, weâd select it like so:
.header-title {
font-size: 2rem;
@include for-size(phone-only) {
font-size: 1rem;
}
}
Itâs that simple!
Writing media queries doesnât need to be a headache, and by using SASS mixins, we have one centralized location in which to manage our media queries.
Handling the responsivity of our projects just got a whole lot easier!
Related Posts:
A little about me..
Hey, Iâm Tim! đ
Iâm a freelance business owner, web developer & author. I teach both new and experienced freelancers how to build a sustainable and successful freelancing business. Check out my Complete Guide to Freelancing if you'd like to find out more.
While you're here, you can browse through my blogs where I post freelancing tips, code tutorials, design inspiration, useful tools & resources, and much more! You can also join the newsletter, or find me on X.
Thanks for reading! đ