Patterns for Sensible CSS Customized Properties Use

I have been enjoying round with CSS Customized Properties to find their energy since browser help is lastly at a spot the place we are able to use them in our manufacturing code. I’ve been utilizing them in a quantity other ways and I’d love so that you can get as enthusiastic about them as I’m. They’re so helpful and highly effective!

I discover that CSS variables utilization tends to fall into classes. In fact, you’re free to make use of CSS variables nevertheless you want, however considering of them in these completely different classes may show you how to perceive the other ways during which they can be utilized.

Variables. The fundamentals, akin to setting, a model colour to make use of wherever wanted.
Default Values. For instance, a default border-radius that may be overridden later.
Cascading Values. Utilizing clues primarily based on specificity, akin to consumer preferences.
Scoped Rulesets. Intentional variations on particular person parts, like hyperlinks and buttons.
Mixins. Rulesets supposed to deliver their values to a brand new context.
Inline Properties. Values handed in from inline types in our HTML.

The examples we’ll take a look at are simplified and condensed patterns from a CSS framework I created and keep known as Cutestrap.

A fast word on browser help

There are two frequent traces of questions I hear when Customized Properties come up. The primary is about browser help. What browsers help them? Are there fallbacks we have to use the place they aren’t supported?

The worldwide market share that help the issues we’re masking on this put up is 85%. Nonetheless, it’s price cross-referencing caniuse) along with your consumer base to find out how a lot and the place progressive enhancement is sensible in your challenge.

The second query is at all times about use Customized Properties. So let’s dive into utilization!

Sample 1: Variables

The very first thing we’ll deal with is setting a variable for a model colour as a Customized Property and utilizing it on an SVG ingredient. We’ll additionally use a fallback to cowl customers on trailing browsers.

–brand-color: hsl(230, 80%, 60%);

fill: pink; /* fallback */
fill: var(–brand-color);

Right here, we have declared a variable known as –brand-color in our html ruleset. The variable is outlined on a component that’s at all times current, so it’s going to cascade to each ingredient the place it’s used. Lengthy story brief, we are able to use that variable in our .brand ruleset.

We declared a pink fallback worth for trailing browsers. Within the second fill declaration, we move –brand-color into the var() operate, which is able to return the worth we set for that Customized Property.

That’s just about how the sample goes: outline the variable (–variable-name) after which apply it to a component (var(–variable-name)).

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

Sample 2: Default values

The var() operate we used within the first instance can even present default values in case the Customized Property it’s making an attempt to entry just isn’t set.

For instance, say we give buttons a rounded border. We are able to create a variable — we’ll name it –roundness — however we cannot outline it like we did earlier than. As a substitute, we’ll assign a default worth when placing the variable to make use of.


A use case for default values with out defining the Customized Property is when your challenge continues to be in design however your characteristic is due right this moment. This make it rather a lot simpler to replace the worth later if the design modifications.

So, you give your button a pleasant default, meet your deadline and when –roundness is lastly set as a world Customized Property, your button will get that replace without cost without having to return again to it.

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

You possibly can edit on CodePen and uncomment the code above to see what the button will seem like when –roundness is ready!

Sample three: Cascading values

Now that we have got the fundamentals below our belt, let’s begin constructing the long run we owe ourselves. I actually miss the persona that AIM and MySpace had by letting customers categorical themselves with custom textual content and background colours on profiles.

Let’s deliver that again and construct a faculty message board the place every pupil can set their very own font, background colour and textual content colour for the messages they put up.

Person-based themes

What we’re principally doing is letting college students create a custom theme. We’ll set the theme configurations inside data-attribute rulesets in order that any descendants — a .message ingredient on this case — that eat the themes may have entry to these Customized Properties.

background-color: var(–student-background, #fff);
colour: var(–student-color, #000);
font-family: var(–student-font, “Instances New Roman”, serif);
margin-bottom: 10px;
padding: 10px;



–student-background: blue;
–student-color: yellow;
–student-font: “Comedian Sans MS”, “Comedian Sans”, cursive;

Right here’s the markup:

Chris: I’ve spoken at occasions and given workshops all around the world at conferences.

Rachel: I favor e-mail over different types of communication.

Jen: For this reason I instantly arrange my new staff with Slack for real-time chat.

Tyler: I miss AIM and MySpace, however this message board is okay.

Now we have set all of our pupil themes utilizing [data-student-theme] selectors for our pupil theme rulesets. The Customized Properties for background, colour, and font will apply to our .message ruleset if they’re set for that pupil as a result of .message is a descendant of the div containing the data-attribute that, in flip, comprises the Customized Property values to eat. In any other case, the default values we offered will probably be used as a substitute.

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

Readable theme override

As enjoyable and funky as it’s for customers to regulate custom types, what customers decide will not at all times be accessible with issues for distinction, colour imaginative and prescient deficiency, or anybody that prefers their eyes to not bleed when studying. Keep in mind the GeoCities days?

Let’s add a category that gives a cleaner feel and appear and set it on the father or mother ingredient (

) so it overrides any pupil theme when it’s current.

.readable-theme [data-student-theme]
–student-background: hsl(50, 50%, 90%);
–student-color: hsl(200, 50%, 10%);
–student-font: Verdana, Geneva, sans-serif;

We’re using the cascade to override the coed themes by setting the next specificity such that the background, colour, and font will probably be in scope and can apply to each .message ruleset.

See the Pen
Patterns for Sensible Customized Properties: Instance three.1 by Tyler Childs (@tylerchilds)
on CodePen.

Sample four: Scoped rulesets

Talking of scope, we are able to scope Customized Properties and use them to streamline what’s in any other case boilerplate CSS. For instance, we are able to outline variables for various hyperlink states.





colour: var(–link-active);

Hyperlink Instance

Now that we have written out the Customized Properties globally on the ingredient and used them on our hyperlink states, we needn’t write them once more. These are scoped to our ingredient’s ruleset so they’re solely set on anchor tags and their kids. This enables us to not pollute the worldwide namespace.

Instance: Grayscale hyperlink

Going ahead, we are able to management the hyperlinks we simply created by altering the Customized Properties for our completely different use circumstances. For instance, let’s create a gray-colored hyperlink.

–link: LightSlateGrey;
–link-visited: Silver;
–link-hover: DimGray;
–link-active: LightSteelBlue;

Hyperlink Instance

We’ve declared a .grayscale ruleset that comprises the colours for our completely different hyperlink states. For the reason that selector for this ruleset has a larger specificity then the default, these variable values are used after which utilized to the pseudo-class rulesets for our hyperlink states as a substitute of what was outlined on the ingredient.

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

Instance: Customized hyperlinks

If setting 4 Customized Properties seems like an excessive amount of work, what if we set a single hue as a substitute? That might make issues rather a lot simpler to handle.


–hue: 350;

Hyperlink Instance
Hyperlink Instance

See the Pen
Patterns for Sensible Customized Properties: Instance four.1 by Tyler Childs (@tylerchilds)
on CodePen.

By introducing a variable for a hue worth and making use of it to our HSL colour values within the different variables, we merely have to alter that one worth to replace all 4 hyperlink states.

Calculations are highly effective together with Customized Properties since they let
your types be extra expressive with much less effort. Take a look at this system by Josh Bader the place he makes use of an identical method to implement accessible colour contrasts on buttons.

Sample 5: Mixins

A mixin, with reference to Customized Properties, is a operate declared as a Customized Property worth. The arguments for the mixin are different Customized Properties that can recalculate the mixin once they’re modified which, in flip, will replace types.

The custom hyperlink instance we simply checked out is definitely a mixin. We are able to set the worth for –hue after which every of the 4 hyperlink states will recalculate accordingly.

Instance: Baseline grid basis

Let’s study extra about mixins by making a baseline grid to assist with vertical rhythm. This manner, our content material has a pleasing cadence by using constant spacing.

.baseline *


We have utilized the ruleset for our baseline grid to a .baseline class and any of its descendants.

–rhythm: That is the inspiration of our baseline. Updating it’s going to impression all the opposite properties.
–line-height: That is set to –rhythm by default, since –sub-rhythm just isn’t set right here.
–sub-rhythm: This enables us to override the –line-height — and subsequently, the –font-size — whereas sustaining the general baseline grid.
–line-height-ratio: This helps implement a pleasant quantity of spacing between traces of textual content.
–font-size: That is calculated by dividing our –line-height by our –line-height-ratio.

We additionally set our font-size and line-height in our .baseline ruleset to make use of the –font-size and –line-height from our baseline grid. In brief, every time the rhythm modifications, the road peak and font dimension change accordingly whereas sustaining a legible expertise.

OK, let’s put the baseline to make use of.

Let’s create a tiny webpage. We’ll use our –rhythm Customized Property for the entire spacing between parts.

.baseline h2,
.baseline p,
.baseline ul

.baseline p

.baseline h2
–sub-rhythm: calc(three * var(–rhythm));
–line-height-ratio: 1;

.baseline p,
.baseline h2

.baseline ul

A Tiny Webpage

That is the tiniest webpage. It has three noteworthy options:

  • Tiny
  • Exemplary
  • Identifies as Hufflepuff

We’re basically utilizing two mixins right here: –line-height and –font-size. We have to set the properties font-size and line-height to their Customized Property counterparts with the intention to set the heading and paragraph. The mixins have been recalculated in these rulesets, however they should be set earlier than the up to date styling will probably be utilized to them.

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

One thing to bear in mind: You most likely don’t need to make use of the Customized Property values within the ruleset itself when making use of mixins utilizing a wildcard selector. It provides these types the next specificity than another inheritance that comes together with the cascade, making them exhausting to override with out utilizing !essential.

Sample 6: Inline properties

We are able to additionally declare Customized Properties inline. Let’s construct a light-weight grid system display.

–columns: auto-fit;

show: grid;
hole: 10px;
grid-template-columns: repeat(var(–columns), minmax(zero, 1fr));

Nic Cage
Nic Cage gray
Bill Murray gray
Nic Cage crazy
Nic Cage gif

By default, the grid has equally sized columns that can mechanically lay themselves right into a single row.

See the Pen
Patterns for Sensible Customized Properties: Instance by Tyler Childs (@tylerchilds)
on CodePen.

To regulate the variety of columns we are able to set our –columns Customized Property
inline on our grid ingredient.

See the Pen
Patterns for Sensible Customized Properties: Instance 6.1 by Tyler Childs (@tylerchilds)
on CodePen.

We simply checked out six completely different use circumstances for Customized Properties — at the least ones that I generally use. Even if you happen to had been already conscious of and have been utilizing Customized Properties, hopefully seeing them used these methods provides you a greater concept of when and the place to make use of them successfully.

Are there several types of patterns you utilize with Customized Properties? Share them within the feedback and hyperlink up some demos — I’d like to see them!

In case you’re new to Customized Properties are need to degree up, strive enjoying round with the examples we lined right here, however add media queries to the combo. You’ll see how adaptive these could be and what number of fascinating alternatives open up when you’ve gotten the ability to alter values on the fly.

Plus, there are a ton of different nice sources proper right here on CSS-Tips to up your Customized Properties sport within the Customized Properties Information.

See the Pen
Thanks for Studying! by Tyler Childs (@tylerchilds)
on CodePen.

Leave a Reply