Skip to main content

How to Set a Background Color in CSS

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

background-color fills the area behind an element's content. Set it on body to color the whole page, or on one element to make it stand out.

Syntax​

Syntax
<selector> {
background-color: <color>;
}
PartWhat it does
<selector>The element to color, such as body or .note.
<color>The background color, such as #fff4ce.

MDN's background-color page lists the values.

The Example​

The example is a page of notices on a light grey page, with one notice on a yellow background.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a style element holding a rule for body that sets the background color f3f2f1 and a rule for the class note that sets the background color fff4ce, and a body with the heading Notices and two paragraphs, the first with the class note.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Background color</title>
<style>
body {
background-color: #f3f2f1;
}

.note {
background-color: #fff4ce;
}
</style>
</head>
<body>
<h1>Notices</h1>
<p class="note">The lift is out of order today.</p>
<p>Lunch is at 12:30.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: a light grey page with the heading Notices, the paragraph The lift is out of order today. on a yellow band across the page, and the paragraph Lunch is at 12:30. on the grey.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The whole page is light grey, rgb(243, 242, 241), because body holds the whole page.
  • The notice is yellow across the full width, rgb(255, 244, 206). A paragraph is a block, so it's as wide as the page, and its background fills all of it.
  • The other paragraph shows the grey of the page. Its own background is transparent, rgba(0, 0, 0, 0), the default.

Common Mistakes​

MistakeWhat happensDo this instead
The British spelling: background-colourIt isn't a CSS property, so Chromium ignored it and the paragraph stayed transparentWrite background-color
Expecting a background on an inline element to fill the lineOn a span, the yellow covered only the word: 37 pixels instead of the paragraph's full widthPut the class on a block element, such as p

Troubleshooting​

SymptomLikely causeFix
No background appearsThe property is misspelled, or the selector doesn't matchCheck the spelling and the selector
The background covers only the textThe element is inline, like spanUse a block element, or give it display: block

FAQ​

Why does the background fill the whole width?​

A p is a block element, and a block takes the whole width of its container. In a window 480 pixels wide, the yellow paragraph was 464 pixels wide: the window minus the page's 8-pixel margins.

What's the default background?​

Transparent. A paragraph without a background showed whatever was behind it, here the grey of body.

Conclusion​

Set background-color on body for the page, and on any element that needs its own background. Block elements fill the whole line, inline ones only the area behind their text.

Additional Resources​