If you use Sass or LESS, then you probably already use variables in your style sheets and know how useful they are. If you don’t use a preprocessor, then you might be curious what the fuss is all about and why variables are so popular and how they can be useful. In this article, we’re going to get an overview of why variables are useful, and get acquainted with one particular variable: currentColor.
What are variables good for?
Variables in CSS are useful because they allow us to write more DRY (Don’t Repeat Yourself) code. They are also particularly useful for managing and maintaining large-scale projects that contain a lot of repeated values.
One of the most common use cases for variables are color themes/schemes throughout a website or application. With variables, creating and managing color schemes across a CSS document becomes a lot easier. For example, color schemes usually require you to reuse a few color values for different properties in the CSS file. If you want to change the primary color of a scheme, you would normally have to change all occurrences of that color in the style sheet. Using CSS variables, you can define a variable in one place (for example a variable named “primary-color”), assign a color value to it, and then use the variable as a value anywhere you want in the style sheet. Then, when the time comes to change this color, all you would have to do is assign it a different color value, and all occurrences of that variable in the style sheet will be automatically updated.
CSS 2.1 did not introduce variables. (Although, that’s not entirely true, as you will see in this article.) In 2014, native CSS variables that are similar to preprocessor variables were introduced; these variables are arguably even more capable than preprocessor variables. A CSS variable is accepted as a value by all CSS properties.
In addition to the new variables, CSS already comes with a keyword value that is practically also a variable: the currentColor keyword.
The currentColor keyword
The currentColor keyword is like a CSS variable, except that it has one main restriction: you can only use it where a value is expected; if a property does not accept a “value, it will not accept currentColor as a value.
The following are all examples of using currentColor in properties that accept it as a value.
box-shadow: inset 2px 2px 3px currentColor;
background-color: currentColor; /* not a good idea! */
background-image: linear-gradient(currentColor, transparent);
Another difference between currentColor and other variables is that you don’t get to assign a value to it the same way you would assign other variables values. The value of currentColor is determined by the computed value of the color property that is currently being used on the element. That is, the value of currentColor is equal to the current color property value. And this is where the currentColor name comes from.
So, if we were to go back to our previous example, the currentColor keyword sets the box shadow color to whatever color value you have set on the div. If you haven’t set any color, it will use the inherited color from any of the div’s ancestors. If no ancestor has a color, most browsers will just default to black.
Put another way: the currentColor keyword is used to make properties of an element, or child elements of an element, inherit the color set by the element’s color property. It therefore acts as the inherit value to allow inheritance of a color that would otherwise not be inherited by a property or child element.
This also means that, for properties that already inherit the color value, currentColor will not be of much use.
Properties and elements that inherit the color value by default
When an element has a color value, whether it is explicitly set or inherited, some of the foreground elements of that element that accept a “value will inherit that color value by default.
For example, an element’s borders are part of the element’s foreground; thus, even if you don’t specify a border color, the border will get the same color as the color property value. If the element does not have one, most browsers usually default to black.
The border color in this example will be purple:
.parent {
color: purple;
}
.child {
border: 5px solid; /* we didn’t specify the border color here */
}
The elements that will get/inherit the element’s color value include:
- The element’s text—it is what the
colorproperty is used for. - The text’s outline.
- The element’s border.
- The element’s box shadow.
- An
img’salttext. That is, when the image cannot be displayed, the text that appears in its stead will have that color value. - A list item’s bullet(s) and border.
- In some browsers (e.g Chrome) the horizontal rule’s (`` ) border color. (Without a border, the color will not be affected.)
When you set these element’s properties on an element without explicitly assigning them a color, they will inherit the computed color value of the element by default.
The following demo shows the above elements in action as they inherit the color set on the page’s body. Change the color property value on the body to see these elements’ colors also change.
See the Pen currentColor — Adobe DW Blog by Sara Soueidan (@SaraSoueidan) on CodePen.
At this point, you might be wondering: if so many properties/elements already inherit the color value, how or where can currentColor be useful?
Extending color inheritance with currentColor
There are some places where retrieving the color value and using it could come in handy. One example where currentColor can be used that would not otherwise inherit the color value is gradients. CSS gradient images, be that linear or radial gradients, do not inherit colors. By using currentColor, you can make a linear gradient used as a background image, for example, adjust to match any color you specify somewhere else as the “primary color” of a theme.
background-image: linear-gradient(to bottom, currentColor, #fff);
Such an example was created by Scott Kellum who took this concept a little further and added an animation to the color property. As the color property animates, all the elements affected by that color will also animate their colors. See the Pen currentColor by Scott Kellum (@scottkellum) on CodePen.
This is a great example of using currentColor, particularly the animation part.
However, more practical examples for currentColor exist. Let’s take a look at some of them.
currentColor Use Cases
The idea behind currentColor is to extend the color cascade. This comes in handy in a lot scenarios.
currentColor for theming UI components
From the previous demo, we can move to a more practical (and brilliant, I must say) use case for currentColor demonstrated by Simon “Simurai” in a talk he gave at CSSConfau last year. The talk was about how we can use Flexbox, currentColor and em units inside UI components to quickly style entire Web Apps straight in the browser.
To demonstrate the usefulness of currentColor, Simon created a set of UI elements, including some sliders. These elements have the same color scheme applied. For coloring the sliders and input types, he used the currentColor variable to force the color inheritance in the background color of the slider’s thumb and checkboxes that would otherwise not inherit that color.