background-color
Sets the background colour of an element.
- Initial value
transparent- Inherited
- No
- Animatable
- yes, as a colour
- Applies to
- all elements
- JavaScript
element.style.backgroundColor = "#1e1533"
Example
#.alert-success {
background-color: rgb(16 185 129 / 0.12);
border: 1px solid rgb(16 185 129 / 0.4);
color: #6ee7b7;
}<p class="alert-success" style="margin: 0; padding: 10px 14px; border-radius: 10px; background-color: rgb(16 185 129 / 0.12); border: 1px solid rgb(16 185 129 / 0.4); color: #6ee7b7;">Task completed! +20 EXP</p>Definition and usage
#The background-color property fills an element’s background with a colour. The background covers the content and padding areas, and by default also the area under the border (visible with a dashed border).
The default value is transparent, so elements without a background show their parent’s background. A colour with an alpha channel, for example rgb(124 58 237 / 0.2), tints the background while keeping what is underneath visible.
The background colour is drawn beneath any background image (background-image). This way it works as a fallback colour if the image fails to load.
Syntax
#background-color: <color>;Values
#- No background, the parent’s background shows through.
- A solid, opaque colour.
with transparency
The same purple at 25% opacity.color-mix()
A mix of two colours in the given proportion.
Values in practice
#transparent
.demo { background-color: transparent; }#7c3aed
.demo { background-color: #7c3aed; }rgb(124 58 237 / 0.25)
.demo { background-color: rgb(124 58 237 / 0.25); }color-mix(…)
.demo { background-color: color-mix(in oklab, #7c3aed 40%, #0ea5e9); }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Semi-transparent backgrounds for status colours (success, error) look good in both light and dark themes.
- Set a background colour under the background image too, so the text is readable before the image loads.
Related entries
#- backgroundA shorthand that sets all background properties in one declaration.
- background-imageSets an image or gradient as an element’s background.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- opacitySets the transparency of the whole element together with its contents.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.