Background Color

In the last lesson you learned about the CSS Box Model. Now let’s start a fun part of CSS. We are going to color the background of our boxes.

🎨 What is Background Color?

Every HTML element sits inside a box. You saw that in the box model lesson. That box has a background. By default the background is see-through. So you only see the page behind it.

Background color is the color that fills the whole box behind your content. You set it with the background-color property.

Think about a wall in your room. The text and images are like a poster you stick on the wall. The background color is the paint on the wall behind the poster. The paint fills the whole wall. The poster just sits on top.

🤔 Why Do We Need It?

Plain white pages look boring. They are also hard to read for a long time. Background color helps you in real ways. Here is what it does for you.

  • It makes parts of the page stand out. Like a colored box for a warning message.
  • It groups things together. A card with a light gray background feels like one unit.
  • It sets the mood of a brand. Think of the blue header on Facebook or the red on YouTube.
  • It makes reading easier. Soft colors feel kinder on the eyes than harsh pure white.

So background color is not just decoration. It guides the eye and makes the page clear.

🧩 The Syntax

The property name is background-color. You give it a color value. That is all.

Here is the basic shape of the rule.

selector {
background-color: value;
}

Now let’s color a real box. This makes the background of a <div> light blue.

div {
background-color: lightblue;
}

A color value is just the way you tell CSS which color you want. You can write it in a few ways. They all do the same job.

  • A color name like red, lightblue, or tomato. Easy to remember. But there is only a fixed set of names.
  • A hex code like #ff0000. The # plus six characters stand for one exact color. This is the most common way.
  • An rgb() value like rgb(255, 0, 0). You mix amounts of red, green, and blue.
  • An rgba() value like rgba(255, 0, 0, 0.5). The last number is opacity. So the color can be see-through.

Here is the same red color written in those different ways.

.box-name { background-color: red; }
.box-hex { background-color: #ff0000; }
.box-rgb { background-color: rgb(255, 0, 0); }
.box-faded { background-color: rgba(255, 0, 0, 0.5); }

Quick note on opacity

Opacity means how see-through a color is. In rgba() the last number goes from 0 to 1. So 0 is fully invisible. 1 is fully solid. And 0.5 is half see-through.

🛠️ A Real Example

Let’s make a simple message card. This is the kind of box you see for a “Payment successful” note.

First the HTML gives us a box with some text inside.

<div class="card">
<p>Payment successful! Your order is on the way.</p>
</div>

Now we paint that card with CSS. We give it a soft green background and some space inside.

.card {
background-color: #e6f9ec;
padding: 16px;
border-radius: 8px;
}

Let me walk through what each line does.

  • background-color: #e6f9ec; fills the card with a very light green. Green feels positive. So it suits a success message.
  • padding: 16px; adds space inside the box. So the text does not touch the edges of the green area.
  • border-radius: 8px; softens the corners. The green box looks rounded, not sharp.

What you see

A soft green rounded box. The success text sits inside with space around it. The green fills the whole box behind the text, like paint on a wall.

🎯 Common Color Values

Here are some handy values you will use a lot. The transparent value is special. It means no color at all, so the page shows through.

Value What it does
white Plain white fill
#f5f5f5 Very light gray, soft on the eyes
#333333 Dark gray, good for dark sections
transparent No color, the page shows through
rgba(0, 0, 0, 0.5) Half see-through black, like a shadow layer

⚠️ Common Mistakes

These small slips trip up many beginners. Watch out for them.

  • Mixing up background and background-color. The background one is a bigger shortcut property. For just a color, background-color is clear and safe.
  • Forgetting the # in a hex code. background-color: ff0000; does nothing. It must be #ff0000.
  • Spelling a color value wrong. colour is not valid in CSS. It is always the American spelling color.
  • Setting a dark background but leaving the text dark too. The text then disappears. Always check that the text still reads well on the new background.

The bad example below has dark text on a dark box, so you cannot read it. The good one fixes that with light text.

/* ❌ Avoid: dark box with dark text, you can't read it */
.bad {
background-color: #222222;
}
/* ✅ Good: dark box gets light text */
.good {
background-color: #222222;
color: #ffffff;
}

✅ Best Practices

A few simple habits keep your colors clean and friendly. Try to keep good contrast, which means a clear difference between the background and the text.

  • Keep enough contrast between the background and the text. So everyone can read it easily.
  • Use light, soft backgrounds for large areas. Save bright bold colors for small parts you want to highlight.
  • Stick to a small set of colors across your site. Too many colors look messy.
  • Use hex or rgb for exact brand colors. Color names are fine for quick tests.

🧩 What You’ve Learned

Let’s quickly go over the main points from this lesson.

  • ✅ Every element has a box, and background-color paints that box.
  • ✅ The default background is see-through, so the page behind shows.
  • ✅ You can write colors as names, hex codes, rgb(), or rgba().
  • rgba() lets you make a color see-through with the opacity number.
  • ✅ Always keep good contrast so the text stays easy to read.

Check Your Knowledge

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    Which property sets the background color of an element?

    Why: background-color fills the element's box. The color property is for text color, not the background.

  2. 2

    What is the default background of an element?

    Why: By default the background is transparent, so you see whatever is behind the element, usually the page.

  3. 3

    In rgba(255, 0, 0, 0.5), what does the 0.5 mean?

    Why: The last value in rgba() is opacity. 0 is fully invisible, 1 is fully solid, and 0.5 is half see-through.

  4. 4

    Why is background-color: ff0000; wrong?

    Why: A hex code must start with #. The correct value is #ff0000, otherwise the browser ignores it.

🚀 What’s Next?

Now you can paint a box with a solid color. Next you will learn how to put a picture behind your content instead.

Background Image

Share & Connect