{"id":10,"date":"2026-03-06T14:36:57","date_gmt":"2026-03-06T14:36:57","guid":{"rendered":"https:\/\/halfyolanda.com\/blog\/?p=10"},"modified":"2026-03-06T15:30:11","modified_gmt":"2026-03-06T15:30:11","slug":"css-custom-properties","status":"publish","type":"post","link":"https:\/\/halfyolanda.com\/blog\/index.php\/2026\/03\/06\/css-custom-properties\/","title":{"rendered":"CSS: Custom Properties"},"content":{"rendered":"\n<p class=\"has-vivid-cyan-blue-color has-text-color has-link-color has-larger-font-size wp-elements-45dc0e66f2fc1cd7330806b78ccec73a\" style=\"font-style:normal;font-weight:400\"><strong>What They Are and Why You Should Be Using Them<\/strong><\/p>\n\n\n\n<p style=\"font-style:normal;font-weight:300\">Ever found yourself searching through thousands of lines of CSS just to update a font colour you&#8217;ve changed your mind about? CSS Custom Properties were made for you. In this post, we&#8217;ll go through what they are, how they work, and when to use them.<\/p>\n\n\n\n<p>CSS Custom Properties were first introduced in 2012 and became fully supported across major browsers by 2017. They allow developers to define reusable values directly in their CSS \u2014 think of it as a shared bookshelf that your whole stylesheet can access. No build tools or preprocessors needed. Instead of searching through lines of CSS for a colour or spacing value, you define it once and update it in a single place. This keeps your stylesheets cleaner, consistent, and easier to maintain. Custom Properties are particularly useful when it comes to themes, media queries, and dynamic styling with JavaScript.<\/p>\n\n\n\n<p class=\"has-accent-color has-text-color has-link-color has-large-font-size wp-elements-af4221c0871aae4b861b3fd20ee44e26\"><strong>How do we use CSS Custom Properties<\/strong><\/p>\n\n\n\n<p>The first step when it comes to CSS Custom Properties is to define your values. In common practice, it is best to define them under the<mark style=\"background-color:rgba(0, 0, 0, 0);color:#f78da7\" class=\"has-inline-color\"> <\/mark><strong><code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">:root<\/mark><\/code> <\/strong>selector at the top of your stylesheet. This allows your custom properties to be accessed globally throughout your CSS. To define a custom property, you use<mark style=\"background-color:rgba(0, 0, 0, 0);color:#00d084\" class=\"has-inline-color\"> <\/mark><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code>--<\/code> <\/mark><\/strong>followed by the name of your variable. Below is an example of how it would appear:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"876\" height=\"302\" src=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.46.50-PM.png\" alt=\"\" class=\"wp-image-12\" style=\"aspect-ratio:2.900618122434693;width:400px;height:auto\" srcset=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.46.50-PM.png 876w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.46.50-PM-300x103.png 300w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.46.50-PM-768x265.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><\/figure>\n\n\n\n<p>\u00a0<br>After you&#8217;ve defined your custom properties, you can apply them to any CSS property by placing them inside <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">var()<\/mark><\/strong><\/code>. That way, instead of updating the heading size in multiple places, you only need to update it once in the<code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">:root<\/mark><\/strong><\/code> selector \u2014 any element using that custom property, like <mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code><strong>h1<\/strong><\/code> or <code><strong>h2<\/strong><\/code><\/mark>, will automatically reflect the change. Custom Properties also allow developers to give their values meaningful, descriptive names, making the code easier to read and maintain.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"818\" height=\"1024\" src=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.45.34-PM-818x1024.png\" alt=\"\" class=\"wp-image-11\" style=\"aspect-ratio:0.7988301583514472;width:387px;height:auto\" srcset=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.45.34-PM-818x1024.png 818w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.45.34-PM-240x300.png 240w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.45.34-PM-768x962.png 768w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.45.34-PM.png 1006w\" sizes=\"auto, (max-width: 818px) 100vw, 818px\" \/><\/figure>\n\n\n\n<p class=\"has-accent-color has-text-color has-link-color has-large-font-size wp-elements-483b80041d3da9754c512bdd6bf4131a\"><strong>Key Use Cases<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li class=\"has-secondary-color has-text-color has-link-color wp-elements-74e76287a771333ea4c4b6a3c2bfdf25\" style=\"font-size:31px\"><strong>Theming<\/strong><\/li>\n<\/ol>\n\n\n\n<ol style=\"font-style:normal;font-weight:600\" class=\"wp-block-list has-accent-color has-text-color has-link-color has-normal-font-size wp-elements-face00472a10b7cb2e9c8df15398d7f3\"><\/ol>\n\n\n\n<p>One of the most popular uses of CSS Custom Properties is theming. Before Custom Properties, switching themes meant maintaining an entirely separate stylesheet and loading it in on demand, which was messy and hard to manage. Now, you just define your colours once and override them when needed. You would start by defining your custom properties for your default theme under<strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code>:root<\/code>.<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\"> <\/mark><\/strong>Then you would redefine those variables under<strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code>[data-theme=\u2019dark\u2019]<\/code>.<\/mark><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\"> <\/mark><\/strong>That way all the elements that carry those custom properties will update automatically when the dark theme is active.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"850\" height=\"922\" src=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.56.39-PM.png\" alt=\"\" class=\"wp-image-13\" style=\"aspect-ratio:0.921914329335423;width:405px;height:auto\" srcset=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.56.39-PM.png 850w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.56.39-PM-277x300.png 277w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-12.56.39-PM-768x833.png 768w\" sizes=\"auto, (max-width: 850px) 100vw, 850px\" \/><\/figure>\n\n\n\n<p>To get this working, a small piece of JavaScript toggles the <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">data-theme<\/mark><\/strong><\/code> attribute on your <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">&lt;html><\/mark><\/strong><\/code> tag between <mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code><strong>\"light\"<\/strong><\/code> <\/mark>and <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">\"dark\"<\/mark><\/strong><\/code>. When it switches the browser will automatically re-read the custom properties and apply the changes. All the elements will pick up the new values instantly.<\/p>\n\n\n\n<p class=\"has-secondary-color has-text-color has-link-color wp-elements-4bdc773f9533f94f0a135e1130f50b9e\" style=\"font-size:31px\"><strong>2. Responsive Design<\/strong><\/p>\n\n\n\n<p>Another great use case for CSS Custom Properties is responsive design. I remember the first responsive design project I worked on and how tedious it was because I didn&#8217;t use CSS Custom Properties. Every time I changed a colour or size, I had to manually go through a stylesheet of nearly 900 lines and change every single affected instance. Custom Properties completely eliminate that problem. You just redefine the value once inside a media query and everything updates automatically.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"840\" height=\"1024\" src=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-1.41.57-PM-840x1024.png\" alt=\"\" class=\"wp-image-14\" style=\"aspect-ratio:0.8203151579421697;width:425px;height:auto\" srcset=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-1.41.57-PM-840x1024.png 840w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-1.41.57-PM-246x300.png 246w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-1.41.57-PM-768x937.png 768w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-1.41.57-PM.png 884w\" sizes=\"auto, (max-width: 840px) 100vw, 840px\" \/><\/figure>\n\n\n\n<p class=\"has-primary-color has-text-color has-link-color wp-elements-99d4524f527748dc55aca559dbc29b58\">The values defined under<mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><strong><code><code><strong>:root<\/strong><\/code><\/code><\/strong> <\/mark>act as your default screen size. The media query then redefines those same properties with smaller or larger values that suit your site&#8217;s needs. Any element using <code><strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">var(--font-size)<\/mark><\/strong><\/code> or <mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code><strong><code><strong>var(--spacing)<\/strong><\/code><\/strong><\/code> <\/mark>will automatically adjust accordingly, meaning you no longer have to scan through hundreds of lines to make changes.<\/p>\n\n\n\n<p class=\"has-secondary-color has-text-color has-link-color wp-elements-29d37da31b692295711203f9ad3a11a1\" style=\"font-size:31px\"><strong>3. JavaScript Integration<\/strong><\/p>\n\n\n\n<p>The great thing about CSS Custom Properties is that they can also be connected to your JavaScript. This is actually how the dark and light theme switching would work. You would add a button to your site that the user clicks on. When the user clicks that button it switches the<code><strong><code><code><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\"> <\/mark><\/strong><\/code><\/code><\/strong><\/code><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code><strong><code><code><strong>data-theme<\/strong><\/code><\/code><\/strong><\/code> <\/mark>attribute on the <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\"><code><strong><code><code><strong>&lt;html><\/strong><\/code><\/code><\/strong><\/code> <\/mark><\/strong>tag and the browser will update as needed.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"454\" src=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM-1024x454.png\" alt=\"\" class=\"wp-image-19\" srcset=\"https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM-1024x454.png 1024w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM-300x133.png 300w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM-768x341.png 768w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM-1200x533.png 1200w, https:\/\/halfyolanda.com\/blog\/wp-content\/uploads\/2026\/03\/Screenshot-2026-03-06-at-2.07.57-PM.png 1478w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>This is just one example of how JavaScript and CSS Custom Properties can work together. You can also use JavaScript to update any custom property in real time. Things like changing colours based on a slider, adjusting font sizes based on user preferences, or even driving animations based on mouse movement.<\/p>\n\n\n\n<p class=\"has-accent-color has-text-color has-link-color wp-elements-b522c8b42b19feb9f6f9c9dd73b0bf65\"><strong>A few things to keep in mind<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use fallback values when defining your variables. That way if a variable isn&#8217;t properly set it has something to fall back on <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">color: var(&#8211;primary-color, #3b82f6);.<\/mark><\/strong><\/li>\n\n\n\n<li>Name your variables clearly. It&#8217;s better to be descriptive with your naming conventions, it helps keep your stylesheet functional and understandable. <\/li>\n\n\n\n<li>Define your custom properties at the top under the <strong><mark style=\"background-color:rgba(0, 0, 0, 0);color:#0693e3\" class=\"has-inline-color\">:root <\/mark><\/strong>selector if you want the whole stylesheet to access them. Alternatively, you can define them inside a specific selector, but keep in mind that only that element and its children will have access to them.<\/li>\n<\/ul>\n\n\n\n<p><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-accent-color\">Wrapping Up!<\/mark><\/strong><\/p>\n\n\n\n<p>CSS Custom Properties are a small change but make a big difference to your CSS. They help keep your code cleaner, consistent and easier to update! They are fully supported by all modern web browsers, so you can start using them today!<\/p>\n\n\n\n<p>If you&#8217;d like to explore further, here are some resources that helped me understand CSS Custom Properties:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/medium.com\/@kenoyekitoye\/a-guide-to-css-custom-properties-446b48086d08\">A Guide to CSS Custom Properties \u2014 Medium<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/CSS\/Guides\/Cascading_variables\/Using_custom_properties\">Using CSS Custom Properties \u2014 MDN Web Docs<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/css-tricks.com\/a-complete-guide-to-custom-properties\/\">A Complete Guide to Custom Properties \u2014 CSS-Tricks<\/a><\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What They Are and Why You Should Be Using Them Ever found yourself searching through thousands of lines of CSS just to update a font colour you&#8217;ve changed your mind about? CSS Custom Properties were made for you. In this post, we&#8217;ll go through what they are, how they work, and when to use them. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-content-management"],"_links":{"self":[{"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/10","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=10"}],"version-history":[{"count":7,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"predecessor-version":[{"id":37,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions\/37"}],"wp:attachment":[{"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/halfyolanda.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}