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!

In this week's roundup: Firefox gains locksmith-like powers, Samsung's Galaxy Store starts supporting Progressive Web Apps, CSS Subgrid is shipping in Firefox

In this week's roundup: Internet Explorer finds its way into Edge, Google Search Console touts a new speed report, and Firefox gives Facebook's notification

You’re probably already at least a little familiar with CSS variables. If not, here’s a two-second overview: they are really called custom properties, you set

Building websites is programming. Writing HTML and CSS is programming. I am a programmer, and if you're here, reading CSS-Tricks, chances are you're a

Here's what I'd like you to know upfront: this is a hard problem. If you've landed here because you're hoping to be pointed at a tool you can run that tells

Picture-in-Picture made its first appearance on the web in the Safari browser with the release of macOS Sierra in 2016. It made it possible for a user to pop

Gatsby does a great job processing and handling images. For example, it helps you save time with image optimization because you don’t have to manually

I learned something about percentage-based (%) padding today that I had totally wrong in my head! I always thought that percentage padding was based on the


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

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.