Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Custom properties (--*): CSS variables

BaselineWidely available

Property names that are prefixed with--, like--example-name, representcustom properties that contain a value that can be used in other declarations using thevar() 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 valuesee prose
Applies toall elements
Inheritedyes
Computed valueas specified with variables substituted
Animation typediscrete

Syntax

css
--some-keyword: left;--some-color: #0000ff;--some-complex-value: 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 any 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.

Example

HTML

html
<p>  This paragraph should have a blue background and yellow text.</p><p>  This paragraph should have a yellow background and blue text.</p><div>  <p>    This paragraph should have a green background and yellow text.  </p></div>

CSS

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

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp