CSS background Property
The background property is a shorthand that sets multiple background properties in a single CSS declaration.
It can define background colors, images, gradients, positioning, sizing, repetition, attachment, and painting areas. The shorthand also supports multiple background layers, making it useful for creating everything from simple colored sections to complex decorative backgrounds.
background Syntax
selector {
background: #e8f2fb;
}
The shorthand accepts one or more background components. Components can be combined in a single declaration, provided their syntax and ordering requirements are followed.
.solid-background {
background: #1769aa;
}
.image-background {
background: url("background.jpg");
}
.repeating-background {
background: url("pattern.png") repeat;
}
.positioned-background {
background: url("background.jpg") center / cover no-repeat;
}
.fixed-background {
background: url("background.jpg") center / cover no-repeat fixed;
}
.gradient-background {
background: linear-gradient(135deg, #1769aa, #69b7e8);
}
.combined-background {
background: #e8f2fb url("pattern.png") center / 100px 100px repeat;
}
When specifying both background-position and background-size in the shorthand, separate them with a forward slash. The position must come before the slash, and the size must come after it.
.hero {
background: #173b5e url("hero.jpg") center / cover no-repeat;
}
The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.
Property Values
The background shorthand combines the following longhand properties.
| Property | Description |
|---|---|
background-color |
Sets the background color beneath the background image layers. |
background-image |
Specifies one or more background images or gradients. |
background-position |
Controls where each background image is positioned. |
background-size |
Controls the dimensions of each background image. |
background-repeat |
Determines whether and how background images repeat. |
background-attachment |
Controls how background images behave when content scrolls. |
background-origin |
Defines the box used to position background images. |
background-clip |
Defines the area in which the background is painted. |
Each component has its own initial value. A background declaration resets omitted components to their initial values.
The shorthand applies to all elements and is not inherited. Its animation behavior follows the individual longhand properties. For example, background colors can interpolate, while background image values generally animate discretely.
The computed value of the shorthand is represented through its individual longhand properties rather than a single independent computed value.
How background Works
The background shorthand provides a compact way to specify the appearance of an element's background.
For example, the following longhand declarations define a centered image that covers its container without repeating.
.banner {
background-color: #173b5e;
background-image: url("banner.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
}
The same result can be written using the shorthand:
.banner {
background: #173b5e url("banner.jpg") center / cover no-repeat;
}
The background color is painted beneath the image. It remains useful when the image contains transparent areas, fails to load, or does not cover the entire element.
Backgrounds are decorative and do not change the size of an element. Unlike an HTML <img> element, a CSS background image does not create intrinsic dimensions for its container.
The background is generally painted within the element's box, according to its background clipping and positioning settings.
Background Colors
A background can consist of a single solid color without an image.
.section {
background: #e8f2fb;
}
CSS supports named colors, hexadecimal values, RGB, HSL, and other valid color formats.
.named-color {
background: lightblue;
}
.hex-color {
background: #1769aa;
}
.rgb-color {
background: rgb(23 105 170);
}
.transparent-color {
background: rgb(23 105 170 / 50%);
}
.hsl-color {
background: hsl(207 76% 38%);
}
When a background color includes transparency, content behind the element may remain visible through it.
A background color can also be combined with a background image.
.panel {
background: #f0f5fa url("texture.png") repeat;
}
The color is always painted below the background image layers. When multiple background images are specified, the color can only be included in the final background layer.
Use the background-color longhand when you only need to change the color without resetting other background settings.
Background Images and Gradients
The background property can display images and CSS-generated gradients.
Background Images
.image-panel {
background: url("background.jpg") center / cover no-repeat;
}
The url() function references an image resource. Its path is resolved relative to the stylesheet containing the declaration.
The cover keyword scales the image to cover the background positioning area, potentially cropping part of the image.
Linear Gradients
.linear-panel {
background: linear-gradient(90deg, #1769aa, #69b7e8);
}
A linear gradient transitions between colors along a specified direction.
Radial Gradients
.radial-panel {
background: radial-gradient(circle, #fff, #1769aa);
}
A radial gradient transitions outward from a central point or specified origin.
Conic Gradients
.conic-panel {
background: conic-gradient(#1769aa, #69b7e8, #1769aa);
}
A conic gradient transitions between colors around a center point.
Combining an Image and a Gradient
.hero {
background: linear-gradient(rgb(0 0 0 / 45%), rgb(0 0 0 / 45%)), url("hero.jpg") center / cover no-repeat;
}
The gradient is the first background layer, so it is painted over the image. This technique can darken a photograph to improve the readability of foreground text.
Background images should generally be used for decoration. Important content images should use appropriate HTML elements and alternative text.
Background Position and Size
The background-position and background-size components control where an image appears and how large it is rendered.
Positioning an Image
.top-left {
background: url("pattern.png") left top no-repeat;
}
.centered {
background: url("pattern.png") center no-repeat;
}
.bottom-right {
background: url("pattern.png") right bottom no-repeat;
}
Position values can use keywords, lengths, percentages, or supported combinations of these values.
Using cover
.cover-background {
background: url("photo.jpg") center / cover no-repeat;
}
The cover keyword scales the image proportionally until the background positioning area is completely covered. Some parts of the image may be cropped.
Using contain
.contain-background {
background: url("logo.png") center / contain no-repeat;
}
The contain keyword scales the image proportionally so the entire image fits inside the background positioning area. Uncovered space may remain.
Explicit Background Dimensions
.sized-background {
background: url("icon.png") center / 80px 80px no-repeat;
}
Explicit sizes can use lengths, percentages, or auto. When both dimensions are specified, the image may be stretched if they do not match its natural proportions.
The Required Slash
.correct {
background: url("photo.jpg") center / cover no-repeat;
}
The slash is required when including a background size in the shorthand. A declaration such as background: url("photo.jpg") center cover; is invalid because the size is not separated from the position.
Background Repetition and Attachment
The shorthand can control whether background images repeat and how they behave during scrolling.
Background Repetition
.repeat-both {
background: url("pattern.png") repeat;
}
.repeat-horizontal {
background: url("pattern.png") repeat-x;
}
.repeat-vertical {
background: url("pattern.png") repeat-y;
}
.no-repeat {
background: url("pattern.png") no-repeat;
}
.round-repeat {
background: url("pattern.png") round;
}
.space-repeat {
background: url("pattern.png") space;
}
The repeat value tiles the image horizontally and vertically. The repeat-x and repeat-y values limit repetition to one direction.
The round value adjusts tile dimensions so complete tiles fit the available area. The space value distributes complete tiles with spacing when sufficient room is available.
Background Attachment
.scroll-background {
background: url("photo.jpg") center / cover no-repeat scroll;
}
.fixed-background {
background: url("photo.jpg") center / cover no-repeat fixed;
}
.local-background {
background: url("pattern.png") repeat local;
}
The scroll value is the default. The background scrolls with the element rather than its scrollable content.
The fixed value fixes the background relative to the viewport in typical screen layouts. The local value allows the background to scroll with the element's scrollable contents.
Fixed background effects may behave differently on mobile browsers, so test them across devices.
Background Origin and Clipping
The background-origin and background-clip components determine the box used for positioning and the area where the background is painted.
Background Origin
The origin controls the positioning area for background images.
.padding-origin {
background: url("pattern.png") padding-box;
}
.border-origin {
background: url("pattern.png") border-box;
}
.content-origin {
background: url("pattern.png") content-box;
}
The default origin is padding-box. This positions the background relative to the padding box, excluding the border.
Background Clipping
Clipping controls how far the background is painted.
.border-clipped {
background: #e8f2fb border-box;
}
.padding-clipped {
background: #e8f2fb padding-box;
}
.content-clipped {
background: #e8f2fb content-box;
}
The default clipping area is border-box, which allows the background to extend beneath the border.
Specifying Origin and Clip Together
.custom-background-box {
background: url("pattern.png") content-box padding-box;
}
When two box keywords appear, the first specifies background-origin and the second specifies background-clip.
When only one box keyword is supplied, it sets both properties to that value.
The newer background-clip: text value is generally specified separately rather than as part of the shorthand's standard box-keyword syntax.
Multiple Background Layers
The background property supports multiple background images or gradients separated by commas.
.layered-background {
background: url("foreground.png") center / contain no-repeat, url("texture.png") repeat, #e8f2fb;
}
Background layers are painted from front to back. The first image is closest to the viewer, and each following image is painted behind the previous one.
The background color is painted beneath all image layers and can only be specified in the final layer.
Gradient Over an Image
.hero {
background: linear-gradient(rgb(0 0 0 / 50%), rgb(0 0 0 / 50%)), url("hero.jpg") center / cover no-repeat;
}
The first layer creates a translucent dark overlay. The second layer displays the photograph underneath.
Multiple Decorative Patterns
.decorated-panel {
background: radial-gradient(circle at top left, rgb(255 255 255 / 70%), transparent 60%), repeating-linear-gradient(45deg, #d8ebfa 0, #d8ebfa 12px, #e8f2fb 12px, #e8f2fb 24px);
}
The radial gradient creates a highlight over the repeating striped gradient.
Each background layer can have its own position, size, repetition, attachment, origin, and clipping settings.
Use commas to separate complete layers, but use spaces to separate components within an individual layer.
Shorthand Reset Behavior
Like other CSS shorthand properties, background resets its associated longhand properties when they are omitted.
.panel {
background-image: url("pattern.png");
background-repeat: no-repeat;
background-size: cover;
background: #e8f2fb;
}
The final declaration resets background-image to none, background-repeat to repeat, and background-size to auto. The earlier background image is no longer displayed.
Preserving Existing Background Settings
Use a longhand property when you want to change only one component.
.panel {
background: url("pattern.png") center / cover no-repeat;
background-color: #e8f2fb;
}
Here, background-color adds a color without resetting the image, position, size, or repetition.
Declaration Order Matters
.panel {
background: #e8f2fb;
background-size: cover;
}
In this example, background-size: cover is applied after the shorthand, although it has no visible effect until a background image is specified.
When working with complex backgrounds, decide whether the shorthand or individual longhand properties make the declaration easier to understand and maintain.
The shorthand also resets background-blend-mode to its initial value of normal, even though blend modes cannot be specified directly in the background shorthand.
background Example
The following example demonstrates three background styles using the background shorthand: a solid color, a linear gradient, and a layered gradient pattern.
<div class="background-demo">
<div class="background-box solid">Solid Color</div>
<div class="background-box gradient">Linear Gradient</div>
<div class="background-box layered">Layered Background</div>
</div>
.background-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
}
.background-box {
display: flex;
align-items: center;
justify-content: center;
min-height: 150px;
padding: 15px;
box-sizing: border-box;
border: 1px solid #1769aa;
border-radius: 6px;
color: #fff;
font-weight: bold;
text-align: center;
}
.background-box.solid {
background: #1769aa;
}
.background-box.gradient {
background: linear-gradient(135deg, #1769aa, #69b7e8);
}
.background-box.layered {
background: linear-gradient(rgb(0 0 0 / 30%), rgb(0 0 0 / 30%)), repeating-linear-gradient(45deg, #1769aa 0, #1769aa 15px, #69b7e8 15px, #69b7e8 30px);
}
Each box uses the same layout styling but a different background declaration.
The first box uses a single color value. The second creates a smooth transition between two colors using linear-gradient().
The third combines a translucent dark gradient with a repeating striped gradient. Because the dark gradient is listed first, it is painted over the stripes.
All three effects are created entirely with CSS and do not require external image files.
Important Considerations
Keep the following points in mind when using background:
- Shorthand property: The property combines several background longhand properties into one declaration.
- Reset behavior: Omitted background components are reset to their initial values, including
background-blend-mode. - Default appearance: Without author styling, an element normally has a transparent background and no background image.
- Background size: When included in the shorthand, background size must follow background position and a forward slash.
- Multiple layers: Commas separate background layers, and the first image layer is painted on top.
- Background color: A background color can only be included in the final layer and is painted beneath all background images.
- Image repetition: Background images repeat by default unless a different repetition value is specified.
- Content sizing: Background images do not determine an element's intrinsic dimensions.
- Image accessibility: Important content images should use appropriate HTML elements rather than decorative CSS backgrounds.
- Text readability: Ensure sufficient contrast between foreground text and the background, especially when using photographs or gradients.
- Longhand properties: Use individual background properties when changing one component without resetting the others.
The shorthand is especially useful when defining a complete background in one place. For small changes to an existing background, longhand properties can provide greater control and reduce unintended resets.
Browser Support
The background shorthand property has Baseline Widely available status and is supported across modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.
Basic background colors, images, positioning, and repetition have long-standing browser support. More advanced features, such as certain gradient functions and newer clipping values, have their own compatibility requirements.
When using newer background features, check compatibility for the specific value or function rather than relying only on support for the shorthand.
.banner {
background: #1769aa;
background: linear-gradient(135deg, #1769aa, #69b7e8);
}
The first declaration provides a solid-color fallback. Browsers that support the gradient use the second declaration.
Visit Can I Use? to check background feature compatibility. Search for individual features when necessary.
Specifications
The background shorthand property is defined in the CSS Backgrounds and Borders Module Level 3 specification.
