← Back to Chapters

CSS Current Keywords

? CSS Current Keywords

⚡ Quick Overview

CSS current keywords are special values that let you refer to the current or inherited value of a property. They help you:

  • Keep styles consistent across borders, text, shadows, and more.
  • Reduce repetition of the same color or value.
  • Make your CSS easier to maintain and theme.

? Key Concepts

  • currentColor: Uses the element’s current color (text color) for other color-related properties like border, outline, box-shadow, and SVG strokes.
  • inherit: Forces a property to take the computed value from its parent element, even if that property does not usually inherit by default.

? Syntax & Theory

currentColor:

  • Reads the element’s computed color value.
  • Useful when you want text, borders, icons, and shadows to share the same color.
  • Changing color automatically updates all places that use currentColor.

inherit:

  • Copies the value from the parent element’s computed style.
  • Helpful when a property is not naturally inherited (for example, border).
  • Gives you explicit control over inheritance behavior.

? Code Example: currentColor & inherit

This example shows how currentColor reuses the text color for the border and how inherit makes a child element use its parent’s color.

? View Code Example
/* Use currentColor so the border matches the text color */
.box {
  color: #007bff;
  border: 2px solid currentColor;
  padding: 15px;
  margin-bottom: 20px;
}

/* Force the child to inherit the parent text color */
.child {
  color: inherit;
  background-color: #f0f0f0;
  padding: 15px;
}

? Live Output (Conceptual)

Imagine this HTML structure:

<div class="box">...<div class="child">...</div></div>

  • The .box text is blue because color: #007bff;.
  • The .box border is also blue because it uses border: 2px solid currentColor;.
  • The .child text uses color: inherit;, so it gets the same blue text color from its parent.
  • The .child has a light gray background, making the blue text stand out.

? Use Cases

  • Buttons where text, border, and icon should always share the same color.
  • Themed components: change one color and have all related styles update automatically.
  • SVG icons using currentColor so they follow the surrounding text color.
  • Typography systems where children inherit font-related properties from a parent container using inherit.

✅ Tips & Best Practices

  • Use currentColor wherever possible for borders, outlines, and shadows to keep colors consistent.
  • Prefer inherit when you want a child to follow a parent’s style instead of setting the same value again.
  • Combine currentColor with CSS variables for powerful theming systems.
  • Test components in different parent contexts to verify that inheritance works as expected.
  • Remember: inherit copies from the parent, while initial resets to the browser’s default.

? Try It Yourself

  • Create a .button class where the text, border, and box-shadow all use currentColor. Change color and observe the effect.
  • Wrap some text in a parent with color: darkgreen; and add child elements that use color: inherit;. Confirm that all children turn dark green.
  • Make a .card component where only the root sets color. Use inherit on headings and paragraphs inside it.
  • Experiment with a dark theme: change the parent’s color and see how components using currentColor adapt automatically.