background-color
Specifies the background color of an element.
Sample Code
style.css
/* Transparent (default) */
div.test { background-color: transparent;}
/* Hex color code */
div.test1 { background-color: #ff0;}
/* Color name */
div.test2 { background-color: red;}
/* rgb() function */
div.test3 { background-color: rgb(0, 119, 204);}
/* rgba() for semi-transparency (last value is opacity: 0–1) */
div.test4 { background-color: rgba(0, 0, 0, 0.5);}
/* Practical example using CSS custom properties */
:root { --main-bg: #f0f4ff;}
.card { background-color: var(--main-bg);}
/* Change background color on hover */
button { background-color: #0077cc;}
button:hover { background-color: #005599;}
Available Values
| Value | Description |
|---|---|
| transparent | Sets the background to transparent. This is the default value. |
| Color code or color name | Sets the background color to the specified color. |
Browser Preview
<p style="background-color: #ff0; ">The background color is set to '#ff0'.</p>
Browser Compatibility
Desktop
8 ○
7 ○
6 ○
2 and below ×Mobile
Android Browser
4.4+ ○
3 and below ×Same support as desktop.
Same support as desktop.
※ Version data is based on MDN.
Notes
You can specify the background color using transparent, a web color code, or a color name.
For a full list of supported color names, see the Mozilla MDN documentation.
※ background-color is also covered in detail in this tutorial article.
If you find any errors or copyright issues, please contact us.