color accepts several value formats: named colors like red or steelblue, hexadecimal notation like #ff0000, functional notation like rgb(255, 0, 0) or hsl(0, 100%, 50%), and modern formats supporting an alpha/transparency channel, like rgba() or hsl() with a fourth value. Unlike most CSS properties, color is inherited by default, meaning a color set on a parent element automatically applies to its text-containing descendants unless a more specific rule overrides it further down the tree, which is exactly why setting a base text color once on the body element commonly affects the whole page's text without repeating that declaration everywhere.
1Understanding Color
color accepts several value formats: named colors like red or steelblue, hexadecimal notation like #ff0000, functional notation like rgb(255, 0, 0) or hsl(0, 100%, 50%), and modern formats supporting an alpha/transparency channel, like rgba() or hsl() with a fourth value. Unlike most CSS properties, color is inherited by default, meaning a color set on a parent element automatically applies to its text-containing descendants unless a more specific rule overrides it further down the tree, which is exactly why setting a base text color once on the body element commonly affects the whole page's text without repeating that declaration everywhere.
Take advantage of color's automatic inheritance by setting a base text color once on the body or a root element, rather than repeating the same color declaration on every individual text element throughout the page.
body {
color: #333;
}2Practical Example
Here is a real-world application of Color showing how it is used in production CSS code.
.warning {
color: rgba(200, 0, 0, 0.8);
}3Best Practices
Follow these guidelines when working with Color:
1. Set a base color once on a high-level element like body, relying on inheritance to apply it throughout the page's text by default
2. Use HSL notation, hsl(hue, saturation%, lightness%), when you want to programmatically or visually reason about adjusting a color's lightness or saturation, since it's more intuitive for that than hex or RGB
3. Check color contrast against the background for accessibility, ensuring text remains legible for users with visual impairments
Tip: Take advantage of color's automatic inheritance by setting a base text color once on the body or a root element, rather than repeating the same color declaration on every individual text element throughout the page.
body {
color: #333;
}