Background Color
Learn how to change the background color of HTML elements using CSS.
What is Background Color?
The background-color property
is used to set the background color of
an HTML element.
It can be applied to elements such as
div, section,
body, header,
and many others.
How Background Color Works
HTML Element
div
CSS Property
background-color
Browser
Colored background
Example 1: Light Blue Background
We can use a color name such as
lightblue.
div {
background-color: lightblue;
padding: 20px;
}
Output
Example 2: HEX Background Color
Background colors can also be defined using HEX values.
div {
background-color: #e0f2fe;
padding: 20px;
}
Output
Example 3: RGB Background Color
RGB values can also be used to define a background color.
div {
background-color: rgb(220, 252, 231);
padding: 20px;
}
Output
Example 4: Background Color for a Section
We can apply a background color to a complete section of a webpage.
section {
background-color: #f3e8ff;
padding: 30px;
}
Output
CIIT Training Institute
Learn Full Stack Development step by step.
Background Color Formats
CSS supports different ways to specify background colors.
| Format | Example | Meaning |
|---|---|---|
| Color Name |
lightblue
|
Uses a predefined CSS color name. |
| HEX |
#e0f2fe
|
Uses hexadecimal color notation. |
| RGB |
rgb(220, 252, 231)
|
Uses red, green and blue values. |
| RGBA |
rgba(59, 130, 246, 0.5)
|
Adds transparency to the color. |
background-color
Defines the background color of an HTML element.
background-color: lightblue;
padding
Adds space between the content and the background area.
padding: 20px;
The
background-color
property changes the area behind the
content of an element. The content itself
remains inside that colored area.
Summary
-
background-colorchanges the background color of an element. - CSS supports color names, HEX, RGB and RGBA.
-
paddingcan be used to create space inside the colored area. - Background colors can be applied to divs, sections, headers, body and other elements.