Technology News

Multi-Value CSS Properties With Optional Custom Property Values

Imagine you have an element with a multi-value CSS property, such as transform: optional custom property values:

.el { transform: translate(100px) scale(1.5) skew(5deg);
}

Now imagine you don’t always want all the transform values to be applied, so some are optional. You might think of CSS optional custom property values:

.el { /* |-- default ---| |-- optional --| */ transform: translate(100px) var(--transform);
}

But surprisingly using optional custom property values like this does not work as intended. If the --transform variable is not defined the whole property will not be applied. I’ve got a little “trick” to fix this and it looks like this:

.el { transform: translate(100px) var(--transform, );
}

Notice the difference? There is a fallback defined in there that is set to an empty value: (, )

That’s the trick, and it’s very useful! Here’s what the specification has to say:

In an exception to the usual comma elision rules, which require commas to be omitted when they’re not separating values, a bare comma, with nothing following it, must be treated as valid in var(), indicating an empty fallback value.

This is somewhat spiritually related to the The CSS Custom Property Toggle Trick that takes advantage of a custom property having the value of an empty space.

Demo

Like I said, this is useful and works for any multi-value CSS property. The following demo shows it using text-shadow, background, and filter in addition to the transform example we just discussed.

See the Pen CSS var – Fallback To Nothing by Yair Even Or (@vsync) on CodePen.

Some properties that accept multiple values, like text-shadow, require special treatment because they only work with a comma delimiter. In those cases, when the CSS custom property is defined, you (as the code author) know it is only to be used in a situation where a value is already defined where the custom property is used. Then a comma should be inserted directly in the custom property before the first value, like this:

--text-shadow: ,0 0 5px black;

This, of course, inhibits the ability to use this variable in places where it’s the only value of some property. That can be solved, though, by creating “layers” of variables for abstraction purposes, i.e. the custom property is pointing to lower level custom properties.

Beware of Sass compiler

While exploring this trick, uncovered a bug in the Sass compiler that strips away the empty value (,) fallback, which goes against the spec. I’ve reported the bug and hope it will be fixed up soon.

As a temporary workaround, a fallback that causes no rendering can be used, such as:

transform: translate(100px) var(--transform, scale(1));
Top 7 Best WordPress Plugin Of All Time
9 New React and JavaScript Links for February 2022

Related Articles

5 Most Famous Illustrators

5-most-famous-illustrators
Illustrations are great pieces of art. There are so many great works out there, and we’ll be taking on some of the most influential and famous illustrators, and their works.…

How to Revamp Your Phone Case

How to Revamp Your Phone Case
Every so often, a new ‘trendy’ phone case is launched that becomes a staple for everyone. For this very reason, we’re going to discuss budget-friendly methods you can use to…

Tips For Demand Planning For A Restaurant

tips-for-demand-planning-for-a-restaurant
Tips for demand planning for a restaurant Sitting in a nice restaurant with friends and family ordering food good food and drinks is always a mood changer. Eating fancy without…

Comparing JAWS, NVDA, and VoiceOver

Comparing JAWS, NVDA, and VoiceOver
A screen reader is an important accessibility tool for people with no or limited vision. People who are blind or those with low vision can use a screen reader to…