๐Ÿ”ฐvariables

a.k.a css custom properties

๐Ÿ”ธ Valid Names โ”Š Cheatsheet โ”Š Key Points

:root {
    /* โญ๏ธ define css vars */
    --panel-bg: #fff;
    --panel-color: #000;
}

.panel {
    /* โญ๏ธ use css vars */
    color: var(--panel-color);
    background-color: var(--panel-bg);
}

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