CSS Variables

a.k.a css custom properties

🔸 Valid NamesCheatsheetKey Points

Valid Names

⭐️ valid CSS variable names ( begin with two dashes "--" ❗️):

/* ✅ valid names */
--spacing: var(--line_height);
--spacing_1: calc(var(--spacing) * var(--scale));
--spacing_2: calc(var(--spacing_1) * var(--scale));
--spacing_-1: calc(var(--spacing) / var(--scale));

/* ❌ not valid names */
--spacing_+1:    

Cheatcheet

:root { 
    /* ⭐️ CSS variable */
    --main-font: Helvetica, Arial, sans-serif; 
}

p {
    /* ⭐️ use var() to reference the variable */
    font-family: var(--main-font); 
}

key points

  • The custom properties behave as a sort of scoped variable because the values are inherited by descendant elements.

redefine css vars

Last updated