<p>Custom properties not only enable us to make our code more efficient, but allow us to work some real magic with CSS too. One area where they have huge potential is theming. At Atomic Smash we use Tailwind CSS, a utility class framework, for writing our styles. In this article, we’ll look at how custom properties can be used for theming, and how we can integrate them with Tailwind to maximize the reusability of our code. We won’t cover getting up and running with Tailwind — check out the official documentation for that — but even if you’re new to it you might find some of these tips useful. </p> <p>Theming overview </p> <p>Let’s say we have a “Call To Action” (CTA) component with a heading, body copy, and button. </p> <p>Writing regular (non-Tailwind) CSS for this color scheme would look something like this: </p> <p>.cta {<br />   background-color: #742a2a; // dark red<br />   color: #ffffff; //white<br /> }<br />     <br /> .cta__heading {<br />   background-color: #e53e3e; // medium red<br />   color: #742a2a;<br /> }<br /> 
<br /> .cta__button {<br />   background-color: #e53e3e;<br /> } </p> <p>Using Tailwind, we would apply these colors as utility classes in our HTML: </p> <p>  Join our mailing list<br />  <br />     Be the first to hear about our new offerings<br />     Sign up<br />   </p> <p>I’ve deliberately left out classes relating to anything other than the basic color scheme, but you can see the example in its entirety in this demo: </p> <p>CodePen Embed Fallback </p> <p>Now, if we wanted to apply a different color scheme to our component, we would need to override the color values of our original component. Without Tailwind, a common way to do that would be to append a theme class to the component itself, and redefine the color values lower down in the cascade. So for a component with a modifier class of .cta&#8211;blue (using the BEM convention) we’ll apply the CSS values for a blue color scheme: </p> <p>.cta&#8211;blue {<br />   background-color: #2a4365; // dark blue<br /> }<br /> 
<br /> .cta&#8211;blue .cta__heading {<br />   background-color: #3182ce; // medium blue<br />   color: #2a4365;<br /> }<br /> 
<br /> .cta&#8211;blue .cta__button {<br />   background-color: #3182ce;<br /> } </p> <p>If we’re using Sass or another preprocessor, it’s likely we’ll make life easier for ourselves by using variables for those color names, and we might nest the .cta__heading and .cta__body selectors. It doesn’t exactly make our code more concise, but it does make it more manageable by having a single place to update those values. </p> <p>Now, suppose we have 10 different color schemes, as was my experience on a recent project. Our code starts to get longer, as we’re basically duplicating the above example 10 times in order to change those color values. Now imagine every component in our design system needs 10 color schemes, and many of those components are far more complex than our simple CTA. Maybe our themes need different fonts too. Suddenly we have a lot of CSS to write. </p> <p>Theming with Tailwind </p> <p>If we’re using Tailwind, on the other hand, we’d need to change multiple classes in the HTML itself. Even if we’re using a JavaScript framework, like React or Vue, this is not exactly a trivial task. In order to ensure unused styles are removed in a production build, Tailwind discourages the use of string concatenation for class names (at the time of writing). So building our themes means potentially piling a lot of logic into our components. </p> <p>Theming with Custom Properties </p> <p>By using custom properties for our color themes, we can drastically reduce the amount of code we need to write, and alleviate the maintenance burden. Let’s first take a look at how we can do this in regular CSS. </p> <p>We define our custom properties as variables on the :root selector, making them global variables. (The body selector would serve us just as well.) Then we can use those variables in a selector, in place of our color property values: </p> <p>:root {<br />   &#8211;primary: #742a2a; // dark red;<br />   &#8211;secondary: #e53e3e; // medium red<br /> }<br /> 
<br /> .cta {<br />   background-color: var(&#8211;primary);<br />   color: white;<br /> }<br /> 
<br /> .cta__heading {<br />   background-color: var(&#8211;secondary);<br />   color: var(&#8211;primary);<br /> }<br /> 
<br /> .cta__button {<br />   background-color: var(&#8211;secondary);<br /> } </p> <p>This is where the real magic happens: now the code for creating each of our themes becomes a case of only updating those custom property values. The new values will be inherited wherever we apply our theme class: </p> <p>.th-blue {<br />   &#8211;primary: #2a4365; // dark blue<br />   &#8211;secondary: #3182ce; // medium blue<br /> } </p> <p>If we want a blue color scheme, we can apply that .th-blue class to the component, or even use it on the tag to apply to apply a page-wide theme, which can be overridden on individual components as desired. Using a utility class potentially saves us writing even more code compared to a component-specific class (such as .cta&#8211;blue in the original code), as it could be applied anywhere in our codebase. </p> <p>CodePen Embed Fallback </p> <p>Handling older browsers </p> <p>Like many agencies, plenty of our clients at Atomic Smash still require us to support Internet Explorer 11. While I’m okay with a progressive enhancement approach in most cases (by providing simpler fallback layouts for browsers that don’t support CSS Grid, for instance), I find theming is one area that often doesn’t allow for easy compromise. Clients want their brand colors and fonts seen, even on older browsers. Providing fallbacks using feature queries would entail a lot of extra work that would negate the benefits of using custom properties in the first place. To overcome this, we need a polyfill. </p> <p>There are a couple of options for polyfilling custom properties in IE 11. </p> <p>postcss-custom-properties </p> <p>The first is using a PostCSS plugin called postcss-custom-properties. If you’re already using PostCSS in your workflow, this is fairly simple to add. It works by processing your CSS and outputting the result of the variable as the property value. So if you have the following CSS: </p> <p>:root {<br />   &#8211;color: red;<br /> }<br /> 
<br /> h1 {<br />   color: var(&#8211;color);<br /> } </p> <p>The processed result will be: </p> <p>h1 {<br />   color: red;<br />   color: var(&#8211;color);<br /> } </p> <p>Browsers that don’t support custom properties will ignore the second rule and fall back to the regular property value. There is also an option to remove the rules with the custom properties in the output, so the file size will be smaller. This means that no browsers will get the custom property — which is an issue if you’re updating variables dynamically — but you’ll be able to use them for static values in your code with no ill effects. </p> <p>Unfortunately this polyfill has some limitations: </p> <p>You need to specify the file (or files) in your config where you’re defining the custom properties.Custom properties can only be defined on the :root selector. </p> <p>The first limitation is relatively trivial, but the second unfortunately renders this polyfill entirely useless for our theming use case. It means we can’t redefine variables on a selector to create our themes. </p> <p>ie11CustomProperties </p> <p>This polyfill option involves serving a client-side script, rather than preprocessing the CSS. We can add the following script to our head to ensure the polyfill will only be loaded in IE 11: </p> <p>window.MSInputMethodContext &#038;&#038; document.documentMode &#038;&#038; document.write(&#8221;); </p> <p>This permits us to enjoy the full benefits of custom properties as in the examples here, so it’s the solution I decided to go with. It has a limitation where custom properties set in style attributes aren’t polyfilled. But I’ve tested it for the theming example above and it works just fine. </p> <p>But what does this have to do with Tailwind?  </p> <p>As we’ve already seen, utility classes — single-purpose classes that can be applied anywhere in our HTML — can make our code more reusable. That’s the main selling point of Tailwind and other utility class frameworks — the size of the CSS file you ship should end up smaller as a result. Tailwind makes multiple color classes available: .bg-red-medium would give us a red background-color property value, .text-red-medium for color and so on for border, box-shadow, or any place you can think of that you might need a color value.  </p> <p>Colors can be defined in a config file: </p> <p>module.exports = {<br />   theme: {<br />     colors: {<br />       red: {<br />         medium: &#8216;#e53e3e&#8217;,<br />         dark: &#8216;#742a2a&#8217;<br />       },<br />       blue: {<br />         medium: &#8216;#3182ce&#8217;,<br />         dark: &#8216;#2a4365&#8217;<br />       }<br />     }<br />   }<br /> } </p> <p>If we want to use custom property values for our Tailwind classes, we can specify them in the config: </p> <p>module.exports = {<br />   theme: {<br />     colors: {<br />       &#8216;th-primary&#8217;: &#8216;var(&#8211;primary)&#8217;,<br />       &#8216;th-secondary&#8217;: &#8216;var(&#8211;secondary)&#8217;<br />     }<br />   }<br /> } </p> <p>I’m prefixing my colors and theme-related class names with th- so that it’s obvious they’re specifically related to theming, but feel free to use whatever convention suits you. </p> <p>Now those classes will be available to us through Tailwind. Using .bg-th-primary gives us the equivalent of writing: </p> <p>.some-element {<br />   background-color: var(&#8211;primary);<br /> } </p> <p>In our CSS we can define our custom properties for our themes as before: </p> <p>:root {<br />   &#8211;primary: #742a2a;<br />   &#8211;secondary: #742a2a;<br /> }<br /> 
<br /> .th-blue {<br />   &#8211;primary: #2a4365;<br />   &#8211;secondary: #3182ce;<br /> } </p> <p>Let’s apply those classes to our HTML. The first example gives us a component with our default theme (the variables defined on the :root). The second has our blue theme. The only difference is the addition of the .th-blue class on the component. (Once again, I’ve omitted the classes unrelated to the theme, for brevity and clarity.)</p>

Breakdown

Custom properties not only enable us to make our code more efficient, but allow us to work some real magic with CSS too. One area where they have huge potential is theming. At Atomic Smash we use Tailwind CSS...