Little Reminder About Custom Properties with Invalid Values

Chris Coyier Chris Coyier on

This is like one of those weirdly difficult quizzes about CSS. If you’ve got a <p> element sitting there in a totally normal basic HTML layout, then this CSS:

html {
  --color: blah;
}
body {
  color: blue;
}
p {
  color: green;
  color: var(--color);
}Code language: CSS (css)

What color does the <p> render as?

It’s blue. You might think it’s green, as the value blah is an invalid color. If the CSS had…

p {
  color: green;
  color: blah;
}Code language: CSS (css)

… then indeed it would be green. But CSS can’t know (in time) that the custom property is invalid, so it falls back to inheritance, which makes it blue from the body.

Wanna learn modern CSS layout?

Frontend Masters logo

We have an in-depth course on Advanced CSS Layouts that digs into the big stuff like grid and flexbox, and then more like custom properties and calc(), using them for advanced layouts and practical solutions.

7-Day Free Trial

Leave a Reply

Your email address will not be published. Required fields are marked *

$839,000

Frontend Masters donates to open source projects through thanks.dev and Open Collective, as well as donates to non-profits like The Last Mile, Annie Canons, and Vets Who Code.