CSS color Property
The color property sets an element's foreground color, primarily affecting text and text decorations.
It also supplies the current color used by other CSS properties through the currentColor keyword. Colors can be specified using named colors, hexadecimal notation, RGB, HSL, HWB, Lab, OKLCH, and other supported CSS color functions.
The color property is inherited, making it useful for establishing consistent text colors throughout a website. To change the background behind an element, use the background-color property instead.
color Syntax
The color property accepts a CSS color value, including a named color, numeric color notation, system color, or supported color function.
selector {
color: #1769aa;
}
General Syntax
selector {
color: <color>;
}
Named Color
p {
color: navy;
}
Hexadecimal Color
p {
color: #1769aa;
}
RGB Color
p {
color: rgb(23 105 170);
}
HSL Color
p {
color: hsl(207 76% 38%);
}
Color with Transparency
p {
color: rgb(23 105 170 / 75%);
}
Current Color
p {
color: currentColor;
}
CSS-Wide Keywords
p {
color: inherit;
}
p {
color: initial;
}
p {
color: unset;
}
p {
color: revert;
}
p {
color: revert-layer;
}
Property Values
The color property supports the CSS <color> data type, which includes several color formats and keywords.
| Value | Description |
|---|---|
| Named colors | Predefined color names such as red, blue, and rebeccapurple. |
| Hexadecimal | Colors written using hexadecimal digits, such as #1769aa. |
rgb() |
Defines a color using red, green, and blue channels. |
hsl() |
Defines a color using hue, saturation, and lightness. |
hwb() |
Defines a color using hue, whiteness, and blackness. |
lab() |
Defines a color using perceptual lightness and two color-opponent coordinates. |
lch() |
Defines a color using lightness, chroma, and hue. |
oklab() |
Defines a color using OKLab lightness and color-opponent coordinates. |
oklch() |
Defines a color using OKLab lightness, chroma, and hue. |
color() |
Specifies a color in an explicit color space, such as display-p3. |
color-mix() |
Mixes two colors in a specified color space. |
currentColor |
References the element's current foreground color. |
| System colors | Uses colors provided by the operating system or browser, such as CanvasText. |
transparent |
Represents a fully transparent color. |
CSS-Wide Keywords
| Value | Description |
|---|---|
inherit |
Uses the computed color value from the parent element. |
initial |
Resets the property to its initial value, CanvasText. |
unset |
Uses the inherited color because the property is inherited. |
revert |
Reverts the property according to cascade origin rules. |
revert-layer |
Reverts the property according to cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | CanvasText |
| Inherited | Yes |
| Applies to | All elements and text |
| Computed value | Computed CSS color value |
| Animation type | Color interpolation |
| Percentages | Supported within applicable color functions |
| Primary use | Setting foreground and text colors |
How It Works
The color property establishes the foreground color of an element and provides a value that other properties can reference.
Step 1: Select an Element
<p class="message">This is a sample message.</p>
Step 2: Apply a Color
.message {
color: #1769aa;
}
The paragraph text appears in the specified blue color.
Step 3: Inherit the Color
<div class="content">
<p>This paragraph inherits its color.</p>
</div>
.content {
color: #1769aa;
}
The paragraph inherits the foreground color from its parent unless another rule overrides it.
Step 4: Override the Color
.content {
color: #1769aa;
}
.content .warning {
color: #b42318;
}
An element with the warning class uses the specified red color instead of inheriting the parent's blue.
Step 5: Reuse the Color
.content {
color: #1769aa;
border: 2px solid currentColor;
}
The border uses the element's current foreground color.
Named Colors
CSS provides predefined color names that can be used directly without numeric values.
Common Named Colors
.red-text {
color: red;
}
.blue-text {
color: blue;
}
.green-text {
color: green;
}
.purple-text {
color: rebeccapurple;
}
Additional Examples
.dark-text {
color: darkslategray;
}
.light-text {
color: lightsteelblue;
}
.highlight-text {
color: tomato;
}
Named Color Characteristics
Named colors are easy to read and convenient for examples. However, hexadecimal or functional notation may be more practical when a design requires exact color values.
Case Insensitivity
.example-one {
color: navy;
}
.example-two {
color: NAVY;
}
Both declarations represent the same named color. Lowercase spelling is generally preferred for consistency.
Hexadecimal Colors
Hexadecimal color notation uses a hash symbol followed by digits representing the red, green, and blue channels.
Six-Digit HEX
.element {
color: #1769aa;
}
Three-Digit HEX
.element {
color: #369;
}
Each digit is expanded to a pair, so #369 is equivalent to #336699.
Eight-Digit HEX
.element {
color: #1769aacc;
}
The final two digits represent the alpha channel, controlling opacity.
Four-Digit HEX
.element {
color: #369c;
}
This shorthand represents red, green, blue, and alpha channels.
Common HEX Values
| Color | HEX Value |
|---|---|
| Black | #000000 |
| White | #ffffff |
| Red | #ff0000 |
| Green | #008000 |
| Blue | #0000ff |
| Gray | #808080 |
Hexadecimal notation is widely used for website color palettes and design systems.
RGB Colors
The rgb() function defines a color using red, green, and blue channels.
Modern RGB Syntax
.element {
color: rgb(23 105 170);
}
Legacy RGB Syntax
.element {
color: rgb(23, 105, 170);
}
Both examples represent the same color.
RGB Percentages
.element {
color: rgb(100% 0% 0%);
}
RGB with Alpha
.element {
color: rgb(23 105 170 / 50%);
}
Legacy rgba() Syntax
.element {
color: rgba(23, 105, 170, 0.5);
}
Modern CSS supports alpha directly in rgb(), so a separate rgba() function is not required.
RGB Channel Ranges
Traditional RGB channel values range from 0 to 255. Percentage values can also be used, and modern CSS permits values outside the traditional range before color processing and display.
HSL Colors
The hsl() function defines a color using hue, saturation, and lightness.
Modern HSL Syntax
.element {
color: hsl(210 70% 40%);
}
Legacy HSL Syntax
.element {
color: hsl(210, 70%, 40%);
}
HSL with Alpha
.element {
color: hsl(210 70% 40% / 60%);
}
Hue
Hue represents a position around the color wheel. Common reference points include red at 0 degrees, green at 120 degrees, and blue at 240 degrees.
Saturation
Saturation describes the intensity of a color. A value of 0% produces a shade of gray, while 100% represents full saturation in HSL.
Lightness
Lightness controls the HSL lightness level. A value of 0% produces black, while 100% produces white.
Adjusting Lightness
.dark-blue {
color: hsl(210 70% 25%);
}
.medium-blue {
color: hsl(210 70% 45%);
}
.light-blue {
color: hsl(210 70% 70%);
}
HSL can be convenient when adjusting colors while keeping the same hue.
HWB Colors
The hwb() function defines a color using hue, whiteness, and blackness.
Basic HWB Syntax
.element {
color: hwb(210 20% 10%);
}
HWB with Alpha
.element {
color: hwb(210 20% 10% / 75%);
}
Whiteness
Increasing whiteness adds more white to the resulting color.
Blackness
Increasing blackness adds more black to the resulting color.
HWB Examples
.color-one {
color: hwb(210 0% 0%);
}
.color-two {
color: hwb(210 30% 10%);
}
.color-three {
color: hwb(210 10% 40%);
}
HWB can be useful for describing variations of a hue in terms of added white and black.
Modern Color Functions
Modern CSS supports additional color spaces and functions that provide more precise or perceptually useful color control.
Lab
.element {
color: lab(55% 20 -30);
}
The lab() function uses perceptual lightness and two opponent-color coordinates.
LCH
.element {
color: lch(60% 50 250);
}
The lch() function expresses color using lightness, chroma, and hue.
OKLab
.element {
color: oklab(60% 0.05 -0.1);
}
OKLab is designed to provide improved perceptual uniformity for many color operations.
OKLCH
.element {
color: oklch(65% 0.15 250);
}
OKLCH is useful for building color palettes with more predictable lightness adjustments.
Display-P3
.element {
color: color(display-p3 0.1 0.5 0.9);
}
The color() function can specify colors in wider-gamut color spaces such as Display-P3.
Color Mixing
.element {
color: color-mix(in oklab, blue 70%, white);
}
The color-mix() function combines colors in a selected color space.
Fallback for Modern Colors
.element {
color: #1769aa;
color: oklch(55% 0.15 245);
}
Browsers that do not support the second declaration retain the earlier hexadecimal color.
Check individual function compatibility when using newer color spaces in production.
currentColor Keyword
The currentColor keyword references the element's current foreground color.
Border Using Text Color
.element {
color: #1769aa;
border: 2px solid currentColor;
}
Background Using Text Color
.element {
color: #1769aa;
background-color: currentColor;
}
In this example, the background uses the same color as the text, which can make the text difficult to read. Choose contrasting colors when necessary.
SVG Icon Using Current Color
.icon {
color: #1769aa;
fill: currentColor;
}
Outline Using Current Color
.element {
color: #1769aa;
outline: 2px solid currentColor;
}
currentColor on the color Property
.element {
color: currentColor;
}
When currentColor is used as the value of the color property itself, it resolves to the inherited color rather than creating a circular reference.
The keyword is especially useful for borders, icons, outlines, and decorative elements that should follow the text color automatically.
System Colors
CSS system color keywords use colors supplied by the browser or operating system's user interface settings.
CanvasText
.element {
color: CanvasText;
}
LinkText
a {
color: LinkText;
}
GrayText
.disabled-label {
color: GrayText;
}
ButtonText
.button-label {
color: ButtonText;
}
System Color Characteristics
System colors can adapt to user preferences and accessibility modes, including forced-colors environments.
Forced Colors
@media (forced-colors: active) {
.custom-button {
color: ButtonText;
border-color: ButtonText;
}
}
Use system colors when an element should follow platform-defined colors rather than a fixed design palette.
Color Inheritance
The color property is inherited, so child elements normally use their parent's foreground color unless another rule specifies a different value.
Parent Color
.article {
color: #333333;
}
Inherited Paragraph Color
<article class="article">
<h2>Article Heading</h2>
<p>This paragraph inherits the article color.</p>
</article>
Override a Child
.article {
color: #333333;
}
.article h2 {
color: #1769aa;
}
Explicit Inheritance
.article a {
color: inherit;
}
The link uses the inherited color rather than the browser's usual link color.
Set a Global Text Color
body {
color: #333333;
}
Setting a base text color on the body element provides a consistent default for much of the document, while allowing individual components to override it.
Transparency
CSS colors can include an alpha channel that controls how transparent the foreground color appears.
RGB Alpha
.element {
color: rgb(23 105 170 / 50%);
}
HSL Alpha
.element {
color: hsl(210 70% 40% / 50%);
}
HEX Alpha
.element {
color: #1769aa80;
}
Transparent Color
.element {
color: transparent;
}
The transparent keyword represents a fully transparent color. The text may remain in the document and accessibility tree even though it is not visible.
Alpha vs. opacity
.alpha-text {
color: rgb(23 105 170 / 50%);
}
.opacity-element {
color: #1769aa;
opacity: 0.5;
}
An alpha channel affects the specified color. The opacity property affects the entire rendered element, including its contents.
For readable text, avoid unnecessarily reducing contrast with transparency.
color vs. background-color
The color and background-color properties control different parts of an element's appearance.
| Property | Purpose |
|---|---|
color |
Sets the foreground color, primarily affecting text. |
background-color |
Sets the background color behind an element. |
Text Color
.message {
color: #1769aa;
}
Background Color
.message {
background-color: #e5f1fb;
}
Using Both Properties
.message {
color: #1769aa;
background-color: #e5f1fb;
padding: 15px;
}
When combining foreground and background colors, choose values that provide sufficient contrast for readability.
Practical Uses
The color property is used throughout websites to establish readable text and consistent visual styling.
Body Text
body {
color: #333333;
}
Headings
h1,
h2,
h3 {
color: #1769aa;
}
Links
a {
color: #1769aa;
}
a:hover {
color: #104b7a;
}
Error Messages
.error-message {
color: #b42318;
}
Success Messages
.success-message {
color: #166534;
}
Muted Text
.muted-text {
color: #666666;
}
CSS Custom Properties
:root {
--text-color: #333333;
--link-color: #1769aa;
}
body {
color: var(--text-color);
}
a {
color: var(--link-color);
}
Responsive Color Changes
@media (prefers-color-scheme: dark) {
body {
color: #f5f5f5;
background-color: #1a1a1a;
}
}
Use consistent color values and custom properties to make site-wide color changes easier to maintain.
color Example
The following example demonstrates named colors, hexadecimal colors, RGB, HSL, and inherited text color.
<div class="color-demo">
<p class="named-color">Named Color: navy</p>
<p class="hex-color">HEX Color: #1769aa</p>
<p class="rgb-color">RGB Color: rgb(180 35 35)</p>
<p class="hsl-color">HSL Color: hsl(145 65% 30%)</p>
<div class="inherited-color">
<p>This paragraph inherits its color.</p>
</div>
</div>
.color-demo {
padding: 15px;
border: 1px solid #cccccc;
}
.color-demo p {
margin-bottom: 12px;
font-weight: bold;
}
.color-demo .named-color {
color: navy;
}
.color-demo .hex-color {
color: #1769aa;
}
.color-demo .rgb-color {
color: rgb(180 35 35);
}
.color-demo .hsl-color {
color: hsl(145 65% 30%);
}
.color-demo .inherited-color {
color: #7040a0;
padding: 10px;
background-color: #f5f5f5;
}
.color-demo .inherited-color p {
margin-bottom: 0;
}
Named Color: navy
HEX Color: #1769aa
RGB Color: rgb(180 35 35)
HSL Color: hsl(145 65% 30%)
This paragraph inherits its color.
Each paragraph uses a different color notation. The final paragraph inherits its purple foreground color from the parent container.
The browser converts supported color values into the appropriate rendered colors regardless of the notation used.
Accessibility
Text color plays an important role in readability and accessibility. Foreground and background colors should provide sufficient contrast for users with low vision or color-vision differences.
Contrast Requirements
WCAG 2.2 Level AA generally requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text.
Large text is generally defined as at least 18 points, or 14 points when bold.
Readable Text Example
.readable-text {
color: #222222;
background-color: #ffffff;
}
Avoid Color-Only Meaning
Do not rely solely on color to communicate errors, success states, required fields, or other important information.
<p class="error-message">Error: Please enter a valid email address.</p>
Respect User Color Preferences
@media (prefers-color-scheme: dark) {
body {
color: #f5f5f5;
background-color: #1a1a1a;
}
}
Forced Colors
@media (forced-colors: active) {
.message {
color: CanvasText;
background-color: Canvas;
}
}
Test Color Combinations
Check contrast for normal, hover, focus, disabled, and error states. Transparent colors and background images can affect the final contrast ratio.
Use color to support clear information rather than as the only way to communicate meaning.
Important Considerations
Keep the following points in mind when using color:
- Purpose: Sets the foreground color, primarily for text.
- Initial value:
CanvasText. - Inheritance: The property is inherited.
- Named colors: CSS supports predefined color names.
- HEX: Supports three-, four-, six-, and eight-digit hexadecimal notation.
- RGB: Defines colors using red, green, and blue channels.
- HSL: Defines colors using hue, saturation, and lightness.
- HWB: Defines colors using hue, whiteness, and blackness.
- Modern functions: Lab, LCH, OKLab, OKLCH, and other functions provide additional color capabilities.
- Alpha: Transparency can be specified within supported color formats.
- currentColor: Allows other properties to reuse the foreground color.
- System colors: Can follow browser and operating system color settings.
- Backgrounds: Use
background-colorto control the background separately. - Animation: Color values can be transitioned and animated.
- Custom properties: CSS variables can centralize color values.
- Accessibility: Maintain sufficient contrast between text and its background.
- Color-only meaning: Do not rely on color alone to communicate important information.
- Browser support: The property is universally supported, although newer color functions have varying compatibility.
Use consistent color values and maintain readable contrast throughout the website. For reusable designs, consider defining a color palette with CSS custom properties.
Browser Support
The color property has been supported by all major browsers for many years, including Chrome, Edge, Firefox, and Safari. It is considered a widely available CSS feature.
Traditional color formats such as named colors, hexadecimal notation, RGB, and HSL have broad support. Newer color functions, including OKLCH, Display-P3, and advanced color mixing, have different browser compatibility requirements.
Widely Supported Example
.element {
color: #1769aa;
}
Modern Color Fallback
.element {
color: #1769aa;
color: oklch(55% 0.15 245);
}
Feature Detection
@supports (color: oklch(60% 0.15 250)) {
.element {
color: oklch(60% 0.15 250);
}
}
For current compatibility details, visit Can I Use? and search for the specific color function or feature being used.
Specifications
The color property and its supported color values are defined in the CSS Color specifications.
These specifications define foreground color behavior, color notation, color spaces, interpolation, and additional color functions.
