Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How does currentColor let CSS reuse an element’s text color?

Quick answer

The color property sets an element’s text color and establishes its currentColor value. Set a border or outline color to currentColor to reuse the color that applies to that element. For example, declare color once on a button, then use currentColor for its border and outline. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I want a button’s text, border, and outline to use one color. How does currentColor help?

Geez

The color property sets the text color and establishes that element’s currentColor value. Another color-valued property can use currentColor to refer to that color. [S1] The outline color also accepts currentColor. [S2]

.button {
  color: #2457a7;
  border: 2px solid currentColor;
  outline: 2px solid currentColor;
}

The text, border, and outline here share the button’s color. currentColor supplies the border and outline colors; the example sets their widths and styles separately.

Follow-up question

Does currentColor refer to the nearest parent’s color, or the color that applies to the button?

Geez

It refers to the color of the element where you use it, not automatically to a particular ancestor. If the button inherits its text color, that inherited value is the button’s applicable color. If the button has its own color declaration, its border or outline using currentColor follows that color instead. [S1]

For example, if a parent sets color: #2457a7 and the button inherits it, the button’s border can follow that color. If the button sets color: #8a2d3c, its border follows the button’s own color. When checking a result, look at the color that applies to the element with the currentColor declaration.

Follow-up question

Can I use it for an outline too, and what should I check if the outline looks wrong?

Geez

Yes. outline-color accepts currentColor, so you can make an outline use the element’s text color. [S2] For example:

.notice {
  color: #2457a7;
  outline: 2px solid currentColor;
}

If the outline does not look as expected, check which color applies to .notice, including whether it inherits a value. Also check that the outline has a width and style: currentColor provides its color, not those other settings.

Follow-up question

What’s a quick way to review this pattern in my CSS?

Geez

Follow the rule from the element outward: find its applicable color, then check that each property you want to coordinate uses currentColor. For example, a border and outline on the same element can each use that value. [S1][S2]

As a practical review, change the element’s color declaration and check whether the border and outline follow. That is a suggestion for reviewing your own page, not a claim that this example has been tested in a particular browser. If one does not follow, check whether that property uses currentColor and whether the element’s applicable color is the one you intended.

A practical checklist

  • Identify the element whose text and other details should share a color.

  • Find the color that applies to that element, including any inherited value.

  • Use currentColor for its border or outline color when you want either to follow its text color. [S1][S2]

  • Set border or outline width and style separately as needed.

  • Change the element’s color during review and check whether the coordinated details follow.

A few more useful answers

What happens if I set `color: currentColor`?

For the color property itself, currentColor is treated as inherit. [S1] To make a border or outline follow the text color, use currentColor on that other property.

Can `currentColor` be used for properties other than borders and outlines?

It can be used as the value of another color-valued property to refer to the element’s color. [S1] The examples here cover border and outline colors. [S1][S2]

Does `currentColor` set an outline’s width or style?

No. It supplies the outline color; set width and style separately when needed. [S2]

Sources

References used for this guide.

  1. [S1] MDN: CSS color property and the currentColor value
    developer.mozilla.org · Checked
  2. [S2] MDN: CSS outline-color property
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work