The color property
color sets the text color (and everything drawn in the text color by default, such as borders with currentColor). It is inherited, so it is handy to set it on parent elements.
Syntax
color: red;
color: #0284c7;
color: rgb(2 132 199);
color: rgb(2 132 199 / 50%); /* with transparency */
color: hsl(200 98% 39%); - Initial value
CanvasText- Inherited
- yes
- Applies to
- all elements and text
Values
| Value | What it does |
|---|---|
red | a color name — CSS has over 140 of them |
#0284c7 | hexadecimal #rrggbb (or short #rgb); #rrggbbaa adds transparency |
rgb() | red, green and blue from 0 to 255; after / comes the transparency |
hsl() | hue (0–360°), saturation and lightness in percent — handy for shades of one color |
currentColor | the current color value: a border, shadow or icon takes the text color |
Example
.named { color: red; }
.hex { color: #0284c7; }
.rgb { color: rgb(22 163 74); }
.hsl { color: hsl(280 70% 45%); }
.box {
color: #b45309;
border: 2px solid currentColor;
padding: 8px;
} <p class="named">color: red</p>
<p class="hex">color: #0284c7</p>
<p class="rgb">color: rgb(22 163 74)</p>
<p class="hsl">color: hsl(280 70% 45%)</p>
<div class="box">a currentColor border — the same color as the text</div> Result
Good to know
- color is inherited: set it on body and it applies to all text until a descendant sets its own.
- The initial value is the system text color CanvasText (usually black).
- The background color is a different property, background-color. For readability, normal text needs a contrast of at least 4.5 : 1 with its background (WCAG).
Sources and specification
- MDN: color — MDN Web Docs
- CSS Color Module Level 4 (W3C) — W3C
See also
CSS selectors
CSS selectors pick elements to style: by tag, class, id, attribute and state, plus combinators and specificity — with examples for beginners.
The padding property
The CSS padding property sets the inner space between the border and the content: the 1–4 value forms, percentages, its effect on width and box-sizing.
Build a card with CSS
Practice exercise: build a product card in HTML and link an external stylesheet to it.
Linking CSS
Lesson 11: three ways to add CSS — an external file via <link rel="stylesheet">, a <style> block in head and the style attribute. Example and task.