The CSS Series: Part 11 - Custom Properties
Tags: css, html, 100daysofcode, design
Published: 2021-01-05 15:58:01

Once you pick a color palette for your website, you can imagine how often you'll write those color values in your stylesheet. You could have the same color listed as a property value in thirty or more places! Now imagine if your client or team suddenly needs to change your site's color palette? You'd have to go through the entire document and change each of those 30+ values!

One way to avoid this is to use CSS custom properties, also known as CSS variables. CSS custom properties are variables defined by CSS authors that contain specific values to be reused throughout a document. This tutorial will show you how to create and use CSS variable.

## Understanding CSS Custom Properties
Let's say our client thinks they'd like their website background to be gray. And all borders are black. Font colors should be white, they say. But you've worked with enough people to know that this will change a few times before finishing your project, so you decide to set up custom properties. You would create custom properties for each of these colors like so:

/ stylesheet.css
```css
:root {
    --background: gray;
    --border: black;
    --font: white;
    }
```

By declaring the CSS custom properties inside of the [root pseudo-class](https://developer.mozilla.org/en-US/docs/Web/CSS/:root), these custom properties are globally available throughout your entire project. Assigning a custom value to a div declaration means that the custom variable would only be available to div types. This is due to a process referred to as scoping, which you'll learn more about as you dive into a language like JavaScript. But for now, let's carry on with our website design stylesheet, inserting these custom properties into your project like so:

/ stylesheet.css
```css
:root {
    --background: gray;
    --border: black;
    --font: white;
    }

div .header {
    background-color: var(--background);
    border: 5px solid var(--border);
    color: var(--font);
    }
    
h1,
h2,
h3,
h4 {
    color: var(--font);
    }
    
body {
    background-color: var(--background);
    color: var(--font);
    }
```

Now your html is styled with the property values assigned to your custom properties of --background, --border, and --font. As you can see, if you needed to change the color palette of the entire website, all you would need to do is change the property value assigned to your custom property declarations inside of the root pseudo-class.

The benefits of CSS custom properties are not limited to colors and you're really only limited by your imagination.

You can catch up on the rest of the series at the links below:
[Part 1: An Intro to CSS](https://dev.to/vetswhocode/the-css-series-part-1-an-intro-to-css-2368)
[Part 2: How to Link a Stylesheet to Your index.html File](https://dev.to/vetswhocode/the-css-series-part-2-how-to-link-a-stylesheet-to-your-index-html-file-1d0e)
[Part 3: Selectors](https://dev.to/vetswhocode/the-css-series-part-3-selectors-49b8)
[Part 4: Properties](https://dev.to/vetswhocode/the-css-series-part-4-properties-17d7)
[Part 5: The Box Model](https://dev.to/vetswhocode/the-css-series-part-5-the-box-model-32i)
[Part 6: CSS Units](https://dev.to/vetswhocode/the-css-series-part-6-css-units-a02)
[Part 7: Flexbox](https://dev.to/vetswhocode/the-css-series-part-7-css-units-362i)
[Part 8: CSS Grid](https://dev.to/vetswhocode/the-css-series-part-8-css-grid-109m)
[Part 9: Display Property](https://dev.to/vetswhocode/the-css-series-part-9-display-property-h1h)
[Part 10: Specificity](https://dev.to/vetswhocode/the-css-series-part-10-specificity-4ge9)