Variable Cascading

suggest change

CSS variables cascade in much the same way as other properties, and can be restated safely.

You can define a variable more than once. The winning declaration follows the cascade: when specificity is equal, the later one applies.

Assuming this HTML:

<a class="button">Button Green</a>
<a class="button button_red">Button Red</a>
<a class="button">Button Hovered On</a>

We can write this CSS:

.button {
  --color: green;
  padding: .5rem;
  border: 1px solid var(--color);
  color: var(--color);
}

.button:hover {
  --color: blue;
}

.button_red {
  --color: red;
}

And get this result:

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents