Code has been added to clipboard!

CSS Variables: Learn to Define Custom Properties

Reading time 2 min
Published Oct 1, 2019
Updated Oct 2, 2019

TL;DR — CSS variables are custom entities that you can define and reuse in your document instead of repeating the same values.

What's a variable in CSS?

Just like you can define your own functions in JavaScript or PHP, you can create custom CSS properties, also known as CSS variables. They contain particular values that you would otherwise need to repeat multiple times in a document (e.g., a specific color).

Using variables in CSS not only saves you time but also simplifies updating the page, as you only need to change the value in one place instead of all across the document.

How to create a CSS variable

To define a variable in CSS, you need to write two dashes (--) in front of its actual name and specify a particular value:

--name: value;

In the example below, we create a custom CSS color variable with a specific color defined in a HEX value:

Example
p {
  --main-text-color: #036627;
}

We recommend using semantic names instead of random ones (e.g., --main-text-color is better than --color1). This will make it easier to read and update the code later.

Using variables in CSS code

To reuse the value you specified in the CSS variable, you need to use the var() function with the variable's name as the value:

Example
p {
  color: var(--main-text-color);
}

CSS variables: useful tips

  • When using the var() function, you can also include a specific value as a fallback. Write it after the variable name and separate the two using a comma.
  • Keep in mind that CSS variables are case-sensitive!
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index