How to Inherit Values Using CSS Variables with a Fallback?
How to inherit values inside CSS variables
CSS variables, also known as custom properties, allow us to store and manipulate values in a way that can be reused throughout our stylesheet. However, one limitation of CSS variables is that they can't inherit values from their parent elements.
Problem
For example, consider the following code:
:root { --color: rgba(20, 20, 20, 0.5); /* Default value */ } .box { width: 50px; height: 50px; display: inline-block; margin-right: 30px; border-radius: 50%; position: relative; } .red { background: rgba(255, 0, 0, 0.5); } .blue { background: rgba(0, 255, 0, 0.5); } .box:before { content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; border-radius: 50%; transform: translateX(30px); background: var(--color); filter: invert(1); }
In this code, we have a :root rule that defines a --color variable with a default value of rgba(20, 20, 20, 0.5). We also have a .box class that sets some styles for a rectangular element, and a :before pseudo-element that creates a circular element inside the box.
The background property of the :before pseudo-element is set to var(--color), which means that it will inherit the value of the --color variable. However, we can override the value of the --color variable for each box by using inline styles, as shown in the following example:
<div class="box red">
The first two boxes will have their --color variable set to rgba(0, 255, 0, 0.5) and rgba(0, 255, 255, 0.5) respectively, while the third box will attempt to inherit the --color variable from its parent element. However, as we mentioned earlier, CSS variables cannot inherit values, so the --color variable for the third box will remain at its default value of rgba(20, 20, 20, 0.5).
Solution
The var() function provides a way to define a fallback value for a CSS variable in case the variable is not defined or is set to its initial value. The fallback value is specified as the second argument to the var() function, as shown in the following example:
background: var(--color, inherit);
In this example, if the --color variable is not defined or is set to its initial value, the background property will inherit the color of the parent element. This is exactly the behavior that we want in this case.
Here is the updated code with the fallback value added:
:root { --color: rgba(25, 25, 25, 0.5); /* Defined as the default value */ } .box { width: 50px; height: 50px; display: inline-block; margin-right: 30px; border-radius: 50%; position: relative; } .red { background: rgba(255, 0, 0, 0.5); } .blue { background: rgba(0, 0, 255, 0.5); } .box:before { content: ""; position: absolute; top: 0; left: 0; right: 0; bottom: 0; border-radius: 50%; transform: translateX(30px); background: var(--color, inherit); filter: invert(1); }
Now, all three boxes will inherit the color of their parent element, even if the --color variable is set to a different value in the inline styles.
The above is the detailed content of How to Inherit Values Using CSS Variables with a Fallback?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

It's out! Congrats to the Vue team for getting it done, I know it was a massive effort and a long time coming. All new docs, as well.

I had someone write in with this very legit question. Lea just blogged about how you can get valid CSS properties themselves from the browser. That's like this.

I'd say "website" fits better than "mobile app" but I like this framing from Max Lynch:

There are a number of these desktop apps where the goal is showing your site at different dimensions all at the same time. So you can, for example, be writing

The other day, I spotted this particularly lovely bit from Corey Ginnivan’s website where a collection of cards stack on top of one another as you scroll.
