Less variables in media queries
See original GitHub issueWe have the following code:
@baseWidth: 100%;
div.div1 {
width: @baseWidth / 4;
float: left;
height: 100% / 3;
}
div.div2 {
width: @baseWidth - 15%;
font-size: 16px;
span.span1 {
display: inline-block;
width: @baseWidth / 2 - 15%;
}
}
which translates into the following code:
div.div1 {
width: 25%;
float: left;
height: 33.333%;
}
div.div2 {
width: 85%;
font-size: 16px;
}
div.div2 span.span1 {
display: inline-block;
width: 35%;
}
However, I would want to have the baseWidth at 80% if the screen resolution is lower than 1025px, so my basic instinct is to do this, which is very readable and nice:
@baseWidth: 100%;
@media (max-width: 1024px) {
@baseWidth: 80%;
}
div.div1 {
width: @baseWidth / 4;
float: left;
height: 100% / 3;
}
div.div2 {
width: @baseWidth - 15%;
font-size: 16px;
span.span1 {
display: inline-block;
width: @baseWidth / 2 - 15%;
}
}
However, this doesn’t work, of course, because variables are evaluated at compile time, media queries at runtime.
But how about auto-creating media-queries for every property in every selector that uses that variable? That would translate into:
div.div1 {
width: 25%;
float: left;
height: 33.333%;
}
@media (max-width: 1024px) {
div.div1 {
width: 20%;
}
}
div.div2 {
width: 85%;
font-size: 16px;
}
@media (max-width: 1024px) {
div.div2 {
width: 65%;
}
}
div.div2 span.span1 {
display: inline-block;
width: 35%;
}
@media (max-width: 1024px) {
div.div2 span.span1 {
width: 5%;
}
}
The algorithm could be optimized to group media queries and stuff, but you get the idea.
Issue Analytics
- State:
- Created 9 years ago
- Reactions:5
- Comments:11 (7 by maintainers)
Top Results From Across the Web
LESS CSS set variables in media query? - Stack Overflow
LESS compiles your media queries to actual media queries, so at compile time there is no indication of which media query will be...
Read more >Media Queries with Less | Packt Hub
The magic happens in two parts—immediately at the top of the file, we've set a number of Less variables, which encapsulate the media...
Read more >Media query variables in LESS (for Bootstrap ... - gists · GitHub
Media query variables in LESS (for Bootstrap, but could be used anywhere). Raw. readme.md. I got the idea from the Trello CSS guide....
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 Using Variables in Media Queries - W3Schools
Now we want to change a variable value inside a media query. Tip: Media Queries are about defining different style rules for different...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
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
This was implemented in
postcss-css-variables
.Hmm, yes, while these FRs (this one and #1192) started with a bit different things in mind they ended as an exactly the same story (“Less to implicitly autogenerate CSS rulesets/properties depending on different variable values inside media queries”).
Well, I guess it’s safe to close this by now too. In summary: while theoretically this is possible, in practice it would require too complex implementation, but what’s more important it would be too magical and too weird to use.
In short: learn and use mixins.