Using CSS Custom Properties To Optimize Styling Efficiency and Readability

United States News News

Using CSS Custom Properties To Optimize Styling Efficiency and Readability
United States Latest News,United States Headlines

Learn about using fallback values with CSS Variables to optimise code.

By now, most of us are accustomed to using CSS Custom Properties on projects. Since its introduction in 2017, CSS Custom Properties, also known as CSS variables, have optimised the code we work with and allowed engineers a finer degree of control when managing values within rules.

CSS Custom Properties can be tricky to deal with and aren’t always as intuitive as one might hope. However, when used right, they can significantly reduce boilerplate code and keep things efficient, making our work more productive. While the approach below is not perfect, it works well when writing CSS for building projects. Why Use CSS Custom Properties As far as my experience goes, I can only think of three possible reasons to implement a CSS Variable: We want to use a given value in more than one place, We know that a value will change in the future and want to reduce our efforts for refactoring, We need to store dynamic values from JS execution. With this in mind, we can be more assertive about our use cases. Point 3 is less of a concern for this article, but points 1 and 2 are essential. Let's illustrate this using the familiar HTML button element. The Infamous Button Buttons are notoriously tricky to style consistently for such a simple HTML element. Perhaps it’s because they have several different states and applications, regardless - it's usually the first component we Frontend Engineers take on when it comes to a fresh build. Let's have a look at how we could write the style for a button using CSS Custom Properties: :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { --button-bg: var; --button-color: var; --button-border: var; align-items: center; background-color: var; border: 1px solid var; color: var; display: inline-flex; line-height: 1; } As far as this example goes, from a technical standpoint, it’s great. It’s clean and straightforward and would get a 👍 from me in a code review. But could we optimise it further? Well, yes. The var function in CSS is pretty nifty…we’re used to using it in a somewhat one-dimensional way by just rendering out a value; however, it can be more dynamic. The way to do this is by using a fallback value. If we considered the above example, we could rewrite it like so: :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { align-items: center; background-color: var); border: 2px solid var); color: var); cursor: pointer; display: inline-flex; line-height: 1; padding-block: 0.875rem; padding-inline: 1rem; } This is far cleaner and will lead to the same outcomes as the previous example - with less boilerplate code. Overriding it in a variation makes each locally scoped CSS Variable optional. We can easily now optionally define the variables defined in .button as separate variations: .button:where { --button-bg: orange; --button-border: teal; } .button:where { --button-bg: teal; --button-border: orange; } Although this technique is not revolutionary, we care how many lines of compressed text are heading over the wire. The less physical memory a CSS file takes up, the better, which ultimately translates to how many lines of CSS exist. In the first example, we’re looking at almost double the amount of code to achieve the same thing in our optimised example. This compound effect has an influential impact on performance and further improves readability and developer experience. If you'd like to play around a bit further, feel free to fork the following Code Pen: https://codepen.io/iamdainemawer/pen/NWmQZMz Support Since April 2017, according to Google Baseline, this feature is well-supported across all major browsers. If you're interested to learn more about Baseline, read my post about Google Baseline and Its Impact You can check for browser support by adding the following statement to your CSS stylesheets: @supports {} Can I Use - CSS Nesting CSS Specification - CSS Nesting Module Resources MDN - CSS Custom Properties CSS Variables Polyfill CSS Custom Properties can be tricky to deal with and aren’t always as intuitive as one might hope. However, when used right, they can significantly reduce boilerplate code and keep things efficient, making our work more productive. While the approach below is not perfect, it works well when writing CSS for building projects. Why Use CSS Custom Properties As far as my experience goes, I can only think of three possible reasons to implement a CSS Variable: We want to use a given value in more than one place, We know that a value will change in the future and want to reduce our efforts for refactoring, We need to store dynamic values from JS execution. We want to use a given value in more than one place, We want to use a given value in more than one place, We know that a value will change in the future and want to reduce our efforts for refactoring, We know that a value will change in the future and want to reduce our efforts for refactoring, We need to store dynamic values from JS execution. We need to store dynamic values from JS execution. With this in mind, we can be more assertive about our use cases. Point 3 is less of a concern for this article, but points 1 and 2 are essential. Let's illustrate this using the familiar HTML button element. The Infamous Button Buttons are notoriously tricky to style consistently for such a simple HTML element. Perhaps it’s because they have several different states and applications, regardless - it's usually the first component we Frontend Engineers take on when it comes to a fresh build. Let's have a look at how we could write the style for a button using CSS Custom Properties: :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { --button-bg: var; --button-color: var; --button-border: var; align-items: center; background-color: var; border: 1px solid var; color: var; display: inline-flex; line-height: 1; } :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { --button-bg: var; --button-color: var; --button-border: var; align-items: center; background-color: var; border: 1px solid var; color: var; display: inline-flex; line-height: 1; } As far as this example goes, from a technical standpoint, it’s great. It’s clean and straightforward and would get a 👍 from me in a code review. But could we optimise it further? Well, yes. The var function in CSS is pretty nifty…we’re used to using it in a somewhat one-dimensional way by just rendering out a value; however, it can be more dynamic. The way to do this is by using a fallback value . var fallback value If we considered the above example, we could rewrite it like so: :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { align-items: center; background-color: var); border: 2px solid var); color: var); cursor: pointer; display: inline-flex; line-height: 1; padding-block: 0.875rem; padding-inline: 1rem; } :root { --color-primary: purple; --color-text: black; --color-text-inverted: white; --color-border: gray; } .button { align-items: center; background-color: var); border: 2px solid var); color: var); cursor: pointer; display: inline-flex; line-height: 1; padding-block: 0.875rem; padding-inline: 1rem; } This is far cleaner and will lead to the same outcomes as the previous example - with less boilerplate code. Overriding it in a variation makes each locally scoped CSS Variable optional. We can easily now optionally define the variables defined in .button as separate variations: .button:where { --button-bg: orange; --button-border: teal; } .button:where { --button-bg: teal; --button-border: orange; } .button:where { --button-bg: orange; --button-border: teal; } .button:where { --button-bg: teal; --button-border: orange; } Although this technique is not revolutionary, we care how many lines of compressed text are heading over the wire. The less physical memory a CSS file takes up, the better, which ultimately translates to how many lines of CSS exist. In the first example, we’re looking at almost double the amount of code to achieve the same thing in our optimised example. This compound effect has an influential impact on performance and further improves readability and developer experience. If you'd like to play around a bit further, feel free to fork the following Code Pen: https://codepen.io/iamdainemawer/pen/NWmQZMz https://codepen.io/iamdainemawer/pen/NWmQZMz Support Since April 2017, according to Google Baseline , this feature is well-supported across all major browsers. If you're interested to learn more about Baseline, read my post about Google Baseline and Its Impact Google Baseline Google Baseline and Its Impact You can check for browser support by adding the following statement to your CSS stylesheets: @supports {} @supports {} Can I Use - CSS Nesting CSS Specification - CSS Nesting Module Can I Use - CSS Nesting Can I Use - CSS Nesting CSS Specification - CSS Nesting Module CSS Specification - CSS Nesting Module Resources MDN - CSS Custom Properties CSS Variables Polyfill MDN - CSS Custom Properties MDN - CSS Custom Properties CSS Variables Polyfill CSS Variables Polyfill

We have summarized this news so that you can read it quickly. If you are interested in the news, you can read the full text here. Read more:

hackernoon /  🏆 532. in US

 

United States Latest News, United States Headlines

Similar News:You can also read news stories similar to this one that we have collected from other news sources.

Zendaya’s Challengers Wardrobe Is a Mix of Custom and VintageZendaya’s Challengers Wardrobe Is a Mix of Custom and VintageThe star wore three very different Louis Vuitton looks this weekend.
Read more »

Harvard Researchers Develop Programmable Metafluid with Versatile PropertiesHarvard Researchers Develop Programmable Metafluid with Versatile PropertiesHarvard researchers have created a versatile programmable metafluid that can change its properties, including viscosity and optical transparency, in response to pressure. This new class of fluid has potential applications in robotics, optical devices, and energy dissipation, showcasing a significant breakthrough in metamaterial technology.
Read more »

NuPhy’s First Custom Mechanical Keyboard Gem80 Reviewed In FullNuPhy’s First Custom Mechanical Keyboard Gem80 Reviewed In FullI write about consumer audio, computer accessories, Apple Macs, macOS and digital photography at Forbes. I love technology that makes life more fun, more creative and more productive. My mission is to hunt down and review the best products featuring innovative technology so you know what to buy.
Read more »

Letitia James to Begin Claiming Donald Trump's PropertiesLetitia James to Begin Claiming Donald Trump's PropertiesLegal doubts remain about the insurance company posting Trump's $175 million bond in his New York fraud case.
Read more »

Zendaya's Custom 'Challengers' Stilettos Have Tennis Balls for HeelsZendaya's Custom 'Challengers' Stilettos Have Tennis Balls for HeelsBorn and raised in Memphis, India Roby is a freelance journalist based in New York City. In 2021, India graduated from The New School with a Bachelor’s in Journalism + Design and a minor in Fashion Communication.
Read more »

Building a Custom Homebrew Formula: A Case on tfblueprintgenBuilding a Custom Homebrew Formula: A Case on tfblueprintgenIn this brief blog, we are going to discuss how we can create a custom brew package for an app named tfblueprintgen.
Read more »



Render Time: 2026-05-17 09:29:13