I'm trying to combine the use of a Sass variable with @media queries as follows:
$base_width:1160px;
@media screen and (max-width: 1170px) {$base_width: 960px;}
@media screen and (min-width: 1171px) {$base_width: 1160px;}
$base_width
is then defined at various points in the stylesheet width percentage-based measurements to produce fluid layouts.
When I do this, the variable seems to be recognized properly but the conditions for the media query are not. For example, the above code produces an 1160px layout regardless of screen width. If I flip-flop the @media statements like so:
@media screen and (min-width: 1171px) {$base_width: 1160px;}
@media screen and (max-width: 1170px) {$base_width: 960px;}
It produces a 960px layout, again regardless of screen width. Also note that if I remove the first line of $base_width: 1160px;
it returns an error for an undefined variable. Any ideas what I'm missing?
Similar to Philipp Zedler's answer, you can do it with a mixin. That lets you have everything in a single file if you want.
This is simply not possible. Since the trigger
@media screen and (max-width: 1170px)
happens on the client-side.Achieving your expected result would only be possible if SASS grabbed all rules and properties in your stylesheet containing your
$base_width
variable and copied/changed them accordingly.Since it won't work automatically you could do it by hand like this:
This is not really DRY but the best you can do.
If the changes are the same every time you could also prepare a mixin containing all the changing values, so you wouldn't need to repeat it. Additionally you can try to combine the mixin with specific changes. Like:
And the Mixin would look like this
Edit: Please do not use this solution. The answer by ronen is much better.
As a DRY solution, you can use the
@import
statement inside a media query, e.g. like this.You define all responsive elements in the file included using the variables defined in the media query. So, all you need to repeat is the import statement.
I had the same problem.
The
$menu-width
variable should be 240px on the mobile view@media only screen and (max-width : 768px)
and 340px on the desktop view.So i have simply created two variables:
And here is how i have used it: