Posted about 1 month ago. Visible to the public. Repeats.

CSS variables aka CSS Custom Properties

CSS variables are very different from preprocessor variables. While preprocessors use variables to compile a static piece of CSS, CSS custom properties are a reactive (i.e. live) part of the styles. Think of them like usual CSS properties that cascade, but have:

  • a special syntax: CSS variables always start with a double-dash (--color)
  • no inherent meaning: Defining a CSS variable will not change any styles in itself
  • a special functionality: CSS variables can be used within the values of other properties, including CSS variables (var(--color))
  • a superpower: they can be used to influence the Shadow DOM of Web Components

CSS variables are a means to store CSS values with elements and have them cascade according to the DOM structure instead being based on the CSS structure (as it is in preprocessors). This open up a whole new set of possibilities, especially for adapting components. E.g. button styles could be changed by simply setting a variable, with no need to even know what CSS properties use that value.

They're supported by all modern browsers.

Quick example

Copy
body { --shadow-color: gray; } button { box-shadow: .1em .1em .1em var(--shadow-color); } button:hover { --shadow-color: skyblue; }

Resources

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Owner of this card:

Avatar
Dominik Schöler
Last edit:
about 1 month ago
by Tobias Kraze
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Dominik Schöler to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more