Set variables using media query
See original GitHub issueI would like to be able to set variables using media queries. This would mean that with the following input:
@media (max-width: 300px) {
$padding: 4px;
}
@media (max-width: 600px) {
$padding: 10px;
}
div.content {
padding: $padding;
}
I would expect the following output:
@media (max-width: 300px) {
div.content {
padding: 4px;
}
}
@media (max-width: 600px) {
div.content {
padding: 10px;
}
}
This would provide a convenient way of setting variables based on screen width without largely repeating ourselves with the rest of the CSS code that must be written.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:4
- Comments:5 (1 by maintainers)
Top Results From Across the Web
CSS Using Variables in Media Queries - W3Schools
Here, we first declare a new local variable named --fontsize for the .container class. We set its value to 25 pixels. Then we...
Read more >Want CSS variables in media query declarations? Try this!
Let's see why CSS variables / custom properties fail in media queries + some workarounds to try.
Read more >CSS native variables not working in media queries
A CSS property declaration acts as a "setter", it sets a value for an element, while the max-width in a media query is...
Read more >Using CSS Media Queries and Variables for Responsiveness
"In this article, we'll learn how to make responsiveness for manageable using CSS Media queries and Variables."
Read more >CSS Variables in Media Queries - DZone Web Dev
Announcement · A custom media query declaration starts with the @custom-media statement. · The second place is occupied by the custom media query...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
While this would be pretty cool, it’s very much contrary to the existing semantics of a Sass stylesheet. What you’ve outlined already has a meaning: it sets the variable twice in sequence and then accesses it (modulo some scoping issues). The sequential and largely imperative nature of Sass means that the sort of automatic duplication of rules you’re describing becomes very complex. We actually considered something very similar for handling SassScript
&
, and ended up realizing that it was effectively impossible to do so and preserve the existing semantics.The best approximation you can do right now is to put the styles that need to vary in a mixin with parameters and include that in your media queries.
+1 for this