Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

CSS Sass Basics (retired) Advanced Sass Concepts Working with Media Queries

alahab2
alahab2
9,709 Points

Variables in Media queries in Sass

Reading an article about variables in media queries in Sass i saw this code:

You're not limited to using variables in the numerical part of a @media test. Go ahead and set the whole test as a variable. (Note the need for the interpolation braces #{})>

$information-phone: "only screen and (max-width : 320px)";

@media #{$information-phone} {
  background: red;
}

Unfortunately there is no further explanation about interpolation braces #{}

Can anyone give some more information about it?

P.s. here is the article itself http://thesassway.com/intermediate/responsive-web-design-in-sass-using-media-queries-in-sass-32

3 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi K SA,

You can find more information about interpolation in Sass at the below link.

http://sass-lang.com/documentation/file.SASS_REFERENCE.html#interpolation_

In the past you needed interpolation a lot more then now as the Sass compiler has been upgraded vastly since the release of 3.2 so you don't need #{} everywhere in your code, for your example above you can simply use the variable you have inline after @media.

@media $information-phone {
  background: red;
}

Interpolation braces #{} evaluate a statement within the braces and print it's result. so if you place a variable in the interpolation braces, the value of the variable will be evaluated and used in its place.

$information-phone: "only screen and (max-width : 320px)";

@media #{$information-phone} {
  background: red;
}

/* Will evaluate to: */

@media only screen and (max-width : 320px) {
  background: red;
}