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"> <p>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).</p> <h3 id="Solution">Solution</h3> <p>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:</p> <pre class="brush:php;toolbar:false">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!

What does it look like to refactor your own code? John Rhea picks apart an old CSS animation he wrote and walks through the thought process of optimizing it.

CSSanimationsarenotinherentlyhardbutrequirepracticeandunderstandingofCSSpropertiesandtimingfunctions.1)Startwithsimpleanimationslikescalingabuttononhoverusingkeyframes.2)Useeasingfunctionslikecubic-bezierfornaturaleffects,suchasabounceanimation.3)For

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version
