CSS background-color Property

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The background-color property paints a solid color behind an element's content. By default it fills the border box, under the border. Change the paint area with background-clip.

The color sits under any background-image. Transparent pixels in an image still show the color. To style the text itself, use color. Check contrast so the text stays readable.

Standard-track.

Not deprecated and not experimental. Baseline Widely available. The initial value is transparent. The property is not inherited.

CSS Syntax

syntax.css
background-color: <color> | transparent | currentColor | initial | inherit;

A <color> can be a name (rebeccapurple), hex (#2563eb), rgb(), hsl(), and other color forms. One value only — this property does not take a comma list.

Property Values

Each editor below sets only background-color (and a text color when contrast needs it).

ValueDescriptionTry it
Named colorKeywords such as red or rebeccapurpleTry it
Hex#rgb, #rrggbb, optional alphaTry it
rgb()Red, green, blue channels; optional alphaTry it
hsl()Hue, saturation, lightness; optional alphaTry it
transparentDefault. No fill; the parent background can show throughTry it
currentColorUses the element's computed colorTry it
initialSets this property to its default value, transparentTry it
inheritCopies the background color from the parentTry it

How to Use background-color

Pick a fill, put it on the element, then check text contrast.

  1. Choose a color value: name, hex, rgb(), or hsl().
  2. Set background-color on the element. It is not inherited.
  3. Set a contrasting color for the text. Aim for about 4.5:1 contrast for normal text (WCAG).
  4. Remember the fill sits under any background-image.
PieceRequired?Role
Color valueYesName, hex, rgb(), hsl(), transparent, or currentColor
Text colorStrongly recommendedKeep readable contrast against the fill
Alpha channelOptionalLets a parent color show through without using opacity
background-clipOptionalLimits how far the fill reaches (border, padding, content, text)

Live Preview

Change only background-color on the sample box. The label stays readable on each preset.

The fill sits behind the text. transparent shows the page behind the box.

Fill
Solid background

background-color: #2563eb

CSS Examples

Each snippet sets only the fill you are studying. Text color is added when contrast needs it. Use Try It Yourself for a live editor.

Example 1 — Hex Background

Fill a box with a six-digit hex color and white text.

CSS
<style>
  div {
    background-color: #2563eb;
    color: #fff;
    padding: 16px;
  }
</style>

<div>Solid background</div>
Try It Yourself

How It Works

  1. Pick a hex value. #2563eb is a six-digit RGB hex (blue).
  2. Paint the box. background-color fills the border box by default.
  3. Set the text separately. color: #fff keeps the words readable on the blue fill.
  4. Not inherited. Nested elements do not get this blue unless you set it again or use inherit.

Example 2 — Semi-Transparent Fill

Use an alpha channel so a yellow parent shows through the blue.

CSS
<style>
  section {
    background-color: #fde68a;
    padding: 16px;
  }
  div {
    background-color: rgb(37 99 235 / 0.5);
    padding: 16px;
  }
</style>

<section>
  <div>Half-blue over yellow</div>
</section>
Try It Yourself

How It Works

  1. Paint the parent. The section is solid yellow so you can see what shows through.
  2. Add alpha on the child. rgb(37 99 235 / 0.5) is the same blue at 50% opacity.
  3. Only the fill fades. The text stays fully opaque. That is different from opacity, which would fade the words too.
  4. Other alpha forms. You can also write #2563eb80 or hsl(221 83% 53% / 50%).

Example 3 — Match the Text Color

Reuse the element's color as the background with currentColor.

CSS
<style>
  div {
    color: #db2777;
    background-color: currentColor;
    padding: 16px;
  }
  span {
    color: #fff;
  }
</style>

<div><span>Pink fill from currentColor</span></div>
Try It Yourself

How It Works

  1. Set the foreground first. color: #db2777 is pink on the box.
  2. Point the fill at it. background-color: currentColor copies that computed pink.
  3. Rescue the text. A child span uses white so the words stay readable on the pink fill.
  4. One source of truth. Change the parent's color and the fill follows. Borders can do the same with border-color: currentColor.

Edge Cases & Pitfalls

If the fill looks wrong, one of these is usually why.

image

An opaque image covers the color

The color is always under background-image. A solid gradient or photo hides it. You only see the color through transparent parts of the image.

inherit

Not inherited by default

Children start at transparent, so a parent's blue does not pass down. Use inherit when you want that copy, or set the color again.

contrast

Low contrast fails accessibility

Light text on a light fill is hard to read. Aim for about 4.5:1 for normal text and 3:1 for large text (WCAG).

opacity

opacity fades more than the fill

opacity fades the whole element, including text and children. Prefer an alpha color when you only want a see-through background.

shorthand

The background shorthand can reset it

A later background rule that omits a color can set the fill back toward transparent. Prefer the longhand while you learn.

Browser Support

background-color is a browser painting property. Node.js, Deno, and Bun do not paint it.

✓ Baseline · Widely available

background-color

Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, Internet Explorer 4+, Opera 3.5+. Named colors, hex, rgb, hsl, transparent, and currentColor are all widely supported in current browsers.

99% Core property support
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 3.5+
Full support
Samsung Internet All modern versions
Full support
Internet Explorer 4+
Full support
background-color Widely supported

Bottom line: Safe everywhere you ship CSS. Prefer modern rgb() / hsl() with slash alpha, or 8-digit hex, in current browsers. Keep text contrast high against the fill.

Key Takeaways

  • Role: paint a solid fill behind content. It sits under any background image.
  • Default: transparent. Not inherited.
  • Not color: this property fills the box; color paints the text.
  • Accessibility: keep strong contrast between the fill and the text.

One line: background-color: #2563eb; fills the box; pair it with a contrasting color for the text.

Frequently Asked Questions

It paints a solid color behind an element's content. The color sits under any background-image. Transparent parts of an image still show the color.
transparent. No fill is painted, so the parent background can show through. The property is not inherited.
background-color fills the area behind the content. color paints the text and other foreground content. Pair them for contrast.
Yes. Use an alpha channel such as rgb(37 99 235 / 0.5), hsl(... / 50%), or an 8-digit hex like #2563eb80. That is different from opacity, which fades the whole element.
Always underneath. List images with background-image; the color is the bottom layer. If the image is opaque, you will not see the color.
It is Baseline Widely available. Chrome 1+, Firefox 1+, Safari 1+, Edge 12+, Internet Explorer 4+, and Opera 3.5+. It is a browser CSS property, not a Node.js feature.

Next: background-image

Learn how to paint a file or a gradient above this solid color.

background-image tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful