Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

CSS var() Function

CSS var() Function

CSS Functions CSS Functions

Example

Define a property named "--main-bg-color", then call it with the var() function:

:root

{

--

main-bg-color:

coral

;

}

#div1

{

background-color:

var

(--

main-bg-color

)

;

}

#div2

{

background-color:

var

(--

main-bg-color

)

;

}

Try it »


Definition and usage

The var() function is used to insert custom property values; it is useful when a value is used in multiple places.

Supported version: CSS3


Browser support

The numbers in the table indicate the first browser version that supports the function.

function
var() 49.0 15.0 31.0 9.1 36.0

CSS Syntax



var(custom-property-name, value)

Value Description
custom-property-name Required. The name of the custom property, which must start with --.
value Optional. A fallback value, used when the property does not exist.

More examples

Example

Use the var() function to call multiple custom values:

:root

{

--

main-bg-color:

coral

; --

main-txt-color:

blue

; --

main-padding:

15

px

;

}

#div1

{

background-color:

var

(--

main-bg-color

)

;

color:

var

(--

main-txt-color

)

;

padding:

var

(--

main-padding

)

;

}

#div2

{

background-color:

var

(--

main-bg-color

)

;

color:

var

(--

main-txt-color

)

;

padding:

var

(--

main-padding

)

;

}

Try it »


CSS Functions CSS Functions