Jump to content
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Sign in to follow this  
Rss Bot

An introduction to CSS custom properties

Recommended Posts

One of the single best features of CSS processors is variables. Having the ability to declare once and reuse the variable across your project should not be optional in any system.

Using a preprocessor like Sass (find out more in our article on what is Sass?) gives you at least one extra build step, which can be a pain if you just want to build a thing fast. CSS has come a long way since the dark days of table hacks, so let's look into one of the most useful aspects: custom properties.

We'll start with a simple example.

The :root pseudo selector targets the highest-level parent element in the DOM, giving all elements access to --color-red. Yes, CSS variables definition should start with --, and you can access them anywhere with var(). Well, anywhere that is cascaded under our :root selector.

color-red image

With var() you can also define a fallback value, which will be used if the given variable is unreachable or non-existent

With var() you can also define a fallback value, which will be used if the given variable is unreachable or non-existent for the class.

Values are inherited from the DOM, which means you can make them more specific.

Every var(--color) is red, except every var(--color) under site-navigation. Generally speaking it's not an ideal practice to overwrite a value that is already defined but there are cases in which a scoped value is still the most adequate solution.

These are the basics but you can do so much more. A useful example for responsive web design is changing layout based on viewport size.

Under 640px the media query triggers, changing the flex direction to column on site-navigation class, making its content vertical (column) instead of the default horizontal (row). In this example, because of the default variable given to site-navigation, you don't even need the initial :root definition of --flex-layout; instead it will actually go straight to row.

amedia written in a green bubble

Under 640px the media query triggers, changing the flex direction to column and making the content vertical instead of horizontal

This is cool but it's not all fun and games just yet. For instance, since the media query is not an element, the breakpoint value cannot come from a custom property. Although CSS Working Group has a draft of using env() for queries, vendor implementation and proper support is probably years  away from where things are today.

That's okay. We will stick to what we have now. One more advanced use case for a custom property is switching themes. You can define a base theme, build your website around it and just switch it out, with the browser doing the heavy lifting. And it's not even that heavy. 

Essentially, think of it being along the lines of Twitter's Night Mode, but without you actually switching the CSS (I see you).

Input colour's picked value replaces the document's --color value, making the change without a hitch. You can play with blend modes, alpha channel colours or pngs – endless possibilities and fun.

white text saying - - dark on a dark background

You can build your website around a base theme and just switch it out, with the browser doing the heavy lifting 

Now with your future-proof CSS in place, which doesn't depend on any third-party developer and uses only custom properties, there is a good chance you are covered and ready to kick-start production.

But what if you want something other than variables in your code, let's say CSS modules? As of today, for existing CSS features like modules or nesting, you couldn't spare the extra build step any longer, but you can implement it with the sweet promise of not having to rewrite code when vendors catch up with the spec.

Instead of refactoring your CSS every time you want to improve your output, you should write your code in a specification aligned that way in the first place. This is the main difference between pre- and post-processors. A preprocessor actually writes the CSS for you (basically, from a text file), while a post-processor aligns your already valid CSS for more browser support, the latter giving you more flexibility in the process.

Using the native method always beats the workaround, and having the working knowledge of future technologies is the best position you can be in when learning CSS.

This article was originally published in issue 306 of net, the world's best-selling magazine for web designers and developers. Subscribe to net here. 

Related articles:

View the full article

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×