daniel.kelley
daniel.kelley 4d ago β€’ 10 views

CSS `background-color` examples: Adding color to divs, headers, and more

Hey everyone! πŸ‘‹ Struggling with making your web pages pop with color? The `background-color` property in CSS is your best friend for adding life to your divs, headers, and more! Let's dive in and master it together. 🎨
πŸ’» Computer Science & Technology
πŸͺ„

πŸš€ Can't Find Your Exact Topic?

Let our AI Worksheet Generator create custom study notes, online quizzes, and printable PDFs in seconds. 100% Free!

✨ Generate Custom Content

1 Answers

βœ… Best Answer
User Avatar
andrea.thomas Mar 16, 2026

πŸ“š Quick Study Guide: CSS `background-color`

  • 🎨 Purpose: The `background-color` CSS property sets the background color of an element.
  • 🌈 Color Values: Can accept various color formats:
    • πŸ“ Keyword Names: (e.g., `red`, `blue`, `green`, `transparent`).
    • πŸ”’ Hexadecimal: (e.g., `#FF0000` for red, `#00FF00` for green, `#FFFFFF` for white).
    • πŸ“Š RGB: (e.g., `rgb(255, 0, 0)` for red, `rgb(0, 128, 0)` for green).
    • πŸ’‘ RGBA: (e.g., `rgba(255, 0, 0, 0.5)` for semi-transparent red, where 0.5 is the alpha value).
    • 🌟 HSL: (e.g., `hsl(0, 100%, 50%)` for red, `hsl(120, 100%, 25%)` for dark green).
    • πŸ’§ HSLA: (e.g., `hsla(0, 100%, 50%, 0.7)` for semi-transparent red).
  • πŸ”— Inheritance: `background-color` is NOT inherited by default from parent elements. Each element needs to have its `background-color` explicitly set or it will default to `transparent`.
  • 🎯 Application: Can be applied to almost any HTML element, including `
    `, `

    ` to `

    `, `

    `, ``, ``, etc.

  • ✨ Best Practice: Always consider contrast for readability, especially for text over a colored background.
  • βš™οΈ Shorthand: While `background-color` is specific, the `background` shorthand property can also set color along with other background properties (image, repeat, position). Example: `background: red url(image.png) no-repeat center;`

🧠 Practice Quiz

1. Which CSS property is used to set the background color of an HTML element?

A) color

B) background-image

C) background-color

D) text-color

2. What is the hexadecimal code for pure white background color?

A) #000000

B) #FFFFFF

C) #FF0000

D) #0000FF

3. Which of the following color values represents a semi-transparent red?

A) rgb(255, 0, 0)

B) #FF0000

C) rgba(255, 0, 0, 0.5)

D) hsl(0, 100%, 50%)

4. If a `div` element has no `background-color` explicitly set, what will its default background color be?

A) Black

B) White

C) Transparent

D) The same as its parent element

5. Which of the following is NOT a valid way to specify a color for `background-color`?

A) Keyword name (e.g., `blue`)

B) Hexadecimal code (e.g., `#336699`)

C) RGB value (e.g., `rgb(51, 102, 153)`)

D) Font-family name (e.g., `Arial`)

6. To apply a background color to the entire web page, which HTML element should you target in your CSS?

A) <html>

B) <body>

C) <head>

D) <main>

7. The `background-color` property is part of the `background` shorthand property. True or False?

A) True

B) False

C) Only if an image is also present

D) Only for `div` elements

Click to see Answers

1. C

2. B

3. C

4. C

5. D

6. B

7. A

Join the discussion

Please log in to post your answer.

Log In

Earn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! πŸš€