Skip to main content

Command Palette

Search for a command to run...

How to Write Media Queries with SASS Mixins

Learn How to Use Media Queries with SASS Mixins

Published
4 min read
How to Write Media Queries with SASS Mixins
R

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js, and enjoy working with modern tech stacks to build innovative solutions. I'm committed to writing clean and efficient code, following best practices, and staying updated with the latest industry trends.

Throughout my career, I've gained valuable experience in full-stack development, including frontend design, backend development, and database management. I love solving complex problems and implementing creative solutions that enhance user experiences. I believe in the power of collaboration and thrive in cross-functional teams where I can contribute my skills and learn from others.

Beyond coding, I am an avid learner and enjoy sharing my knowledge with the developer community. I regularly contribute to open-source projects, write technical blog posts, and engage in discussions on emerging technologies. I'm always seeking opportunities to expand my skill set and stay at the forefront of software engineering.

When building websites and apps, we’ll often use media queries to help ensure our layouts’ are responsive. 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 that 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 when 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, I’ve written up 5 common breakpoints into @mixin blocks.

The @content syntax is a directive of SASS that 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 set up 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.

Conclusion

If you liked this blog post, follow me on Twitter where I post daily about Tech related things!

🌎 Let's Connect

SASS Series

Part 14 of 15

Take your CSS skills to the next level with my SASS tutorial series! My comprehensive tutorials cover everything from the basics of variables and mixins to advanced techniques like nesting!

Up next

SASS Build Process Tutorial

Learn How to Setup a SASS Build Process

More from this blog

Rembert Designs Blog | Helping Web Developers And Designers with Resources

107 posts

Passionate Software Engineer with three years of experience, dedicated to creating robust and scalable web applications. I have a strong foundation in JavaScript, React, Python, and Node.js.