question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Set variables using media query

See original GitHub issue

I 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:closed
  • Created 9 years ago
  • Reactions:4
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
nex3commented, Apr 23, 2014

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.

0reactions
rizqinizamilcommented, Nov 13, 2015

+1 for this

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found