{"id":22240,"date":"2022-11-01T06:49:58","date_gmt":"2022-11-01T13:49:58","guid":{"rendered":"https:\/\/coderpad.io\/?p=22240"},"modified":"2023-06-05T13:56:50","modified_gmt":"2023-06-05T20:56:50","slug":"what-front-end-developers-need-to-know-about-css-variables","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/what-front-end-developers-need-to-know-about-css-variables\/","title":{"rendered":"What Front-End Developers Need to Know about CSS Variables"},"content":{"rendered":"\n<p>CSS variables (also known as custom properties) enable you to declare reusable and dynamic CSS values.<\/p>\n\n\n\n<p>For a long time, a common challenge most web developers faced when working with CSS was the tendency to declare the same styles over and over again throughout the stylesheet. For example, on a website with a red color scheme, you might repeatedly declare the style <code>color: red;<\/code> throughout your stylesheets.<\/p>\n\n\n\n<p>This can lead to issues in the maintainability of your CSS styles, such as having to edit all instances of the declaration <code>color: red;<\/code> in your stylesheets when the color scheme is to be changed to purple. Enter CSS variables.<\/p>\n\n\n\n<p>CSS variables are author-defined property-value pairs, author-defined means they are not in the CSS specification but are defined by you, the web developer.<\/p>\n\n\n\n<p>They provide a way to define a CSS declaration once and then reuse that declaration throughout the stylesheet. This enables you to write more dynamic CSS and provides a more efficient and clean way of writing CSS.<\/p>\n\n\n\n<p>In this article, we&#8217;ll talk about CSS variables, what they are, how they work, and what you need to know to enable you to create more dynamic and reusable styles.<\/p>\n\n\n\n<p>Let&#8217;s dive in!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a CSS variable?<\/h2>\n\n\n\n<p>In a programming language like JavaScript, a variable is defined as a way of storing information in the program\u2014a storage unit. Each variable acts like a container, where you pass a name that acts as the label on the container and assigns that name to a value that represents the contents in the container. Once a variable is assigned a value, you can then use the name of the variable to access its value anywhere down in the program. CSS variables operate in such a manner.<\/p>\n\n\n\n<p>To initialize a CSS variable, you define a property with a custom name of your choice and assign any valid CSS value to it.<\/p>\n\n\n\n<p>Syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">element<\/span> {\n  <span class=\"hljs-attribute\">--variable-name<\/span>: css-value;\t\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>CSS variables, like every CSS property, must be defined within the style block of a CSS selector.&nbsp;&nbsp;<\/p>\n\n\n\n<p>Here, <code>element<\/code> refers to any valid HTML element, the curly braces <code>{}<\/code> refers to the declaration block of the element, <code>--variable-name<\/code> and <code>css-value<\/code> refer to the name of the CSS variable and the value you assign to it.<\/p>\n\n\n\n<p>Notice the double hyphens <code>--<\/code> prefixed to the variable&#8217;s name. To define a CSS variable, you must prefix the double hyphens <code>--<\/code> to the variable&#8217;s name (known as the custom property notation). This ensures you avoid accidental name overlapping of reserved CSS properties or other properties starting with a single hyphen &#8211; like <code>-web-kit.<\/code><\/p>\n\n\n\n<p>Now, you can reuse the values in your CSS variables as the value in other properties across your CSS styles by passing the variable name to the CSS <code>var()<\/code> function.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">child-element<\/span> {\n  <span class=\"hljs-attribute\">property<\/span>: <span class=\"hljs-built_in\">var<\/span>(--variable-name);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>var()<\/code> function parses the variable and returns its value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Scope and the CSS Cascade<\/h3>\n\n\n\n<p>The CSS selector that defines a CSS variable determines how it <a href=\"https:\/\/wattenberger.com\/blog\/css-cascade\" target=\"_blank\" rel=\"noopener\">cascades down through the stylesheet<\/a> and the child elements of that HTML element.<\/p>\n\n\n\n<p>In CSS, the cascade is a mechanism that determines how accessible a CSS style is to other parts of a stylesheet. It answers the questions: &#8220;from where can certain CSS styles be accessed?&#8221;, &#8220;from where can it not be accessed?&#8221;, &#8220;what can access it?&#8221; and &#8220;what can&#8217;t?&#8221;<\/p>\n\n\n\n<p>Each HTML element you reference in CSS through CSS selectors creates its scope: the space\/environment where the CSS properties and styles it defines can be accessed, which also determines how it accesses other CSS properties and styles via a process called cascading.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s look at an example to understand this better.<\/p>\n\n\n\n<p>When you define an HTML element within another element, like so:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">outer-element<\/span>&gt;<\/span> \n<span class=\"hljs-comment\">&lt;!-- also known as the \u201cparent element\u201d --&gt;<\/span>    \n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">inner-element<\/span>&gt;<\/span>\n      <span class=\"hljs-comment\">&lt;!-- also known as the \u201cchild element\u201d --&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">innner-element<\/span>&gt;<\/span>    \n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">outer-element<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The inner element will be able to inherit and access some of the CSS styles defined in that of the outer element CSS ruleset and that of its parent elements. <\/p>\n\n\n\n<p>However, this doesn&#8217;t work in a backward-compatible manner. That is, the outer element will never have access to the CSS styles defined in the CSS ruleset of the inner element because CSS styles only cascade downwards in the stylesheet in a parent-to-child direction and never in the opposite direction.&nbsp;<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f This behavior also applies to variables in other programming languages and is called <strong>scoping<\/strong>.<\/p>\n<\/blockquote>\n\n\n\n<p>As a result, it is a common practice to define CSS variables in the <code>:root<\/code> pseudo-class.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\" target=\"_blank\" rel=\"noopener\">MDN<\/a> defines CSS pseudo-class as a keyword added to a selector that specifies the selected element(s) state. For example, the pseudo-class <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/:hover\" target=\"_blank\" rel=\"noopener\"><code>:hover<\/code><\/a>, signifies that a user&#8217;s pointer is hovering over an element.<\/p>\n<\/blockquote>\n\n\n\n<p>The <code>:root<\/code> pseudo-class represents the element at the document&#8217;s root in the <a href=\"https:\/\/unicorn-utterances.com\/posts\/understanding-the-dom\" target=\"_blank\" rel=\"noopener\">DOM<\/a>. In HTML, this is usually the <code>&lt;html&gt;<\/code> element.<\/p>\n\n\n\n<p>This means the syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span> {\n  <span class=\"hljs-attribute\">--font-size<\/span>: <span class=\"hljs-number\">16px<\/span>;\n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Is the same as writing:&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">html<\/span> {\n  <span class=\"hljs-attribute\">--font-size<\/span>: <span class=\"hljs-number\">16px<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The difference is that <code>:root<\/code> has a much higher <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Specificity\" target=\"_blank\" rel=\"noopener\">specificity<\/a>. It&#8217;s simply a way of setting CSS variables on a selector that&#8217;s as high up in the document tree as possible. This way, they are made globally accessible across your CSS file and cascade to every element on the page.<\/p>\n\n\n\n<p>However, this doesn&#8217;t always have to be the case, as you might want to limit the scope of your CSS variables.<\/p>\n\n\n\n<p>In such cases, you can specify CSS variables on the style block of specific HTML elements to which you wish them to be accessible.&nbsp;<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">nav<\/span> {\n  <span class=\"hljs-attribute\">--font-size<\/span>: <span class=\"hljs-number\">10px<\/span>; \n<span class=\"hljs-comment\">\/* equals to two times the value of the --font-size variable - 20px *\/<\/span>\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-built_in\">calc<\/span>(var(--font-size * <span class=\"hljs-number\">2<\/span>)) \n}\n\n<span class=\"hljs-selector-tag\">nav<\/span> <span class=\"hljs-selector-tag\">a<\/span> {\n<span class=\"hljs-comment\">\/* equals to the value of the --font-size variable - 10px *\/<\/span>\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-built_in\">var<\/span>(--font-size) \n}\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the CSS variable <code>--font-size<\/code> is defined on the navigation element \u2014 <code>nav<\/code> and will only be accessible within it and its child elements.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Assigning CSS variables to other CSS variables<\/h3>\n\n\n\n<p>You can pass a CSS variable as the value for another CSS variable using the <code>var()<\/code> function. The CSS variable will then be parsed by the <code>var()<\/code> function to represent its value.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php shcb-wrap-lines\">:root {\n  --red: <span class=\"hljs-comment\">#a24e34;<\/span>\n  --green: <span class=\"hljs-comment\">#01f3e6;<\/span>\n  --yellow: <span class=\"hljs-comment\">#f0e765;<\/span>\n\n  --danger: <span class=\"hljs-keyword\">var<\/span>(--red); <span class=\"hljs-comment\">\/\/ will transpile back to the value of the --red variable.<\/span>\n  --success: <span class=\"hljs-keyword\">var<\/span>(--green);\n  --warning: <span class=\"hljs-keyword\">var<\/span>(--yellow);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The added benefit here is readability since you now communicate the intent or role of the variable. This can help you write variable names that are more readable, descriptive, and understandable.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Fallback values<\/h3>\n\n\n\n<p>When using CSS variables, you might reference a variable that can not be accessed in that part of the stylesheet due to the CSS cascade or was just not defined. Using the <code>var<\/code> function, you can specify fallback values that will be used in place of a CSS variable if it is inaccessible, contains invalid values, or is undefined.<\/p>\n\n\n\n<p>The <code>var()<\/code> function takes two arguments:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\">var( <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">css-variable-name<\/span>&gt;<\/span>, <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">declaration-value<\/span>&gt;<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Where:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>&lt;css-variable-name<\/code>&gt; is the name of a CSS variable to parse.<\/li>\n\n\n\n<li><code>&lt;declaration-value&gt;<\/code> is the fallback values used when the referenced CSS variable is inaccessible. It is an optional argument.<\/li>\n<\/ul>\n\n\n\n<p>Let&#8217;s look at an example of specifying fallback values in the <code>var<\/code> function.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-built_in\">rgb<\/span>(<span class=\"hljs-number\">111<\/span>, <span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">146<\/span>);\n}\n\n<span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-built_in\">var<\/span>(--background-colour, royalpurple, rebeccapurple, purple); \n <span class=\"hljs-comment\">\/* There is no --background-colour, hence the fallback values will be used *\/<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, we define the CSS variable <code>background-color<\/code> in the <code>:root<\/code> pseudo-class. But, when using it in the <code>body<\/code> tag style block, it is written as <code>background-colour<\/code> in British English and not <code>background-color<\/code> as defined in the <code>:root<\/code> pseudo-class. This will cause the variable to be undefined, and the browser to use the first fallback values.<\/p>\n\n\n\n<p>While the <code>var()<\/code> function only accepts two arguments, in the code block above, it receives four comma-separated values. This is because everything after the first comma is regarded as the second parameter\u2014fallback values. This means you can specify multiple fallback values, and if, for some reason, the first fallback value can&#8217;t be used, the next value would kick in, and so on.<\/p>\n\n\n\n<p>However, this will not work when specifying other CSS variables as fallback values.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.element<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--default-color, --backup-color, purple);\n  <span class=\"hljs-comment\">\/* will be Invalid: \"--backup-color, purple\" *\/<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The CSS style above will be invalid because though you can specify CSS variables as fallback values, they still need to be nested in a <code>var()<\/code> function for their values to be parsed and returned.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.element<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--default-color, var(--backup-color, purple));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above is the right syntax to provide a CSS variable as a fallback. Here, the color purple will only be used if <code>--default-color<\/code> and <code>--backup-color<\/code> are invalid or undefined.<\/p>\n\n\n\n<p>Another use case where fallback values are useful is when the value of a CSS variable is not a valid value for the property to which it is supplied.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span> { \n  <span class=\"hljs-attribute\">--text-red<\/span>: <span class=\"hljs-number\">14px<\/span>; \n} \n\n<span class=\"hljs-selector-tag\">body<\/span> { \n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--text-red, red); \n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this snippet, the CSS variable <code>--text-red<\/code> is defined with a value of <code>14px<\/code>. This is an invalid value when passed to the <code>color<\/code> property. Hence. The fallback value of red will be used.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 Note that if the values passed to the second parameter are not <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/CSS_Values_and_Units\" target=\"_blank\" rel=\"noopener\">supported by the browser<\/a> or are invalid, the fallback will not be applied<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">What you can do with CSS variables<\/h2>\n\n\n\n<p>CSS variables are an amazing CSS feature, and there are no boundaries to what can be achieved using this tool. Let&#8217;s go over some of the things you can accomplish with CSS variables.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Assigning valid data types to CSS variables<\/h3>\n\n\n\n<p>Every CSS property has a set of values it can accept. For example, the CSS property <code>color<\/code> accepts only keyword values that describe a color like <strong>green<\/strong> or a numerical color value. These values are categorized into what are called data types.<\/p>\n\n\n\n<p>CSS data types are a set of acceptable values and units you can assign to CSS properties\u2014from keyword values to length units to CSS functions. Most CSS properties accept only a few data types as their value. However, there are no limits to what you can assign as the value of a CSS variable. They accept all valid data types.<\/p>\n\n\n\n<p>In this section, you&#8217;ll look at most of these data types you can assign to CSS variables. You can get a more comprehensive reference for all CSS data types in the <a href=\"https:\/\/www.w3.org\/TR\/css3-values\/#value-defs\" target=\"_blank\" rel=\"noopener\">CSS specification<\/a>. Let&#8217;s dive in.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Colors<\/h4>\n\n\n\n<p>CSS variables can be assigned CSS color keyword values and numerical color values such as hexadecimal color codes. This opens up a ton of flexibility in creating color themes in your stylesheets.<\/p>\n\n\n\n<p>Say you\u2019re using a CSS color function, like the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/color_value\/rgba\" target=\"_blank\" rel=\"noopener\"><code>rgba()<\/code><\/a> or <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/color_value\/hsl\" target=\"_blank\" rel=\"noopener\"><code>hsl()<\/code><\/a> function notations. You can split the component values of these functions into independent CSS variables, and easily adjust each component&#8217;s value where you want.<\/p>\n\n\n\n<p>That opens up many possibilities, like changing the alpha value for a specific use case or perhaps creating color themes.<\/p>\n\n\n\n<p>For example, when working on the background color of a button with the <code>hsl<\/code> color function. By storing each component value of the function into CSS variables, you can update specific parts of the <code>hsl()<\/code> function when the button is in focus, hovered or disabled, without explicitly updating the <code>background<\/code> property of the button repeatedly in our stylesheet.<\/p>\n\n\n\n<p>Take a look at the snippet below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.btn<\/span> {\n  <span class=\"hljs-attribute\">--hue<\/span>: <span class=\"hljs-number\">216<\/span>;\n  <span class=\"hljs-attribute\">--saturation<\/span>: <span class=\"hljs-number\">85%<\/span>;\n  <span class=\"hljs-attribute\">--lightness<\/span>: <span class=\"hljs-number\">28%<\/span>;\n  <span class=\"hljs-attribute\">--a<\/span>: <span class=\"hljs-number\">1<\/span>;\n\n  <span class=\"hljs-attribute\">background<\/span>: <span class=\"hljs-built_in\">hsl<\/span>(var(--hue), <span class=\"hljs-built_in\">var<\/span>(--saturation), <span class=\"hljs-built_in\">var<\/span>(--lightness), <span class=\"hljs-built_in\">var<\/span>(--a));\n}\n\n<span class=\"hljs-selector-class\">.btn<\/span><span class=\"hljs-selector-pseudo\">:hover<\/span> {\n  <span class=\"hljs-comment\">\/* Change the lightness on hover *\/<\/span>\n  <span class=\"hljs-attribute\">--l<\/span>: <span class=\"hljs-number\">45%<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.btn<\/span><span class=\"hljs-selector-pseudo\">:focus<\/span> {\n  <span class=\"hljs-comment\">\/* Change the saturation on focus *\/<\/span>\n  <span class=\"hljs-attribute\">--s<\/span>: <span class=\"hljs-number\">50%<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.btn<\/span><span class=\"hljs-selector-attr\">&#91;disabled]<\/span> {\n  <span class=\"hljs-comment\">\/* Make the button look disabled *\/<\/span>\n  <span class=\"hljs-attribute\">--s<\/span>: <span class=\"hljs-number\">0%<\/span>;\n  <span class=\"hljs-attribute\">--a<\/span>: <span class=\"hljs-number\">0.8<\/span>;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, by storing each value of the <code>hsl()<\/code> function into CSS variables, you can create a theme for the hover, focus, and disabled state of a button by simply tweaking the values of each variable down in the stylesheet. Rather than re-updating the <code>background<\/code> property of the button, we overrode the specific <code>hsl<\/code> values in the correct context they are needed. That&#8217;s some cool stuff there.<\/p>\n\n\n\n<p>Check out the demo here:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed \"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233586&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Numeric units<\/strong><\/h4>\n\n\n\n<p>There are various numeric data types in CSS, such as:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/integer\" target=\"_blank\" rel=\"noopener\">Integers<\/a>\u2014 whole numbers such as <code>100<\/code> or <code>-45.<\/code><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/number\" target=\"_blank\" rel=\"noopener\">Numbers<\/a>\u2014 decimal digits that may or may not have a decimal point with a fractional component such as <code>0.255<\/code>, <code>128<\/code>, or <code>-1.2<\/code>.<\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/dimension\" target=\"_blank\" rel=\"noopener\">Dimensions<\/a>\u2014 decimal digits suffixed with a CSS length unit such as <code>10px<\/code>, <code>45deg<\/code>, <code>0.5s<\/code>.<\/li>\n\n\n\n<li>And <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/percentage\" target=\"_blank\" rel=\"noopener\">percentages<\/a>\u2014 numbers suffixed with the percentage sign (<code>%<\/code>) representing a fraction of some other value. For example, <code>50%<\/code>.<\/li>\n<\/ul>\n\n\n\n<p>They are all assignable as values to CSS variables.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">:root{\n--border-size: <span class=\"hljs-number\">2.5<\/span>px;\n--scale-value: <span class=\"hljs-number\">1.5<\/span>;\n}\n\nh1 {\n  <span class=\"hljs-attr\">border<\/span>: <span class=\"hljs-keyword\">var<\/span>(--border-size) solid black;;\n}\n\n<span class=\"hljs-attr\">h1<\/span>:hover {\n  <span class=\"hljs-attr\">transform<\/span>: scale(<span class=\"hljs-keyword\">var<\/span>(--scale-value));\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h4 class=\"wp-block-heading\">Strings<\/h4>\n\n\n\n<p>You can assign strings\u2014text characters enclosed in single or double quotes\u2014as values to certain CSS properties. For example, the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/content\" target=\"_blank\" rel=\"noopener\">content<\/a> <code>property,<\/code> when used on the <code>::before<\/code> and <code>::after<\/code> pseudo-elements, can accept a string value that&#8217;ll be inserted into the DOM. You can assign string values to CSS variables as well.&nbsp;<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed \"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233587&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>In the CoderPad sandbox above, the CSS variable <code>--status<\/code> is defined on a <code>ul<\/code> element with a string value of <code>\"New!\"<\/code>. The <code>--status<\/code> variable is then used with the CSS <code>content<\/code> property on the <code>::after<\/code> <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Learn\/CSS\/Building_blocks\/Selectors\/Pseudo-classes_and_pseudo-elements#what_is_a_pseudo-element\" target=\"_blank\" rel=\"noopener\">pseudo-element<\/a> of each <code>li<\/code> element in the list to insert additional decorative text. Due to the CSS cascade, we can alter the <code>--status<\/code> variable in the style block of each <code>li<\/code> element to insert a different text at its end.&nbsp;<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Multiple data types<\/h4>\n\n\n\n<p>Typically, you can only assign a single CSS value to a property. However, some CSS properties can accept multiple CSS values of different types. They are called shorthand properties.<\/p>\n\n\n\n<p>CSS shorthand properties let you set the values of multiple related CSS properties simultaneously in a single declaration. For example, the CSS property <code>font<\/code> is a shorthand property that accepts multiple CSS values that lets you specify the most common font-related properties like <code>font-style,<\/code> <code>font-weight,<\/code> etc. Similarly, you can assign multiple CSS values of different data types to a CSS variable.&nbsp;<\/p>\n\n\n\n<p>A good example would be the CSS <code>border<\/code> property. It accepts three values that specify the border&#8217;s width, style, and color.<\/p>\n\n\n\n<p>Now, instead of storing these values in different CSS variables:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span>{\n  <span class=\"hljs-attribute\">--border-width<\/span>: <span class=\"hljs-number\">1px<\/span>;\n  <span class=\"hljs-attribute\">--border-style<\/span>: solid;\n  <span class=\"hljs-attribute\">--border-color<\/span>: <span class=\"hljs-number\">#000<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.box<\/span>{\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-built_in\">var<\/span>() <span class=\"hljs-built_in\">var<\/span>() <span class=\"hljs-built_in\">var<\/span>();\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can store them all in a single CSS variable and pass it to the property.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span>{\n  <span class=\"hljs-attribute\">--border-box<\/span>: <span class=\"hljs-number\">1px<\/span> solid <span class=\"hljs-number\">#000<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.box<\/span>{\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-built_in\">var<\/span>(--border-box);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f Note the values of a CSS variable passed to a shorthand property must match the syntax of the values the shorthand property expects. Else, such values will be considered invalid for the property and will not be applied.<\/p>\n<\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Use CSS variables with <code>calc()<\/code><\/h3>\n\n\n\n<p>You can perform arithmetic operations on CSS variables using the <a href=\"https:\/\/coderpad.io\/blog\/development\/an-overview-of-five-css-math-functions\/#calc\"><code>calc()<\/code><\/a> function. A good use case would be when you need certain values to be in a specific ratio.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span> {\n  <span class=\"hljs-attribute\">--base-text<\/span>: <span class=\"hljs-number\">16px<\/span>;\n\n  <span class=\"hljs-comment\">\/*wil be 2 times the value of --base-text *\/<\/span>\n  <span class=\"hljs-attribute\">--big-text<\/span>: <span class=\"hljs-built_in\">calc<\/span>(var(var(--base-text) * <span class=\"hljs-number\">2<\/span>));\n\n<span class=\"hljs-comment\">\/* will be half the value of --base-text *\/<\/span>\n  <span class=\"hljs-attribute\">--mini-text<\/span>: <span class=\"hljs-built_in\">calc<\/span>(var(var(--base-text) \/ <span class=\"hljs-number\">2<\/span>));\n}\n\n<span class=\"hljs-selector-class\">.h1<\/span> {\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-built_in\">var<\/span>(--big-text);\n}\n\n<span class=\"hljs-selector-class\">.sub-text<\/span> {\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-built_in\">var<\/span>(--mini-text);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, we use the <code>calc()<\/code> function to ensure that the <code>--big-text<\/code> variable will always be twice the value of <code>--base-text<\/code> and <code>--mini-text<\/code> half it&#8217;s value, regardless of what <code>--base-text<\/code> is. This helps us avoid hardcoding such values from scratch.<\/p>\n\n\n\n<p>Another use case for using the <code>calc()<\/code> function with CSS variables would be to combine different CSS units to create values that are impossible to represent using traditional CSS units.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">--nav-height<\/span>: 8<span class=\"hljs-selector-tag\">rem<\/span>;\n<span class=\"hljs-selector-tag\">--main-height<\/span>: <span class=\"hljs-selector-tag\">calc<\/span>(100<span class=\"hljs-selector-tag\">vh<\/span> <span class=\"hljs-selector-tag\">-<\/span> <span class=\"hljs-selector-tag\">var<\/span>(<span class=\"hljs-selector-tag\">--nav-height<\/span>));;\n}\n\n<span class=\"hljs-selector-tag\">main<\/span> {\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-built_in\">var<\/span>(--main-height);\n}\n\n<span class=\"hljs-selector-tag\">nav<\/span> {\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-built_in\">var<\/span>(--nav-height);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we ensure the main section will take up most of the viewport&#8217;s height except for the height of the navigation bar by using the <code>calc()<\/code> function to subtract the navigation bar height, <code>8rem<\/code>, from the viewport height <code>100vh<\/code> in the <code>--main-height<\/code> variable.<\/p>\n\n\n\n<p>Now, no matter what the viewport height is, the <code>main<\/code> tag will always be <code>8rem<\/code> shorter than the full height of the screen.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Change CSS variables lower in the cascade<\/h3>\n\n\n\n<p>You can change the values of CSS variables lower in the cascade.<\/p>\n\n\n\n<p>For example, you can overwrite the value of a global variable defined in the <code>:root<\/code> pseudo-class in the stylebook of another element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-pseudo\">:root<\/span>{\n  <span class=\"hljs-attribute\">--base-font<\/span>: <span class=\"hljs-number\">16px<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.hero-container<\/span> {\n  <span class=\"hljs-attribute\">--base-font<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-built_in\">var<\/span>(--base-font)\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This way, the <code>--base-font<\/code> variable will be set to <code>16px<\/code> for all other elements in the DOM except for the element with the <code>hero-container<\/code> class and all its child elements, where it will be set to <code>20px<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use cases<\/h2>\n\n\n\n<p>There are numerous applications for using CSS variables in your stylesheet. Let&#8217;s go over some of these use cases.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Implement styles that are conditionally rendered<\/h3>\n\n\n\n<p>Whenever there&#8217;s a need to implement dynamic styles, i.e, styles that change based on the application&#8217;s state, you&#8217;d usually opt for a solution using JavaScript or JSX. With CSS variables, you can shift such tasks away from JavaScript and more towards CSS and declare conditionally rendered styles using CSS only.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s look at an example of implementing a dark-mode and light-mode color theme for a website based on the user&#8217;s user agent (operating system) theme preference using CSS only.<\/p>\n\n\n\n<p>If you weren&#8217;t using CSS variables, you&#8217;ll probably have a <code>.dark-theme<\/code> and <code>.light-theme<\/code> class that will hold the styles for each theme, then use JavaScript to conditionally apply the <code>.dark-theme<\/code> or <code>.light-theme<\/code> class to the <code>html<\/code> element based on the user&#8217;s color scheme.<\/p>\n\n\n\n<p>With CSS variables, we won&#8217;t need classes to hold the styles for each theme. We can merely use the <code>prefers-color-scheme<\/code> <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Media_Queries\/Using_media_queries#targeting_media_features\" target=\"_blank\" rel=\"noopener\">media query<\/a> to detect the user&#8217;s theme preference and store the colors for each theme in CSS variables defined in the <code>:root<\/code> pseudo-element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--background-color);\n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--text-color);\n}\n\n<span class=\"hljs-keyword\">@media<\/span> (<span class=\"hljs-attribute\">prefers-color-scheme:<\/span> light) {\n  <span class=\"hljs-selector-pseudo\">:root<\/span> {\n    <span class=\"hljs-attribute\">--background-color<\/span>: <span class=\"hljs-number\">#fff<\/span>;\n    <span class=\"hljs-attribute\">--text-color<\/span>: <span class=\"hljs-number\">#000<\/span>;\n  }\n}\n\n<span class=\"hljs-keyword\">@media<\/span> (<span class=\"hljs-attribute\">prefers-color-scheme:<\/span> dark) {\n  <span class=\"hljs-selector-pseudo\">:root<\/span> {\n    <span class=\"hljs-attribute\">--background-color<\/span>: <span class=\"hljs-number\">#000<\/span>;\n    <span class=\"hljs-attribute\">--text-color<\/span>: <span class=\"hljs-number\">#fff<\/span>;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This is possible because CSS variables cascade; hence variables declared in the <code>:root<\/code> which is the highest element in the DOM, can be accessed by all child elements.&nbsp;<\/p>\n\n\n\n<p>Here&#8217;s a CoderPad sandbox demo:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed \"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=233588&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">Access CSS variables in JavaScript<\/h3>\n\n\n\n<p>A selling point for using native CSS variables over variables in preprocessor languages like Sass, Less, or Stylus is that you can easily access, create and update CSS variables with JavaScript.<\/p>\n\n\n\n<p>CSS variables are scoped to the DOM and its elements; hence they can be accessed in JavaScript using the <code>getPropertyValue<\/code> and <code>setPropertyValue<\/code> browser APIs.<\/p>\n\n\n\n<p>The <code>getPropertyValue<\/code> method returns the value of a CSS property specified on an element.<\/p>\n\n\n\n<p>For example, the snippet below will return the value of the CSS property <code>--theme,<\/code> which is a CSS variable defined on the <code>:root<\/code> element.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> root = <span class=\"hljs-built_in\">document<\/span>.documentElement;\n\n<span class=\"hljs-keyword\">const<\/span> theme = root.style.getPropertyValue(\u201c--theme\u201d);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>setPropertyValue<\/code> method sets the value for a CSS property specified on an element or declares it if that property isn&#8217;t defined.<\/p>\n\n\n\n<p>For example, the snippet below sets the value of the <code>:root<\/code> element CSS variable <code>--base-color<\/code> to <code>#000<\/code> when the user clicks <code>dark-theme-btn<\/code> element. If the variable doesn&#8217;t exist, it will be declared with that value.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> dark-theme-btn = <span class=\"hljs-built_in\">document<\/span>.getElementByid(\u201cdark-theme-btn\u201d)\n\ndark-theme-btn.onclick = <span class=\"hljs-function\">(<span class=\"hljs-params\">e<\/span>) =&gt;<\/span> {\n  e.preventDefault()\n  <span class=\"hljs-built_in\">document<\/span>.documentElement.setPropertyValue(\u201c--base-color\u201d, \u201c#<span class=\"hljs-number\">000<\/span>\u201d)\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>CSS variables are dynamic. The browser will repaint the DOM with their updated values when they change (via JavaScript, or with a media query).<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed \"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/admin.coderpad.io\/sandbox?question_id=233589&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<p>In the code block above, the horizontal and vertical positions of the CoderPad logo are stored with CSS variables. We use JavaScript to set those variables&#8217; values to the mouse&#8217;s position as it moves along the screen.<\/p>\n\n\n\n<p>This doesn&#8217;t apply to variables in preprocessor languages like SCSS because they resolve to a value when compiled and stay at that value.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Try out CSS variables too!<\/h1>\n\n\n\n<p>To recap, CSS variables enable you to create dynamic and reusable styles. They are an alternative to using preprocessor variables but offer much more benefits, such as their dynamic nature to repaint the browser whenever they change, their integration with the cascade and the DOM\u2014something preprocessor variables will never be able to do, and the fact you can use them without a need to preprocess your CSS.<\/p>\n\n\n\n<p>CSS variables allow you to store values and reuse them throughout your stylesheet, providing an easy way to make changes in one place and see the result across the whole stylesheet. This makes editing and revising the sheet easier for you and other developers when they try their hand at your CSS styles. Trust me &#8211; it&#8217;s worth knowing!<\/p>\n\n\n\n<p>What are you waiting for? Try out CSS variables today!<\/p>\n\n\n\n<p><strong><em>This article was written by<\/em><\/strong> <a href=\"https:\/\/twitter.com\/Victor_codejs\" target=\"_blank\" rel=\"noopener\"><strong><em>Victor Ikechukwu<\/em><\/strong><\/a><strong><em>. Victor is a front-end developer and technical writer interested in the JAMstack. He enjoys breaking down developer concepts into clear, understandable bits for others.<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CSS variables allow you to store values and reuse them throughout your stylesheet, providing an easy way to make changes in one place and see the result across the whole stylesheet. Learn how to use CSS variables to improve your stylesheets in the article.<\/p>\n","protected":false},"author":1,"featured_media":22260,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[62],"keyword-cluster":[],"class_list":["post-22240","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=22240"}],"version-history":[{"count":30,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22240\/revisions"}],"predecessor-version":[{"id":32642,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/22240\/revisions\/32642"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/22260"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=22240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=22240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=22240"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=22240"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=22240"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=22240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}