HTML Background Color

The background-color property paints an element's background — the page, a section, a card, or a single word. Pick a color and an opacity below: the preview panel repaints live, the sample text stays on top, and the contrast checker tells you whether the text is still readable.

100%

Card heading on your background

This is body text sitting on the background color. Lower the opacity and the white page behind the panel starts to show through — the color you see is the two colors mixed.

Contrast ratio: —

With opacity below 100%, the background is translucent, so the ratio is measured against the background mixed with the white page behind it. On a different page background the real ratio would differ — check the pair in the contrast checker if the panel sits on anything other than white.

How to set a background color in HTML and CSS: background-color in hex, RGB, HSL, and rgba with alpha explained — live demo with an opacity slider, legibility check, and copyable CSS.

background-color paints an element's background: body { background-color: #3B82F6; }. Pick a color and an opacity in the demo — the panel repaints live, the text stays on top, and the contrast checker tells you whether the text is still readable.

Four ways to write a background color

background-color accepts the same four notations as text color — hex, named color, RGB, and HSL — and the converter on this page copies any of them:

.card {
  background-color: #3B82F6;              /* hex */
  background-color: steelblue;           /* named color */
  background-color: rgb(59, 130, 246);   /* RGB */
  background-color: hsl(217, 91%, 60%);  /* HSL */
}

To paint the whole page, set it on body. The body background fills the entire viewport, even past the end of the content.

body {
  background-color: #F3F4F6;
}

Alpha, in depth: rgba, hsla, and 8-digit hex

All three numeric formats have a transparent variant, and the alpha value always runs from 0 (fully transparent) to 1 (fully opaque):

FormatOpaque50% transparent
RGBArgb(59, 130, 246)rgba(59, 130, 246, 0.5)
HSLAhsl(217, 91%, 60%)hsla(217, 91%, 60%, 0.5)
Hex#3B82F6#3B82F680

Alpha is a fraction, not a percentage: 0.5 means 50%, not 50. In 8-digit hex the alpha is written as two hex digits at the end, so 50% is 80 (128 out of 255), 25% is 40, and 75% is BF. A translucent background does not lighten or darken the color itself — it mixes the color with whatever sits behind the element, which is why the demo shows the “effective color over white”: that is the color your eye actually sees, and the one the contrast ratio is measured against.

rgba is not the same as opacity

background-color: rgba(59, 130, 246, 0.5) makes only the background see-through — the text on top stays fully opaque. The opacity: 0.5 property makes the whole element see-through, including its text and any children, which usually makes the text unreadable. When you want a translucent panel with crisp text, use rgba on the background, never opacity on the element.

Inline vs. block elements — the caveat

background-color paints an element's box, and inline elements have a different box from block elements. A div, a p, or a section is a block: it stretches to the full width available, so its background fills the whole row. A span or an a is inline: its background only covers the text itself, line by line — the rest of the row stays the page background:

<!-- background covers only the words "highlighted text" -->
<p>Some <span style="background-color: #FEF08A;">highlighted text</span> here.</p>

<!-- background covers the full width of the page -->
<div style="background-color: #3B82F6;">A full-width band.</div>

If you want a highlighted word to have padding or a rounded corner that covers more than the text line, give it display: inline-block — it then gets a proper box while staying in the flow of the text.

Backgrounds and readability

Every background needs its text checked against it. White text on #3B82F6 scores 3.68:1 — it passes for large headings (3:1) but fails for normal body text (4.5:1), so a blue card with white body text is a real accessibility failure, not a style choice. The demo above measures the pair live, including the effect of opacity. When a combination fails, darken the text, darken the background, or lighten the background until it passes — the contrast checker can suggest the nearest passing shade.

Frequently asked questions

How do I set a background color in HTML?

With the background-color property: <div style="background-color: #3B82F6;">…</div> for one element, or body { background-color: #3B82F6; } in a stylesheet to paint the whole page. It accepts hex, named colors, rgb(), and hsl().

What does the alpha value in rgba() mean?

Alpha runs from 0 (fully transparent) to 1 (fully opaque): rgba(59, 130, 246, 0.5) is the same blue at 50% opacity, so whatever is behind the element shows through. The same idea in hex is an 8-digit code, #3B82F680.

What is the difference between rgba() and the opacity property?

rgba() on background-color makes only the background translucent — the text stays fully opaque. The opacity property makes the whole element, including its text and children, translucent, which usually makes the text unreadable.

Why does background-color only cover the text on a span?

Because span is an inline element: its background only paints the box around its text, line by line. Block elements like div and p stretch to full width, so their background fills the whole row. Add display: inline-block to an inline element to give it a full box with padding.

How do I check if text is readable on my background color?

Measure the contrast ratio: normal text needs 4.5:1 for WCAG AA, large text needs 3:1. The demo above does it live — and with opacity below 100%, it measures against the color the background actually appears as, mixed with what sits behind it.

Related tools

Tutorials · HTML Text Color · HTML Link Color · CSS Background Color · CSS Color Basics · Contrast Checker