Use the CSS color property to set text (foreground) color. It supports named colors, hex, rgb/rgba, hsl/hsla, and currentColor. In practice, the key is readable contrast against the background.
Use this CSS interview question to rehearse a quick answer, common mistake, follow-up, and production pitfall.
What property is used to change the text color in CSS?Frontend interview answer
This CSS interview question tests whether you can explain CSS Text Color Property: How to Use color Correctly, connect it to production trade-offs, and handle common follow-up questions.
- CSS Text Color Property: How to Use color Correctly explanation without falling back to memorized docs wording
- Colors and Text reasoning, edge cases, and production failure modes
- How you would answer the most likely CSS interview follow-up
Direct answer
The property is color.
h1 {
color: #1d4ed8;
}
p {
color: rgb(31, 41, 55);
}
Format | Example | When teams usually use it |
|---|---|---|
Hex |
| Design tokens and handoff specs. |
RGB/RGBA |
| When opacity control is needed. |
HSL/HSLA |
| Theme tuning with hue/lightness. |
currentColor |
| Keep icon/border aligned with text color. |
Accessibility-first example
:root {
--text-primary: #111827;
--surface: #ffffff;
}
.card {
background: var(--surface);
color: var(--text-primary);
}
Pick colors as a pair (text + background), not in isolation. Good contrast is part of correctness, not polish.
Practical scenario
You are building form states: default text, helper text, error text, success text. You define semantic token colors and reuse them consistently.
Common pitfalls
- Using color alone for status (no icon/text label).
- Passing contrast in light mode but failing in dark mode.
- Hard-coding random hex values across components.
Design tokens require setup, but they pay off in consistency and theme support.
If HTML is the content, color is the ink the user reads. Wrong ink/background pairing makes even correct content unusable.
- Use
colorfor text/foreground color. - It supports multiple color formats and inherits down the tree.
- Always validate contrast on real backgrounds and themes.
Use this as one explanation rep, then continue with the CSS interview questions cluster or a guided prep path.