Closed Bug 996392 Opened 11 years ago Closed 11 years ago

CSS variable not work in media query

Categories

(Core :: CSS Parsing and Computation, defect)

x86
macOS
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: gasolin, Unassigned)

Details

I'd like to use CSS variable to define some breakpoints for responsive design. have a css media query with variable ``` :root { var-red: #F00; var-green: #0F0; var-large: 800px; } body { background: var(green); } @media screen and (max-width: var(large)) { body { background: var(red); } } ``` actual: The page always green since @media query not functional with CSS variable. expect: var(large) in @media works
Hi Fred, two things. First, you can't use CSS Variables inside media query expressions. The media queries spec would need to be updated to allow that somehow -- for example by defining that variables are resolved against the root. I think that could be a useful thing; I'll mail www-style about it. Second, the syntax for CSS Variables changed recently in bug 985838 due to a spec change. You would now write the first bit of the style sheet like this now: :root { --red: #F00; --green: #0F0; --large: 800px; } body { background: var(--green); }
Status: NEW → RESOLVED
Closed: 11 years ago
Resolution: --- → INVALID
As I asked in that thread what happens with: @media screen and (max-width: var(--large)) { :root { --large: 200px; } } ?
Good question. :(
I've awared the syntax change part. Thanks for the information. Does there anywhere to get more information about where can I or cannot use CSS variable? Since it's a bit confused that if I can't use CSS variable somewhere in a CSS file.
I'm not sure if there is any concise documentation about where you can use variables. But currently in Gecko you can only use them in two places: (1) in regular property declarations (and DOM APIs that set property values), regardless of whether they are inside an at-rule; and (2) in @supports conditions, but here it is really just testing whether variables are supported at all, as it doesn't substitute a particular value for the variable but just tests whether the property value parses correctly with the variable references in there.
Thanks it's clear :)
You need to log in before you can comment on or make changes to this bug.