{"id":5519,"date":"2025-04-10T10:59:27","date_gmt":"2025-04-10T15:59:27","guid":{"rendered":"https:\/\/frontendmasters.com\/blog\/?p=5519"},"modified":"2025-04-22T13:31:19","modified_gmt":"2025-04-22T18:31:19","slug":"using-currentcolor-in-2025","status":"publish","type":"post","link":"https:\/\/frontendmasters.com\/blog\/using-currentcolor-in-2025\/","title":{"rendered":"Using currentColor in 2025"},"content":{"rendered":"\n<p>Gotta give props to <code>currentColor<\/code>. It&#8217;s a keyword in CSS that is the OG <em>variable<\/em>. It wasn&#8217;t <em>always<\/em> there, but it was relatively usable in browsers by, say, 2010.<\/p>\n\n\n\n<p><code>currentColor<\/code> is a value, that you have control over, that represents something else.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.card<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: red;\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">3px<\/span> solid currentColor;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>What do you think the color of that border is gonna be? Not a trick question, it&#8217;s <code>red<\/code> my friend. You don&#8217;t have to type <code>red<\/code> twice. You don&#8217;t have to worry those colors that you&#8217;ve so elegantly tied together are going to drift apart. The power of computer programming lives here.<\/p>\n\n\n\n<p>The value <code>currentColor<\/code> resolves to is whatever the <code>color<\/code> value resolves to at the element being effected by the selector. The <code>color<\/code> property cascades, so it could be set three levels up from the current element, or not at all, or whatever. It&#8217;ll always be <em>something.<\/em><\/p>\n\n\n\n<p class=\"learn-more\">p.s. it can be <code>currentColor<\/code>, <code>currentcolor<\/code>, <code>CURRENTCOLOR<\/code>, <code>cUrrENtCoLOr<\/code> or whatever. CSS is unbothered. Just spel it right.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Hard Times<\/h2>\n\n\n\n<p>Business just isn&#8217;t rolling in thick anymore for <code>currentColor<\/code>. Search a 10 year old codebase and you&#8217;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.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-class\">.card<\/span> {\n  <span class=\"hljs-attribute\">--mainColor<\/span>: red;\n \n  <span class=\"hljs-attribute\">color<\/span>: <span class=\"hljs-built_in\">var<\/span>(--mainColor);\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">3px<\/span> solid <span class=\"hljs-built_in\">var<\/span>(--mainColor);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This does the same thing. It&#8217;s an extra line of code, but your fellow computer programmers will all agree that it&#8217;s much more clear and versatile. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Versatile?<\/h2>\n\n\n\n<p>Definitely. Custom properties can be just about anything. It&#8217;s almost weird how permissive the value of a custom property can be. But it&#8217;s certainly not just colors. Hopefully obviously: <code>currentColor<\/code> only references color. There is no <code>currentPadding<\/code> or <code>currentEmotionalState<\/code> or anything.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Bugs?<\/h2>\n\n\n\n<p>I figured I&#8217;d have <a href=\"https:\/\/codepen.io\/editor\/chriscoyier\/pen\/XJWxVpv\">a play<\/a> for old times sake. While I was doing that, I noticed a few oddities. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: orange;\n  <span class=\"hljs-attribute\">accent-color<\/span>: currentColor; <span class=\"hljs-comment\">\/* doesn't work \ud83e\udd37\u200d\u2640\ufe0f *\/<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That one just doesn&#8217;t work (across the three major browsers I tried). Why? No I&#8217;m asking <em>you.<\/em> lol.<\/p>\n\n\n\n<p>Here&#8217;s another:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">body<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: rebeccaPurple;\n}\n<span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid currentColor;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>That one isn&#8217;t a bug, I suppose, as the trouble is that user agent stylesheets tend to set the <code>color<\/code> of buttons themselves (e.g. <code>color: buttontext;<\/code>), so if you want it to work, you&#8217;ll have to set the color explicitly, or force inherit it.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">button<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: inherit;\n  <span class=\"hljs-attribute\">border<\/span>: <span class=\"hljs-number\">1px<\/span> solid currentColor;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>I also swore I found an issue with relative color syntax and currentColor, but now that I&#8217;m typing and fact checking I can&#8217;t find it again so I&#8217;ll just leave it at that. <\/p>\n\n\n\n<p>I will say that using <code>currentColor<\/code> with the relative color syntax is awesome, like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">a<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: orange;\n  <span class=\"hljs-attribute\">text-decoration-color<\/span>: <span class=\"hljs-built_in\">oklch<\/span>(from currentColor calc(l + <span class=\"hljs-number\">0.1<\/span>) c h \/ <span class=\"hljs-number\">90%<\/span>);\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Which is basically what I was doing in <a href=\"https:\/\/frontendmasters.com\/blog\/chilled-out-text-underlines\/\">Chilled Out Text Underlines<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">But is it still cool though?<\/h2>\n\n\n\n<p>I feel like I&#8217;ve made the point that it&#8217;s not amazingly useful anymore, but I might also argue that it&#8217;s not a terrible idea when the intentionality matches up just right. For instance, say you&#8217;ve got icons where the <code>fill<\/code> should always match the text color. That&#8217;s a fine use case really. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">nav<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: salmon;\n\n  svg.icon {\n    <span class=\"hljs-attribute\">fill<\/span>: currentColor;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Another step further, we could pop a little shadow on those icons that is based on that color. <\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css\"><span class=\"hljs-selector-tag\">nav<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: salmon;\n\n  svg.icon {\n    <span class=\"hljs-attribute\">fill<\/span>: currentColor;\n    <span class=\"hljs-attribute\">filter<\/span>: <span class=\"hljs-built_in\">drop-shadow<\/span>(\n      <span class=\"hljs-number\">0<\/span> <span class=\"hljs-number\">1px<\/span> <span class=\"hljs-number\">0<\/span> oklch(from currentcolor calc(l - <span class=\"hljs-number\">0.25<\/span>) c h));\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here&#8217;s a video of that and some other stuff working together. No custom properties here, all <code>currentColor<\/code>:<\/p>\n\n\n\n\t\t<figure class=\"wp-block-jetpack-videopress jetpack-videopress-player aligncenter wp-block-jetpack-videopress--has-max-width\" style=\"max-width: 585px;\" >\n\t\t\t<div class=\"jetpack-videopress-player__wrapper\"> <iframe title=\"VideoPress Video Player\" aria-label='VideoPress Video Player' width='500' height='462' src='https:\/\/videopress.com\/embed\/JlJiTKxm?cover=1&amp;autoPlay=0&amp;controls=1&amp;loop=0&amp;muted=0&amp;persistVolume=1&amp;playsinline=0&amp;preloadContent=metadata&amp;useAverageColor=1&amp;hd=0' frameborder='0' allowfullscreen data-resize-to-parent=\"true\" allow='clipboard-write'><\/iframe><script src='https:\/\/v0.wordpress.com\/js\/next\/videopress-iframe.js?m=1739540970'><\/script><\/div>\n\t\t\t\n\t\t\t\n\t\t<\/figure>\n\t\t\n\n\n<p>I have stuck in my memory <a href=\"https:\/\/simurai.com\/blog\/2014\/05\/04\/cssconf\">a Simurai post from 2014<\/a> where he showed off the power of this as well. <\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"1008\" height=\"560\" src=\"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/04\/cssconf-color3.gif?resize=1008%2C560&#038;ssl=1\" alt=\"\" class=\"wp-image-5559\" style=\"width:583px;height:auto\"\/><figcaption class=\"wp-element-caption\">Nice.<\/figcaption><\/figure>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Do you need it? Not really, custom properties are probably a better bet. But it still has a bit of utility and it&#8217;s fun to think about.<\/p>\n","protected":false},"author":1,"featured_media":5571,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sig_custom_text":"","sig_image_type":"featured-image","sig_custom_image":0,"sig_is_disabled":false,"inline_featured_image":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[81,7,323,225],"class_list":["post-5519","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog-post","tag-color","tag-css","tag-currentcolor","tag-relative-color-syntax"],"acf":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/frontendmasters.com\/blog\/wp-content\/uploads\/2025\/04\/Using-currentColor-in-2025-1.jpg?fit=1140%2C676&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5519","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/comments?post=5519"}],"version-history":[{"count":5,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5519\/revisions"}],"predecessor-version":[{"id":5649,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/posts\/5519\/revisions\/5649"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media\/5571"}],"wp:attachment":[{"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/media?parent=5519"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/categories?post=5519"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frontendmasters.com\/blog\/wp-json\/wp\/v2\/tags?post=5519"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}