1 Answers
π 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)
colorB)
background-imageC)
background-colorD)
text-color2. What is the hexadecimal code for pure white background color?
A)
#000000B)
#FFFFFFC)
#FF0000D)
#0000FF3. Which of the following color values represents a semi-transparent red?
A)
rgb(255, 0, 0)B)
#FF0000C)
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 InEarn 2 Points for answering. If your answer is selected as the best, you'll get +20 Points! π