Using currentColor in 2025
Using currentColor in 2025 êŽë š
Gotta give props to currentColor
. Itâs a keyword in CSS that is the OG variable. It wasnât always there, but it was relatively usable in browsers by, say, 2010. currentColor
is a value, that you have control over, that represents something else.
.card {
color: red;
border: 3px solid currentColor;
}
What do you think the color of that border is gonna be? Not a trick question, itâs red
my friend. You donât have to type red
twice. You donât have to worry those colors that youâve so elegantly tied together are going to drift apart. The power of computer programming lives here.
The value currentColor
resolves to is whatever the color
value resolves to at the element being effected by the selector. The color
property cascades, so it could be set three levels up from the current element, or not at all, or whatever. Itâll always be something.
p.s.
it can be currentColor
, currentcolor
, CURRENTCOLOR
, cUrrENtCoLOr
or whatever. CSS is unbothered. Just spel it right.
Hard Times
Business just isnât rolling in thick anymore for currentColor
. Search a 10 year old codebase and youâll find a few hits, but these days, custom properties are a far more popular choice. Are you turning your back on an old friend? Yes. Is new friend better? Also yes. Just saying.
.card {
--mainColor: red;
color: var(--mainColor);
border: 3px solid var(--mainColor);
}
This does the same thing. Itâs an extra line of code, but your fellow computer programmers will all agree that itâs much more clear and versatile.
Versatile?
Definitely. Custom properties can be just about anything. Itâs almost weird how permissive the value of a custom property can be. But itâs certainly not just colors. Hopefully obviously: currentColor
only references color. There is no currentPadding
or currentEmotionalState
or anything.
Bugs?
I figured Iâd have a play (chriscoyier
) for old times sake. While I was doing that, I noticed a few oddities.
body {
color: orange;
accent-color: currentColor; /* doesn't work đ€·ââïž */
}
That one just doesnât work (across the three major browsers I tried). Why? No Iâm asking you. lol.
Hereâs another:
body {
color: rebeccaPurple;
}
button {
border: 1px solid currentColor;
}
That one isnât a bug, I suppose, as the trouble is that user agent stylesheets tend to set the color
of buttons themselves (e.g. color: buttontext;
), so if you want it to work, youâll have to set the color explicitly, or force inherit it.
button {
color: inherit;
border: 1px solid currentColor;
}
I also swore I found an issue with relative color syntax and currentColor, but now that Iâm typing and fact checking I canât find it again so Iâll just leave it at that.
But is it still cool though?
I feel like Iâve made the point that itâs not amazingly useful anymore, but I might also argue that itâs not a terrible idea when the intentionality matches up just right. For instance, say youâve got icons where the fill
should always match the text color. Thatâs a fine use case really.
nav {
color: salmon;
svg.icon {
fill: currentColor;
}
}
Another step further, we could pop a little shadow on those icons that is based on that color.
nav {
color: salmon;
svg.icon {
fill: currentColor;
filter: drop-shadow(
0 1px 0 oklch(from currentcolor calc(l - 0.25) c h));
}
}
Hereâs a video of that and some other stuff working together. No custom properties here, all currentColor
:
I have stuck in my memory a Simurai post from 2014 where he showed off the power of this as well.
