- Home
- /
- Tutorials
- /
- CSS Tutorial
- /
- CSS Background Color
CSS Colors and Backgrounds
CSS Background Color
Set background colors on elements while accounting for transparency, inherited text color, contrast, and the area painted by the background.
background-color fills the area behind an element's content and padding with one color. It is one of the first CSS properties everyone learns, and the main thing to get right is simple: the text on top must stay easy to read.
You can write the color in any format from CSS Colors: a name, HEX, RGB or HSL. For pictures instead of plain color, see CSS Background Image.
Syntax
css
selector {
background-color: value;
}Example
css
body {
background-color: lightblue;
}That colors the whole page light blue.
Where the color is painted
The background fills the content area and the padding, and runs underneath the border. It does not reach into the margin. So if you want more colored space around the text, add padding, not margin.
The default value is transparent, which means you see whatever is behind the element. Backgrounds are not inherited, but because children are transparent, the parent's color shows through them, which looks the same.
Semi-transparent backgrounds
A see-through overlay
css
.notice {
background-color: rgb(255 255 0 / 30%);
}The last value is the transparency. Unlike opacity, which fades the whole element including its text, a transparent background color only affects the background, so the text stays fully solid.
Complete Background Color Example
html
<!DOCTYPE html>
<html>
<head>
<title>CSS Background Color</title>
<style>
body {
background-color: lightgray;
}
h1 {
background-color: lightblue;
color: black;
}
p {
background-color: yellow;
}
div {
background-color: pink;
padding: 10px;
border: 1px solid black;
}
</style>
</head>
<body>
<h1>Background Color Example</h1>
<p>This paragraph has a yellow background.</p>
<div>This is a div with pink background.</div>
</body>
</html>The page is light grey, the heading has a light blue background, the paragraph a yellow one, and the div is pink with a border and some padding.
Browser Support
Feature | Chrome | Edge | Firefox | Safari |
|---|---|---|---|---|
| background-color | 1 | 12 | 1 | 1 |
Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8.
Good to know
- Check contrast. Dark text on a light background, or the reverse, with a contrast ratio of at least 4.5:1 for normal text.
- When you set a background color, also set the text
color. Otherwise someone's dark-mode or custom browser settings can leave you with light text on your light background. - To color the whole page, set it on
body. The browser spreads the body background to the full window.
CSS Background Color exercises
A background
Give the panel the background color #fffbe6.
The HTML your code works on
<div class="wrap">
<h2 class="title">Menu</h2>
<p class="text">Soup of the day.</p>
<div class="panel">Tea $2</div>
</div>Result
