how to reset a CSS variable (aka custom properties) and use the fallback one?
You can unset the value using initial
to use the fallback one:
:root { --border-width-top: 2px; --border-width-right: 2px; --border-width-bottom: 2px; --border-width-left: 2px; --border-width: 0;}div { margin:5px; border-color: red; border-style: solid; border-width: var(--border-width, var(--border-width-top) var(--border-width-right) var(--border-width-bottom) var(--border-width-left));}
div.box { --border-width:initial; --border-width-top: 10px;}
<div>some content</div><div class="box">some content</div>
How to set CSS variable to the value unset, --unset-it: unset ?
Like I did in this previous answer with inherit
You need to make unset
the fallback value and consider initial
to activate it:
* { box-sizing: border-box;}
#ex { background: yellow; padding: 0.5rem; margin: 1rem 0;}
#div1 { --unset-it: 2px dotted red; outline: var(--unset-it, unset);}
<div id="ex"> <p id="div1"> outline: 2px dotted red; </p> <p id="div1" style="--unset-it:initial"> outline: unset; </p></div>
css variable - default value on var not getting passed
Custom properties inherit.
From the spec:
Custom properties are ordinary properties, so they can be declared on
any element, are resolved with the normal inheritance and cascade
rules, ...
This means that if no value is set for a custom property on a given element, the value of its parent is used.
In this case, the parent element defines the custom property: --span
:
<div class="item" style="--span: 7">
So the value of the --span property will be 7 on all the children - unless a different value is explicitly defined on a child.
Now the difference between the two versions of the code is clear.
In the commented version, var(--span, 1)
in the .item class evaluates to 7 due to inheritance
Since there are only 2 columns in the grid, each item spans the full grid width
In the uncommented version (with --span: 1
; defined in the .item class), var(--span, 1)
in the .item class evaluates to 1 - because it is explicitly defined on the child.
NB
Fallback values are only used as the substitution value when a given custom property is invalid / not defined
In this case, the second item in the outer grid defines the --span custom property with value 7 and it is available to all its child elements.
How to store inherit value inside a CSS variable (aka custom property)?
In such case, we can consider the fallback value of a CSS variable. Like explained in the specification we can write something like this:
background:var(--color,inherit)
By doing this, we tell our property (background
) to use inherit
in case --color
is not defined.
This may solve the issue but in our case it won't be enough since --color
is always defined at :root
level and will get inherited1 by the pseudo element thus we will never use the fallback value.
To fix this we can consider the initial
value in order to undefine our custom property and force the use of the fallback value. As described in the specification:
The initial value of a custom property is an empty value; that is, nothing at all. This initial value has a special interaction with the var() notation, which is explained in the section defining var().
and
To substitute a var() in a property’s value:
- If the custom property named by the first argument to the var()
function is animation-tainted, and the var() function is being used in
the animation property or one of its longhands, treat the custom
property as having its initial value for the rest of this algorithm.- If the value of the custom property named by the first argument to the
var() function is anything but the initial value, replace the var()
function by the value of the corresponding custom property. Otherwise,- if the var() function has a fallback value as its second argument,
replace the var() function by the fallback value. If there are any
var() references in the fallback, substitute them as well.- Otherwise, the property containing the var() function is invalid at
computed-value time
Our code will then look like this:
: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);}
<div class="box red" style="--color:initial;"></div><div class="box blue" style="--color:initial;"></div>
<div class="box" style="background:grey;--color:initial;"></div>
How to provide a fallback CSS-value within a custom element?
If the content of your custom element is encapsulated in a Shadow DOM, which is a recommended practice, you can use the :host
pseudo-class to define a default style.
Then if you define a global style for your custom element it will override the one defined with :host
.
customElements.define( 'custom-icon', class extends HTMLElement { constructor() { super() let size = 100 this.attachShadow( { mode: 'open' } ) .innerHTML = ` <style> :host { display: inline-block ; height: ${size}px ; width: ${size}px ; background-color: firebrick ; color: white; } </style> <slot></slot>` }} )
custom-icon#i1 { --icon-size: 50px; height: var(--icon-size); width: var(--icon-size);}
<custom-icon id="i1">sized</custom-icon><hr><custom-icon>default</custom-icon>
Why display property set to inherit with CSS variable doesn't work?
In such situation, inherit
is used as a value for the custom property and will not be evaluted to the inherit
value using var()
.
Here is a basic example to understand the issue:
.box { --c:inherit; color:var(--c,red);}
<div> <div class="box">I am a text</div></div><div style="--c:blue"> <div class="box">I am a text</div></div>
Related Topics
CSS Opacity and Child Elements
Hover on Child Without Hover Effect on Parent
CSS Blur on Background Image But Not on Content
How to Overwrite Angular 2 Material Styles
How to Remove Extra Margin Space Generated by Inline Blocks
Why Doesn't [CSS Feature] Work in [Browser] But Works in Others
Animated Cursor Support in Web Applications
Are Flex Items Block Level Element
CSS Text Justify with Letter Spacing
Faster Way to Develop and Test Print Stylesheets (Avoid Print Preview Every Time)
I Cannot Change the Font Size of Package Explorer in Eclipse
Less Lists as a Mixin Argument(S)