CSS Background Color

The background-color property paints an element's background. Pick a background below — the demo card repaints live, and the legibility check tells you whether black or white text reads better on it.

Card heading

This is body text on your background color. The button below uses the same background with a border, so you can see how interactive elements look too.

How to set a background color in CSS with background-color — with a live demo card: pick any background, see black vs. white text contrast instantly, and copy the CSS.

The background-color property paints an element's background: .card { background-color: #3B82F6; }. Pick a background in the demo — the card repaints live and the legibility check tells you whether black or white text reads better on it.

The background-color property

background-color paints the background of an element — the whole box, out to the border (but not the margin). Unlike color, it is not inherited: a child element has a transparent background by default, so the parent's background shows through it.

.card {
  background-color: #3B82F6;   /* the card's background */
  color: #FFFFFF;              /* always set the text color with it */
}

The default value is transparent. That matters: if you set a text color on an element but no background, the text sits on whatever is behind it — which may be a photo, a gradient, or another element's color. Set color and background-color together, as a pair, every time.

Picking a text color for your background

There is no universal "dark backgrounds need white text" rule — it depends on the exact shade. A mid-tone background like #808080 fails with both black (5.32:1, passes AA) and white (3.95:1, fails AA for normal text) in different ways, and some mid-tones fail with both. The demo above computes both ratios live: use whichever passes, and if neither passes AA, darken or lighten the background until one does. As a rule of thumb, backgrounds lighter than about #767676 usually take black text, and darker ones take white text — but check, don't guess.

Backgrounds beyond a flat color

background-color is the base layer. You can stack a gradient or an image on top of it with background-image, and the color shows through wherever the image is transparent — and while the image is still loading. That is why setting a background color behind a hero image is good practice: if the image fails, the text still sits on a color you chose. The gradient generator builds CSS gradients you can layer the same way.

Common mistakes

White text on a light background: the classic failure — a light brand color with white text that is invisible. Check the ratio first. Forgetting hover states: a button's background often changes on hover; check the text contrast in both states. Body background only: setting a background on body paints the whole page, but a section with its own background needs its own text color too.

Frequently asked questions

How do I set a background color in CSS?

Use the background-color property: .card { background-color: #3B82F6; }. It paints the element's background out to its border. Always set the text color (color) at the same time, so the text stays readable.

Should I use black or white text on a colored background?

It depends on the exact shade — check the contrast ratio. As a rule of thumb, light backgrounds take black text and dark backgrounds take white text, but mid-tones can fail with both. The demo above computes both ratios live and picks the best one.

What is the default background color in CSS?

Transparent. An element with no background-color shows whatever is behind it — its parent's background, or the page background. That is why you should set a background color behind any image or text that must stay readable.

What is the difference between background and background-color?

background is a shorthand that sets all background properties at once (color, image, position, size, and more). background-color sets only the color. If you only need a flat color, background-color is clearer.

Related tools

CSS Text Color · CSS Link Color · CSS Color Basics · Gradient Generator · Contrast Checker