Custom properties (--*): CSS variables
Quick Summary for --*
Property names that are prefixed with
--
, like --example-name
, represent custom properties that contain a value that can be used in other declarations using the var()
function.
Code Usage for --*
--somekeyword: left; --somecolor: #0000ff; --somecomplexvalue: 3px 6px rgb(20, 32, 54);
More Details for --*
Custom properties (--*): CSS variables
Property names that are prefixed with --
, like --example-name
, represent custom properties that contain a value that can be used in other declarations using the var()
function.
Custom properties are scoped to the element(s) they are declared on, and participate in the cascade: the value of such a custom property is that from the declaration decided by the cascading algorithm.
Initial value | see prose |
---|---|
Applies to | all elements |
Inherited | yes |
Computed value | as specified with variables substituted |
Animation type | discrete |
Syntax
--somekeyword: left; --somecolor: #0000ff; --somecomplexvalue: 3px 6px rgb(20, 32, 54);
<declaration-value>
This value matches any sequence of one or more tokens, so long as the sequence does not contain an disallowed token. It represents the entirety of what a valid declaration can have as its value.
Note: Custom property names are case sensitive — --my-color
will be treated as a separate custom property to --My-color
.
Formal syntax
<declaration-value>
Example
HTML
<p id="firstParagraph">This paragraph should have a blue background and yellow text.</p> <p id="secondParagraph">This paragraph should have a yellow background and blue text.</p> <div id="container"> <p id="thirdParagraph">This paragraph should have a green background and yellow text.</p> </div>
CSS
:root { --first-color: #16f; --second-color: #ff7; } #firstParagraph { background-color: var(--first-color); color: var(--second-color); } #secondParagraph { background-color: var(--second-color); color: var(--first-color); } #container { --first-color: #290; } #thirdParagraph { background-color: var(--first-color); color: var(--second-color); }
Result
Specifications
Specification |
---|
CSS Custom Properties for Cascading Variables Module Level 1 # defining-variables |
See also
Using CSS variables Thevar()
function Select your preferred language English (US)EspañolFrançais日本語Português (do Brasil)Русский中文 (简体) Change language
Posted in
12:22 am, February 22, 2022
Add Comment
Other Items in CSS
rgb()
rgba()
:right
right
@right-bottom
:root
rotate
rotate()
rotate3d()
rotateX()
rotateY()
rotateZ()
row-gap
ruby-align
ruby-position
saturate()
scale
scale()
scale3d()
scaleX()
scaleY()
scaleZ()
:scope
scroll-behavior
scroll-margin
scroll-margin-block
scroll-margin-block-end
scroll-margin-block-start
scroll-margin-bottom
scroll-margin-inline
scroll-margin-inline-end
scroll-margin-inline-start
scroll-margin-left
scroll-margin-right
scroll-margin-top
scroll-padding
scroll-padding-block
scroll-padding-block-end
scroll-padding-block-start
scroll-padding-bottom
scroll-padding-inline
scroll-padding-inline-end
scroll-padding-inline-start
scroll-padding-left
scroll-padding-right
scroll-padding-top
scroll-snap-align
scroll-snap-stop
scroll-snap-type
@scroll-timeline