DEV Community

Stephanie Eckles
Stephanie Eckles

Posted on

Use `currentColor` to Keep CSS DRY and Component Colors Flexible

What is currentColor

currentColor is a special CSS keyword that can be used to request the color value for use in other CSS properties.

Given the following CSS:

p {
  color: blue;
  border-color: currentColor;
}
Enter fullscreen mode Exit fullscreen mode

Both the text color and the paragraph border will be blue.

If the p was a child of another element and didn't set its own color value, then border-color would pick up the value set in the ancestor.

Given the following, the border-color would now be red.

article {
  color: red;
}

article p {
  border-color: currentColor;
}
Enter fullscreen mode Exit fullscreen mode

How currentColor Keeps CSS DRY

DRY is an acronym for a popular coding principle: Don't Repeat Yourself.

So instead of creating classes for every brand or theme color and repeating or overriding styles such as border-color and box-shadow, you can create color classes that simply set the color property.

Using Sass, we can quickly create 3 classes representing our 3 brand colors:

$theme-colors: (
  "primary": #D73846,
  "secondary": #5D5DDE,
  "tertiary": #19965B
);

@each $key, $value in $theme-colors {
  .ink-#{$key} {
    color: $value;
  }
}
Enter fullscreen mode Exit fullscreen mode

Then for each component that intends to use the theme color across child elements, use the currentColor keyword for those properties.

Example (reduced to relevant properties using currentColor):

.card {
  border-color: 1px solid currentColor;

  &:hover {
    box-shadow: 0 0 1px 2px currentColor;

    &:before {
      // Use for background-color
      background-color: currentcolor;
      // Combine with opacity to use as a screen effect
      opacity: 0.08;
    }
  }

  svg {
    // Use currentColor for svg fill
    fill: currentColor;
  }
}
Enter fullscreen mode Exit fullscreen mode

The following Codepen demonstrates the full .card component with each card using one of the .ink-[theme color] classes:

currentColor Offers Flexibility in Component Colors

Without the use of currentColor you would traditionally have repeated all the styles in the above block to handle the individual brand/theme values for all those properties. For a 3 color theme system, in this example the compiled, minified CSS for separate classes v. not using currentColor would add up to about 520b. Spread across a comprehensive design system, this can add up to kb off on the final production build meaning better overall performance.

In addition, currentColor means those components can intake a one-off color rule just by passing a new color value to .card instead of write yet another entire block to change each value.

#special-card {
  color: pink;
}
Enter fullscreen mode Exit fullscreen mode

Dangers of currentColor

The drawback of using currentColor is the possibility of color combinations that do not appropriately pass contrast guidelines for accessibility. The advantage Sass has here is the ability to include functions within your component color-related rules to check for passing contrast. Considering the intent and expected use cases of a given element or component will help you decide whether to use currentColor over more defined color rules.

One way you may anticipate and overcome conflict is by preemptively declaring contrast-passing styles, particularly for typography, such as:

.card {
  // Perhaps support is a very light color, like pink
  &.ink-support {
    h3, a {
      color: scale-color(pink, $lightness: -23%);
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
mzaini30 profile image
Zen

currentColor native CSS?

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman • Edited

AFAIK, currentColor was come from fill attribute value in SVG to tell that the SVG color must inherit to the surrounding text color. Normally, we use style="color: inherit;" for generic HTML elements. And for borders, without specifying border color, its color should inherit to the text color automatically. Example:

html {
  border: 0 solid green;
}

body {
  color: red;
  border: 5px solid; /* a red border */
  border-color: inherit; /* a green border */
}

stackoverflow.com/a/13002311/1163000

Update: It becomes standard in CSS3 due to the SVG spec β†’ w3.org/TR/css-color-3/#currentcolor πŸ‘

Collapse
 
mzaini30 profile image
Zen

Iya sih. Aku baru denger juga. Yang aku tau ya inherit. Tapi kayaknya pemakaiannya berbeda. Mungkin ya.

Thread Thread
 
5t3ph profile image
Stephanie Eckles

It is standard and has terrific browser support, clear back to IE9!

caniuse.com/#feat=currentcolor

The power is in using it for additional CSS properties beyond border color or SVG fill πŸ‘πŸ» It can even be used in gradients.

Thread Thread
 
taufik_nurrohman profile image
Taufik Nurrohman • Edited

But personally, I would prefer current-color or currentcolor just so that it would match to the CSS naming convention πŸ˜ƒ

I hope this value becomes case insensitive in the future. Same hope for viewBox attribute in SVG. I would like to see it as view-box or viewbox.