How to Change Background Color in HTML: Examples and Fixes

by .

To change the background color in HTML, use the CSS property background-color. Put it on body for a page background, or on a class for one section. The HTML supplies the element; CSS supplies its appearance. You do not need JavaScript to give a page a different color.

Start with one visible change. Make the page pale blue, check that it works, then move the rule into your stylesheet. If the color refuses to change, inspect the element before adding more CSS. A rule applied to the wrong box will never give the result you want.

How to change background color in HTML using body and section selectors
Choose the box first: body colors the page, while a class colors a particular section.

How to change background color in HTML: the quick version

For a small experiment, add a style attribute to the opening body tag. Keep the declaration inside the quotation marks. Save the document, then reload the browser tab that displays that file.

<body style="background-color: #eaf4ff; color: #172033;">
  <h1>A pale blue page</h1>
</body>

The pale blue belongs to the background. The dark color belongs to the text. Writing both makes the intended pairing clear. Change the background value to lavender and reload again; you should see a different shade without changing the content.

Inline CSS is great for testing or troubleshooting. Repeated inline declarations, however, become awkward to maintain. If six pages use the same background, a shared stylesheet lets you change all six in one place. Keep the experiment simple, then choose a more convenient home for the rule.

Set the background of the whole page

For a single HTML file, a style element in the document head is a useful next step. Put the rule inside it, rather than writing CSS as ordinary text between paragraphs.

<style>
  body {
    margin: 0;
    background-color: #eaf4ff;
    color: #172033;
  }
</style>

Removing the default body margin also removes the usual gap around your content. That margin is a layout choice, not a requirement for setting a background. Keep it if the page needs it, or add spacing to a content container instead.

On a normal HTML page with a transparent root background, the body background can paint the page canvas. You do not usually need to give the body a fixed height. If your layout specifically needs a minimum screen-height content area, use a minimum height for that layout, rather than treating it as a color fix.

For a site with several pages, put the body rule in the linked CSS file. Our guide to linking CSS to HTML explains where the link element belongs and how to check the file path. A correct color declaration has no effect when the browser has not loaded its stylesheet.

Give one section its own background

Use a class when only a card, notice or section should change. In this example the class name in the HTML matches the selector in the CSS, including the hyphen. The dot belongs in the CSS selector; it does not belong in the class attribute.

<section class="notice-panel">
  <h2>Opening hours</h2>
  <p>We open at nine.</p>
</section>

.notice-panel {
  background-color: #fff2cc;
  color: #172033;
  padding: 1.5rem;
}

The padding creates space between the text and the edge of the colored box. Without it, a correct background may still look cramped. Margin creates space outside the box, so it does not extend that box’s background. If you want more colored space around the words, padding is usually the control you need.

A background does not position the text. To center a heading inside this panel, add an appropriate alignment rule. See how to center text in HTML if the words need to move as well as change appearance.

Section background diagram showing padding inside a yellow notice panel and margin outside
Padding extends the colored area around the content; margin separates the panel from its neighbours.

Pick a color value you can maintain

You can use a named color, a hexadecimal value, or a color function. Choose the form that is easiest to keep consistent with the rest of the project. There is no advantage in translating every existing hex value into a different notation just to change one background.

Common ways to write a background color
Value Example Useful when
Named color lavender You need a quick recognizable test.
Hexadecimal #eaf4ff You have an exact value in a design palette.
RGB rgb(234 244 255) Your design tool supplies red, green and blue values.
Transparent transparent You want the background behind the box to show through.

A brand palette is a useful source of exact values. For example, Cornell’s veterinary college brand palette identifies its primary carnelian as #b31b1b. That is a concrete example of a reusable color token, rather than a reason to copy another organization’s visual identity.

For your own page, record the approved background and text pairing together. A pale panel might work with dark text but disappear behind a white heading. Copying just the background value from a design file leaves half of that decision unfinished.

Try a complete HTML page

Save the following as background-demo.html and open it in your browser. It includes the document structure, the page background and a separate panel. The CSS stays in the head so this example needs only one file.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Background color demo</title>
  <style>
    body {
      margin: 0;
      background-color: #eaf4ff;
      color: #172033;
      font: 1rem/1.6 system-ui, sans-serif;
    }
    main {
      max-width: 42rem;
      margin-inline: auto;
      padding: 2rem 1rem;
    }
    .notice-panel {
      background-color: #fff2cc;
      padding: 1.5rem;
      border-radius: 0.75rem;
    }
  </style>
</head>
<body>
  <main>
    <h1>Community noticeboard</h1>
    <section class="notice-panel">
      <h2>Opening hours</h2>
      <p>We open at nine.</p>
    </section>
  </main>
</body>
</html>

You should get a pale blue page with a yellow notice panel. Change only the panel background to white. If the entire page changes instead, check which declaration you edited. Keeping the page rule and the component rule separate makes this kind of mistake much easier to spot.

Next, narrow the browser window. The main container has a maximum width, but no fixed width that forces it beyond a small screen. Read the notice at a larger text size too. Background styling should survive ordinary changes in viewport and text size without hiding the content.

Check the text against the new background

Don’t assume a pairing works based solely on looking at two different colored squares side-by-side. Instead, test the actual foreground and background values. Berkeley’s color contrast guidance explains the minimum ratios: ordinary text generally needs 4.5:1, while qualifying large text needs 3:1. A large heading and a small caption can therefore need different checks.

If a panel is translucent, the surface underneath affects the visible result. Test the resulting combination rather than comparing an opaque version of the color. A background image also changes the surface behind the words. A solid panel can be easier to keep readable across an unpredictable photograph.

Use words as well as color for meaning. A green background by itself does not tell every reader that a form succeeded. Add a clear label such as “Saved”. Yale’s accessible color guidance recommends pairing color with another visible indicator. That advice matters for notices, errors and status panels as much as for charts.

Why is the background color not changing?

Inspect the exact element you meant to color. In the browser’s developer tools, check the computed background color and the rules that supplied it. A crossed-out declaration has lost to another rule; a missing declaration points towards a loading, selector or syntax problem.

Background problems and the first thing to check
What you see Check first
No visible change Confirm the stylesheet loaded and the selector matches the element.
The right rule is crossed out Inspect the winning rule and its cascade priority.
A white panel covers the page Inspect that child panel’s own background.
The color disappears later Look for a later background shorthand declaration.
Only a narrow strip changes Check which box has content, padding and height.

Take care with the shorthand background. It sets several background properties at once and resets omitted components to their defaults. A later shorthand for an image can therefore reset a previously declared background color. If you only mean to change the color, the longhand property makes that intent easier to follow.

If you edit a page in a content management system, distinguish the article from the surrounding theme. A body rule added through the site stylesheet can affect every page. A class on one notice is narrower. Try a component class first when the requested change belongs to one piece of content, and check another page before keeping any site-wide rule.

A cache can also leave you looking at an older stylesheet. Confirm the file URL in developer tools and inspect its response before assuming that the saved CSS reached the browser. During a local experiment, save both files and reload the correct tab. On a managed site, follow its normal cache-clearing process after confirming that the edit itself was saved.

Hover and focus rules deserve a separate look when a background changes only while a pointer rests on a button. Inspect the relevant state, then check the base rule as well. Keep a visible keyboard focus indicator; matching a button to the page background should not make its location disappear for someone navigating without a mouse.

Change one possible cause at a time. First prove that your selector matches, then verify the winning value, and finally inspect overlapping boxes. Adding several stronger selectors at once may hide the original fault and leave a stylesheet that is harder to maintain.

Background color troubleshooting checks for selector, winning rule and covering child element
Trace the visible box from selector to computed color before adding another rule.

A short self-check

Which property changes the text rather than its background?

color changes the text color. Use background-color for the surface behind it. Set a readable pair when you choose a new background.

Does margin add more yellow inside the notice panel?

No. Margin makes space outside the panel. Padding adds space inside its background, between the content and the box edge.

Will a section automatically inherit its parent’s background color?

No. Background color is not inherited by default. A transparent section can show its parent’s background through it, which can look similar until another surface sits behind it.

Common questions

Can I change the background without a separate CSS file?

Yes. Use a style element in the document head, or an inline style for a small test. Both are CSS. A separate stylesheet is useful for shared site styles, but it is not required for a single file.

Should I use the old bgcolor attribute?

Use CSS for new HTML. You may meet bgcolor in old documents, but keeping presentation in CSS gives you a clearer way to manage the page and its components.

Does a dark background automatically make the text white?

No. Set or verify the text color as part of the same change. Check links, captions and form controls too, because they may have their own rules. A readable heading does not prove every smaller piece of text has enough contrast.

Can I give every paragraph a different background?

Yes, but choose classes for meaningful groups rather than scattering unrelated inline colors everywhere. Keep text readable and make sure the colors serve a purpose for the reader.

Comments are closed.