Member-only story

CSS Variables

Define repeated styles without the use of a preprocessor

Alex Z
2 min readNov 12, 2021

The latest specification for CSS introduced a number of new features that are starting to allow us to ditch preprocessors like SASS and others, and go back to pure CSS.

credit: https://responsivedesign.is/news/2017/06/using-css-variables-correctly/

Variables are one such feature. allowing you to create variables just like you would in SASS.

The syntax is a bit different, with the double dash “--“ telling CSS to interpret the property as a variable and the var() CSS function used to call that variable value.

You might notice in the above that the variable is defined in the:root selector. This demonstrates scoping of variables in CSS, one of its most valuable features. If you override the variable at a lower level of specificity, the more specific variable definition is used. This is useful for cases where you want to override variable values when applying a light/dark theme, or for locale-specific styles like text-orientation, anything where variable overrides need to happen for some subset of the styled content.

--

--

Alex Z
Alex Z

Written by Alex Z

Software Developer + Product Manager. Interested in Travel, Culture, Economics and the Internet. Join Medium: https://tinycode.medium.com/membership

No responses yet