Reader small image

You're reading from  Sass and Compass for Designers

Product typeBook
Published inApr 2013
Reading LevelBeginner
Publisher
ISBN-139781849694544
Edition1st Edition
Languages
Tools
Concepts
Right arrow
Author (1)
Ben Frain
Ben Frain
author image
Ben Frain

Ben Frain has been a web designer/developer since 1996. He is currently employed as a UI-UX Technical Lead at bet365. Before the web, he worked as an underrated (and modest) TV actor and technology journalist, having graduated from Salford University with a degree in Media and Performance. He has written four equally underrated (his opinion) screenplays and still harbors the (fading) belief he might sell one. Outside of work, he enjoys simple pleasures: playing indoor football while his body and wife still allow it and wrestling with his two sons.
Read more about Ben Frain

Right arrow

Inline media queries with Sass


However, while we can split media queries into a separate partial, hopefully, in this chapter, you'll be convinced there is a better way of authoring media queries with Sass.

Let's suppose a container needs to be different widths at different viewports. With Sass, we can produce exactly the same outcome as our prior example by writing the following code:

.style {
  color: $color1;
  @include MQ(Splus) { 
    width: 80%;
  }
  @include MQ(Mplus) { 
    width: 70%;
  }
  @include MQ(Lplus) { 
    width: 60%;
  }
} 

After choosing a selector, any property and value pairs that will be identical across various viewport sizes (color and font-family perhaps) are declared.

Then, the various media query breakpoint styles are nested within. These nested media queries 'bubble up' and produce the same CSS as before:

.style {
  color: red;
}
@media only screen and (min-width: 30em) {
  .style {
    width: 80%;
  }
}
@media only screen and (min-width: 47em) {
  .style {
    width...
lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Sass and Compass for Designers
Published in: Apr 2013Publisher: ISBN-13: 9781849694544

Author (1)

author image
Ben Frain

Ben Frain has been a web designer/developer since 1996. He is currently employed as a UI-UX Technical Lead at bet365. Before the web, he worked as an underrated (and modest) TV actor and technology journalist, having graduated from Salford University with a degree in Media and Performance. He has written four equally underrated (his opinion) screenplays and still harbors the (fading) belief he might sell one. Outside of work, he enjoys simple pleasures: playing indoor football while his body and wife still allow it and wrestling with his two sons.
Read more about Ben Frain