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.
What property is used to change the text color in CSS?
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 the relevant interview-question hub first, then move into a concrete study plan before targeted company sets.