Skip to content

How to Change Background Color in HTML

Short answer: use the CSS background-color property on the element. For the whole page, set it on body: body { background-color: #f0fdf4; }. For one box, use a class or a style attribute: <div style="background-color: lightyellow;">. Set a text color that reads well on it at the same time. Every example below is live: edit the code and the preview changes.

The quick answer

background-color takes any CSS color: a name like lightyellow, hex like #ecfccb, rgb() or hsl(). The color fills the element’s content, its padding and the area under its border, so add some padding to keep the text off the edges.

A class and a style attribute

The examples run in a locked box: scripts are off, nothing is sent anywhere, and your edits go away when you reload. To keep one, copy it into the HTML Editor. Need a color code? Grab one from the HTML Color Picker.

The background of the whole page

Set background-color on body. Even if the page is short, the browser stretches the body’s background to fill the whole window (as long as html has no background of its own), so you won’t see a white strip at the bottom. Set the text color in the same rule, so the two always match.

A dark page

Some old tutorials say <body bgcolor="black">. That attribute is obsolete. Use the CSS above.

A div, section or card

Any block element can have its own background: a <div>, a <section>, a header or a footer. Combine it with padding for room inside and border-radius for rounded corners, and you have a card.

A light card and a dark card

You’ll often see the shorthand background: #f1f5f9;. It works too, but it also resets the other background settings, like an image you set earlier. background-color only changes the color.

Table cells and rows (and why not bgcolor)

Put background-color on th, td or tr. For stripes on every other row (easier to follow across a wide table), use tr:nth-child(even). A color on a cell paints over the color of its row, so one cell can still stand out.

Header, striped rows and one red cell

The bgcolor attribute is obsolete

bgcolor on <body>, <table>, <tr> and <td> is from the early days of HTML. Browsers still show it so old pages don’t break, but it’s not part of HTML5. The one place you’ll still meet it is HTML email, where it’s common even today because some email apps, like desktop Outlook, ignore parts of CSS. For web pages, use CSS. Want a table to start from? The HTML Table Generator builds one with clean CSS.

Old bgcolor vs CSS

Highlight one word

For text that’s highlighted because it’s relevant, like search matches, use <mark>. Browsers show it with a yellow background, and you can change that with CSS. For a background that’s only for looks, like a badge or a label, use a <span> with a class.

<mark> and a span label
Restyle <mark>

Many screen readers don’t say anything special for <mark>, so if the highlight carries meaning (“the changed words are highlighted”), say it in words too.

Gradients, images and see-through colors

A gradient is an image to CSS, not a color, so background-color: linear-gradient(...) does nothing. Use background-image (or the background shorthand), and keep a solid background-color as well. It shows anywhere the gradient can’t, and it gives you a single color to check the text against. Build gradients by dragging color stops in the CSS Gradient Generator.

A gradient with a fallback color
A photo that didn’t load

The same goes for photos: put a color under the image that looks close to it. If the image is slow or missing, the text is still readable. To put a see-through color over a photo or a pattern, give it an alpha value, like rgb(0 0 0 / 65%) or 8-digit hex like #000000a6. Don’t use opacity for this: it fades the text along with the background.

Alpha color vs opacity

Keep the text readable

Every time you pick a background, check the text on it. The guidelines ask for a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text. Bright backgrounds like lime or yellow need dark text; dark and strong colors need white.

Text on colored backgrounds

The Color Contrast Checker gives you the exact ratio for any pair and suggests a fix when it fails. There’s more about text colors in the text color tutorial.

💡 Always set color together with background-color. If you only set the background, the text color may come from somewhere else, like a browser setting or an extension, and end up unreadable on your background.

Dark mode with prefers-color-scheme

Phones and computers have a light or dark setting, and CSS can read it with @media (prefers-color-scheme: dark). The neat way is to keep your colors in CSS variables and only swap the variables in dark mode. Use the Light and Dark buttons to see both. They only change this preview; the code you copy follows the reader’s own setting.

Light and dark colors with variables
  • Add color-scheme. <meta name="color-scheme" content="light dark"> in the head (or :root { color-scheme: light dark; }) tells the browser your page supports both, so scrollbars and form fields match too.
  • Don’t use pure black and pure white. A very dark blue-gray like #0b0f19 with light gray text like #e5e7eb is gentler on the eyes, and still has plenty of contrast.
  • Check the contrast in both modes. A link color that reads well on white is often too dark on a dark background.

Questions people ask

How do I change the background color in HTML?

Use the CSS background-color property. For one element: <div style="background-color: lightyellow;">. In a stylesheet: .box { background-color: #ecfccb; }. It takes any CSS color, such as a name, hex, rgb() or hsl(). Add some padding so the text doesn’t touch the edges.

How do I change the background color of the whole page?

Set it on body: body { background-color: #f0fdf4; color: #111827; }. The browser stretches the body background over the whole window, even when the page is short, as long as html has no background of its own. Set the text color in the same rule so the two always go together.

Can I still use the bgcolor attribute?

Browsers still show bgcolor on body, table, tr and td, but it has been obsolete since HTML5 and validators flag it. Use CSS instead: background-color on the same element, in a class or a style attribute. You’ll still see bgcolor in HTML email, where it’s common because some email apps, like desktop Outlook, ignore parts of CSS.

How do I change the background color of a table cell or row?

Put background-color on the td, th or tr, for example th { background-color: #0b0f19; color: #a3e635; }. For stripes on every other row, use tbody tr:nth-child(even) { background-color: #f1f5f9; }. A color on a cell paints over the color of its row.

How do I highlight one word with a background color?

Use <mark> for text that is highlighted because it is relevant, like search matches. Browsers give it a yellow background, and you can change it with mark { background-color: ...; }. For a background that is only decoration, like a badge, use a span with a class.

Why doesn’t background-color work with a gradient?

A gradient counts as an image in CSS, not a color, so background-color ignores it. Use background-image: linear-gradient(...) or the background shorthand, and keep a solid background-color too, so there’s a color to fall back on and to check the text against.

How do I make a background color see-through?

Give the color an alpha value: rgb(0 0 0 / 50%), hsl(220 40% 10% / 50%) or 8-digit hex like #00000080. Only the background fades and the text stays solid. opacity: 0.5 would fade the text and everything else inside the element as well.

How do I add a background image with a fallback color?

Set both in one rule: background: #1e3a8a url("photo.jpg") center / cover no-repeat;. The color shows while the image loads and if it fails, so light text on top stays readable. Pick a color close to the image.

How do I make a dark mode background?

Use a media query: @media (prefers-color-scheme: dark) { body { background-color: #0b0f19; color: #e5e7eb; } }. It follows the light or dark setting of the reader’s device. Keeping your colors in CSS variables and swapping only the variables keeps it tidy, and color-scheme: light dark makes form fields and scrollbars match.

Keep going

Tools that help