5

According to MDN, the var function accepts multiple fallbacks. But I am unable to get them to work.

I tried the following code on Chrome, Firefox and Safari. The background-color for .demo3 is transparent on all the above browsers.

Is it an error with the code, or is it because the browsers have not implemented variable fallback?

:root {
  --my-var: red;
}

.demo1 {
  background-color: var(--my-var);
}

.demo2 {
  background-color: var(--my-background, pink);
}

.demo3 {
  background-color: var(--my-background, --my-var, pink);
}
<div class="demo1">No fallback</div>

<div class="demo2">Single fallback</div>

<div class="demo3">Multiple fallbacks</div>

1 Answer 1

17

For multiple fallbacks try: background-color: var(--my-background, var(--my-var, pink));

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.