background shorthand
The background shorthand property combines color, image, size, position, repeat, and other properties into one declaration. This reduces code and is useful for quick backgrounds.
Background Shorthand Syntax
CSS
/* Individual properties */
.element {
background-color: blue;
background-image: url('image.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
}
/* Shorthand (equivalent) */
.element {
background: blue url('image.jpg') center / cover no-repeat;
/* color image position / size repeat */
}
/* Even more concise */
.element {
background: url('image.jpg') center/cover no-repeat blue;
}Shorthand Order
Order | Property | Value |
|---|---|---|
1 | background-color | blue, #000, rgba() |
2 | background-image | url(), gradient |
3 | background-position | center, top, 50% 50% |
/ | separator | Required before size |
4 | background-size | cover, contain, 100% |
5 | background-repeat | repeat, no-repeat |
CSS
/* Common background shorthand examples */
/* Simple color */
.element {
background: blue;
}
/* Image with fallback color */
.element {
background: blue url('image.jpg') no-repeat center/cover;
}
/* Gradient */
.element {
background: linear-gradient(to right, blue, red);
}
/* Multiple backgrounds shorthand */
.element {
background:
url('overlay.png') no-repeat center,
url('base.jpg') center/cover,
blue;
}Note
Background shorthand is concise but can be confusing. Individual properties are clearer for complex backgrounds. Use shorthand for simple cases.
Next
Colors in CSS: [Colors in CSS](/css/colors-intro).