Media Queries with SASS Mixins - A Tale of Reusability and Convenience

Media Queries with SASS Mixins - A Tale of Reusability and Convenience

Media Queries with SASS Mixins - A Tale of Reusability and Convenience - Improve your code by writing media queries as independent chunks of code in the form of mixins.

There are days where I feel lucky to be living in an era where we have CSS preprocessors to make our life easier. Besides other advantages, preprocessors like SASS allow us to follow the DRY (Don’t Repeat Yourself). For those of you who are unfamiliar with this principle, the main idea behind it is that we should avoid repeating the same bits of code in our files. Let’s take a look at how SASS helps us to do that while setting media queries.


Step 1 —Breakpoint Sizes:

Let’s standardize our screen sizes by declaring them as variables. " /* Screen sizes */ $sm = 768px; // Small $md = 992px; // Medium $lg = 1200px; // Large "

Step 2 — Mixins:

Set your media queries as mixins using if/else statements.

" /* Media Queries */ @mixin respond($breakpoint) { // Name your mixin 'respond', or whatever you like. // '$breakpoint' is the argument we pass to our mixin.

@if $breakpoint == sm { @media (max-width: $sm) { // Sets max-width as 768px, which we declared in our variables file. @content; // @content includes whatever styles we declare. } }

@else if $breakpoint == md { @media (max-width: $md) { @content; } }

@else if $breakpoint == lg { @media (max-width: $lg) { @content; } } } "

front-end-development coding sass

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Hire Front-end Developer | Dedicated Front-end Programmers In India

Hire Front-end web developers and programmers in India from ValueCoders for your custom offshore Front-end application development projects. Hire Front-end developer from ValueCoders and get interactive Front-end web designs. 16+ years exp. , 100% money back guarantee, 450+ Staff . Contact Us Today!

Top 10 Front End Development Companies In 2021

Looking for a trustworthy front end development company? Read this blog to know the Top 10 Front End Development Companies in 2021.

Top 15 Front-end Development Tools To Use In 2021

Web technologies are moving so fast that it is becoming a tough job for businesses as well as developers to keep up the pace. New tools pop up every year letting the old ones become obsolete (Remember Eclipse?). Front-end development is a major part of the web and front-end technologies have grown multifold in the past years. Businesses are focusing more

Top 6 Productivity Tools for Front-end Developers

Today I wanted to share with you the tools I daily use to increase my productivity as a front-end developer. 6 tools to increase your productivity as a Front-end developer.

Hire Front End Developer | Hire Dedicated Front End Programmers In India

Hire Front-end web developers and programmers in India from ValueCoders for your custom offshore Front-end application development projects. Hire Front-end developer from ValueCoders and get interactive Front-end web designs. 16+ years exp. , 100% money back guarantee, 450+ Staff . Contact Us Today!