CSS background-color Property
The background-color property sets the background color of an HTML element.
It accepts any valid CSS color value, including named colors, hexadecimal values, RGB, HSL, and modern color functions. Background colors can be solid or partially transparent and are painted beneath any background images applied to the element.
background-color Syntax
selector {
background-color: #1769aa;
}
The property accepts any valid CSS <color> value.
.named-background {
background-color: lightblue;
}
.hex-background {
background-color: #1769aa;
}
.rgb-background {
background-color: rgb(23 105 170);
}
.hsl-background {
background-color: hsl(207 76% 38%);
}
.transparent-background {
background-color: rgb(23 105 170 / 50%);
}
.current-background {
background-color: currentColor;
}
The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.
Property Values
| Value | Description |
|---|---|
<color> |
Any valid CSS color value, including named colors, hexadecimal notation, and supported color functions. |
transparent |
A fully transparent color. This is the initial value. |
currentColor |
Uses the element's computed color value as its background color. |
inherit |
Uses the computed background color of the parent element. |
initial |
Resets the background color to transparent. |
unset |
Resets this non-inherited property to its initial value of transparent. |
The property applies to all elements, is not inherited, and has an initial value of transparent. Its computed value is a computed CSS color, and its animation type is based on color interpolation.
Unlike background-image, which can accept multiple image layers, background-color accepts only one color value.
How background-color Works
The background-color property paints a color behind an element's content and background images.
.panel {
padding: 20px;
background-color: #e8f2fb;
}
The color fills the element's background painting area, which normally extends beneath the border.
Background Painting Area
By default, the background color extends through the content, padding, and border areas. An opaque border covers the color beneath it, while a partially transparent border may allow it to show through.
.colored-panel {
padding: 20px;
border: 8px solid rgb(23 105 170 / 35%);
background-color: #e8f2fb;
}
The background-clip property can restrict background painting to the padding box or content box.
.clipped-panel {
padding: 20px;
border: 8px solid rgb(23 105 170 / 35%);
background-color: #e8f2fb;
background-clip: padding-box;
}
In this example, the background stops at the inner border edge.
Backgrounds Do Not Affect Layout
Setting a background color does not change an element's width, height, margins, padding, or positioning.
The color simply fills the appropriate background painting area.
Named Colors
CSS provides predefined color names that can be used directly as background values.
.blue-panel {
background-color: steelblue;
}
.green-panel {
background-color: seagreen;
}
.yellow-panel {
background-color: gold;
}
.gray-panel {
background-color: lightgray;
}
Named colors are convenient for simple examples and common color choices.
However, they provide a fixed set of colors. Hexadecimal or functional color values are usually more flexible when matching a specific website color palette.
Using currentColor
The currentColor keyword uses the element's computed text color.
.color-matched {
color: #1769aa;
background-color: currentColor;
}
In this example, both the text and background use the same color, so the text may become unreadable. The keyword is more useful when coordinating colors between decorative elements or pseudo-elements.
For example, a decorative indicator can use currentColor to follow its parent's text color.
.label {
color: #1769aa;
}
.label::before {
content: "";
display: inline-block;
width: 8px;
height: 8px;
margin-right: 8px;
background-color: currentColor;
}
Hexadecimal Colors
Hexadecimal color values use a hash symbol followed by hexadecimal digits to represent red, green, and blue color components.
.primary-background {
background-color: #1769aa;
}
.secondary-background {
background-color: #e8f2fb;
}
.dark-background {
background-color: #173b5e;
}
The most common format contains six hexadecimal digits, with two digits for each RGB component.
Short Hexadecimal Notation
.short-hex {
background-color: #09c;
}
The three-digit value #09c is equivalent to #0099cc.
Hexadecimal Transparency
CSS also supports four-digit and eight-digit hexadecimal colors with an alpha component.
.transparent-blue {
background-color: #1769aa80;
}
The final two hexadecimal digits represent opacity. In this example, 80 corresponds to approximately 50% opacity.
Hexadecimal values are useful for maintaining consistent colors across a website or design system.
RGB and RGBA Colors
The rgb() function defines a color using red, green, and blue components.
.rgb-panel {
background-color: rgb(23 105 170);
}
In the common 8-bit form, each component ranges from 0 to 255.
RGB Percentages
.percentage-rgb {
background-color: rgb(20% 50% 80%);
}
RGB components can also be specified using percentages.
RGBA Transparency
Modern CSS allows an alpha value to be added using a slash.
.semi-transparent-panel {
background-color: rgb(23 105 170 / 50%);
}
The alpha value controls opacity from fully transparent to fully opaque.
Legacy comma-separated syntax is also supported.
.legacy-rgba {
background-color: rgba(23, 105, 170, 0.5);
}
The rgba() function is an alias of rgb() in modern CSS, and both support alpha transparency.
RGB values are useful when working directly with numeric color components or generating colors programmatically.
HSL and HSLA Colors
The hsl() function defines colors using hue, saturation, and lightness.
.hsl-panel {
background-color: hsl(207 76% 38%);
}
Hue identifies a position on the color wheel, saturation controls color intensity, and lightness determines how light or dark the color appears.
Adjusting Lightness
.dark-blue {
background-color: hsl(207 76% 25%);
}
.medium-blue {
background-color: hsl(207 76% 45%);
}
.light-blue {
background-color: hsl(207 76% 75%);
}
Changing the lightness value produces darker or lighter variations while keeping the same hue and saturation.
HSLA Transparency
.transparent-hsl {
background-color: hsl(207 76% 38% / 50%);
}
The slash introduces an alpha component.
Legacy syntax using hsla() is also valid.
.legacy-hsla {
background-color: hsla(207, 76%, 38%, 0.5);
}
HSL is convenient for creating coordinated color variations, particularly when adjusting hue, saturation, or lightness independently.
Modern CSS Color Values
Modern CSS supports additional color functions that provide more ways to define and manipulate background colors.
HWB Colors
The hwb() function defines a color using hue, whiteness, and blackness.
.hwb-background {
background-color: hwb(207 9% 33%);
}
LAB and LCH Colors
The lab() and lch() functions use perceptually oriented color models.
.lab-background {
background-color: lab(50% -5 -35);
}
.lch-background {
background-color: lch(50% 35 265);
}
These functions can be useful for designing color variations with more predictable perceived lightness.
OKLAB and OKLCH Colors
The oklab() and oklch() functions use newer perceptual color models.
.oklab-background {
background-color: oklab(60% -0.05 -0.12);
}
.oklch-background {
background-color: oklch(60% 0.15 250);
}
OKLCH is particularly useful for building color palettes with coordinated lightness and chroma values.
Mixing Colors
The color-mix() function combines two colors in a specified color space.
.mixed-background {
background-color: color-mix(in srgb, #1769aa 70%, white);
}
This example mixes 70% blue with 30% white.
Modern color functions have different browser support histories. Provide a compatible fallback when supporting older browsers.
.modern-background {
background-color: #1769aa;
background-color: oklch(60% 0.15 250);
}
Browsers that do not support the newer value can retain the earlier hexadecimal color.
Transparent Backgrounds
Background colors can be fully transparent or partially transparent.
Fully Transparent Background
.transparent-panel {
background-color: transparent;
}
The transparent keyword represents a fully transparent color. It is the initial value of background-color.
When an element has a transparent background, backgrounds from elements behind it may remain visible.
Partially Transparent Background
.overlay-panel {
background-color: rgb(23 105 170 / 50%);
}
This produces a blue background with 50% opacity.
background-color vs. opacity
Alpha transparency in background-color affects the background color without making the element's text and other foreground content transparent.
.transparent-background {
background-color: rgb(23 105 170 / 50%);
}
.transparent-element {
background-color: #1769aa;
opacity: 0.5;
}
The first example makes only the background color semi-transparent. The second applies opacity to the entire rendered element, including its text, borders, and descendants.
Use an alpha color when the background should be transparent but foreground content should remain fully opaque.
Background Colors and Images
An element can have both a background color and one or more background images.
.image-panel {
background-color: #e8f2fb;
background-image: url("pattern.png");
}
The background color is painted beneath the image.
Using a Fallback Color
.hero {
background-color: #173b5e;
background-image: url("hero.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
}
If the image fails to load or contains transparent areas, the background color remains visible.
Combining Colors and Gradients
.gradient-panel {
background-color: #1769aa;
background-image: linear-gradient(135deg, #1769aa, #69b7e8);
}
CSS gradients are treated as background images. The solid background color is painted beneath the gradient.
Multiple Background Layers
.layered-panel {
background-color: #e8f2fb;
background-image: linear-gradient(rgb(23 105 170 / 30%), rgb(23 105 170 / 30%)), url("pattern.png");
}
The first image layer is painted on top, the second beneath it, and the background color at the bottom.
The background-color property always specifies a single color beneath the image layers.
Using the background Shorthand
The background-color property can be specified through the background shorthand.
.panel {
background: #e8f2fb;
}
This sets the background color and resets the other background components to their initial values.
Combining a Color and Image
.banner {
background: #173b5e url("banner.jpg") center / cover no-repeat;
}
The shorthand sets the color, image, position, size, and repetition in a single declaration.
Shorthand Reset Behavior
.panel {
background-color: #1769aa;
background: url("pattern.png") repeat;
}
The final shorthand declaration resets background-color to transparent, because no color was included.
To preserve the background color, include it in the shorthand or place the longhand declaration afterward.
.panel {
background: url("pattern.png") repeat;
background-color: #1769aa;
}
Use the longhand property when changing only the background color without resetting background images or other settings.
background-color Example
The following example compares solid, transparent, and partially transparent background colors.
Each box uses the same dimensions and typography. The semi-transparent backgrounds allow the striped demonstration container to show through.
<div class="background-color-demo">
<div class="color-box solid">Solid Color</div>
<div class="color-box semi-transparent">50% Opacity</div>
<div class="color-box transparent">Transparent</div>
</div>
.background-color-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
padding: 15px;
background: repeating-linear-gradient(45deg, #e8f2fb 0, #e8f2fb 15px, #c4e1f5 15px, #c4e1f5 30px);
}
.color-box {
display: flex;
align-items: center;
justify-content: center;
min-height: 150px;
padding: 15px;
box-sizing: border-box;
border: 2px solid #1769aa;
border-radius: 6px;
color: #173b5e;
font-weight: bold;
text-align: center;
}
.color-box.solid {
background-color: #69b7e8;
}
.color-box.semi-transparent {
background-color: rgb(105 183 232 / 50%);
}
.color-box.transparent {
background-color: transparent;
}
The solid background completely covers the striped pattern. The semi-transparent background allows some of the pattern to remain visible, while the transparent background reveals it entirely.
The first box uses an opaque hexadecimal color. The second uses an RGB color with an alpha value of 50%.
The third uses transparent, allowing the demonstration container's striped background to remain fully visible.
Notice that the text and borders remain fully opaque in all three examples because only the background color has been changed.
Accessibility and Color Contrast
Background colors play an important role in text readability and interface accessibility.
When choosing a background color, ensure that text and other important content have sufficient contrast against it.
Text Contrast
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 at least 14 points when bold.
.readable-panel {
background-color: #173b5e;
color: #fff;
}
This example uses light text on a dark blue background to provide strong visual separation.
Transparent Backgrounds
When a background is partially transparent, the final displayed color depends on the content behind it.
.overlay-text {
background-color: rgb(23 105 170 / 70%);
color: #fff;
}
Test the resulting contrast over the actual underlying background, especially when the backdrop contains photographs, gradients, or changing content.
Avoid Using Color Alone
Do not rely exclusively on background color to communicate important information such as errors, warnings, or required fields.
Combine color with meaningful text, icons, or other visual indicators so information remains understandable to users with different visual abilities.
Important Considerations
Keep the following points in mind when using background-color:
- Default value: The initial value is
transparent. - Color formats: The property accepts named colors, hexadecimal notation, RGB, HSL, and supported modern CSS color functions.
- Transparency: Alpha values allow backgrounds to be partially transparent without changing the opacity of foreground content.
- Background images: The color is painted beneath any background image layers.
- Single color: The property accepts one color value, not a comma-separated list of colors.
- Gradients: Gradients are background images and cannot be assigned directly to
background-color. - Painting area: The background normally extends beneath the border, but
background-clipcan restrict its painting area. - Layout: Changing the background color does not alter element dimensions or positioning.
- Inheritance: The property is not inherited, although transparent backgrounds can reveal a parent's background.
- Shorthand resets: A
backgroundshorthand declaration resets the color totransparentif no color is included. - Animation: Background colors can transition smoothly between compatible color values.
- Accessibility: Maintain sufficient contrast between foreground content and its displayed background.
- Root element: The root element has special background propagation and painting rules that differ from ordinary elements.
Use background-color when setting or changing a solid or transparent background without modifying other background properties.
Browser Support
The background-color property has Baseline Widely available status and is supported across all modern browsers, including Chrome, Edge, Firefox, Safari, and other major browsers.
Basic named colors, hexadecimal values, RGB, HSL, and alpha transparency are widely supported.
Newer color functions such as oklch(), lab(), and color-mix() have their own compatibility requirements.
Providing Color Fallbacks
.modern-panel {
background-color: #1769aa;
background-color: oklch(60% 0.15 250);
}
Browsers that do not recognize the newer color value ignore that declaration and retain the earlier hexadecimal color.
Visit Can I Use? to check background feature compatibility. Search separately for individual modern color functions when necessary.
Specifications
The background-color property is defined in the CSS Backgrounds and Borders Module Level 3 specification. Its accepted color values are defined by the CSS Color specifications.
