CSS background-image Property

The background-image property specifies one or more images to display behind an element's content.

Background images can be external image files, CSS-generated gradients, or multiple overlapping layers. They are commonly used for decorative patterns, banners, textured sections, and visual effects without adding extra HTML elements.

background-image Syntax

selector {
  background-image: url("background.jpg");
}

The property accepts an image value, the keyword none, or a comma-separated list of image values.

.no-image {
  background-image: none;
}
.external-image {
  background-image: url("background.jpg");
}
.linear-background {
  background-image: linear-gradient(90deg, #1769aa, #69b7e8);
}
.radial-background {
  background-image: radial-gradient(circle, #fff, #1769aa);
}
.conic-background {
  background-image: conic-gradient(#1769aa, #69b7e8, #1769aa);
}
.layered-background {
  background-image: linear-gradient(rgb(0 0 0 / 40%), rgb(0 0 0 / 40%)), url("background.jpg");
}

The property also accepts CSS-wide values such as inherit, initial, revert, revert-layer, and unset.

Property Values

Value Description
none Displays no background image. This is the initial value.
url() References an external or embedded image resource.
linear-gradient() Creates a gradient that transitions between colors along a straight line.
radial-gradient() Creates a gradient that transitions outward from a center or specified origin.
conic-gradient() Creates a gradient with colors arranged around a center point.
repeating-linear-gradient() Creates a repeating linear gradient pattern.
repeating-radial-gradient() Creates a repeating radial gradient pattern.
repeating-conic-gradient() Creates a repeating conic gradient pattern.
image-set() Provides alternative image resources that the browser can select according to supported image types and resolution.
<image>, <image> Specifies multiple background image layers separated by commas.

The property applies to all elements, is not inherited, and has an initial value of none. Its computed value is a list of computed image values, and its animation behavior depends on the image types involved.

CSS gradients are treated as images, even though they are generated directly by the browser instead of being loaded from image files.

How background-image Works

The background-image property paints an image behind an element's content, within the area established by the other background properties.

.panel {
  min-height: 200px;
  background-image: url("background.jpg");
}

By default, the background image begins at the top-left of its positioning area and repeats horizontally and vertically.

The image does not change the element's intrinsic dimensions. If the element has no content or specified size, adding a background image alone does not give it the dimensions of that image.

Background Painting Order

Background images appear behind the element's foreground content. When multiple images are specified, the first image is painted above subsequent images.

A background color, when present, is painted beneath all background image layers.

Background Images Are Decorative

Unlike the HTML <img> element, a CSS background image does not provide alternative text or participate in document content as an image element.

Use CSS backgrounds for decorative imagery. Use HTML image elements when the image communicates information that users need to understand.

Using Image URLs

The url() function references an image resource that the browser can display as a background.

.photo-background {
  background-image: url("images/landscape.jpg");
}

When CSS is stored in an external stylesheet, relative image paths are resolved relative to the stylesheet's location.

Relative and Root-Relative Paths

.relative-path {
  background-image: url("../images/banner.jpg");
}
.root-relative-path {
  background-image: url("/images/banner.jpg");
}

A relative path is resolved from the stylesheet's location. A root-relative path begins at the website's root directory.

Multiple Image Formats

Background images can use supported formats such as JPEG, PNG, WebP, AVIF, and SVG.

.jpeg-background {
  background-image: url("photo.jpg");
}
.webp-background {
  background-image: url("photo.webp");
}
.svg-background {
  background-image: url("pattern.svg");
}

Choose image formats based on the content, transparency requirements, file size, and browser compatibility.

Using image-set()

The image-set() function provides multiple image resources for the browser to choose from.

.responsive-image {
  background-image: image-set(url("background-1x.png") 1x, url("background-2x.png") 2x);
}

The browser can select an appropriate image based on device resolution and other supported selection criteria.

For more advanced cases, image-set() can also specify image types to help the browser select a supported format.

Linear Gradients

The linear-gradient() function creates a smooth transition between two or more colors along a straight line.

.linear-panel {
  background-image: linear-gradient(90deg, #1769aa, #69b7e8);
}

The angle determines the direction of the gradient. A value of 90deg creates a transition from left to right.

Using Direction Keywords

.horizontal-gradient {
  background-image: linear-gradient(to right, #1769aa, #69b7e8);
}
.vertical-gradient {
  background-image: linear-gradient(to bottom, #1769aa, #69b7e8);
}
.diagonal-gradient {
  background-image: linear-gradient(to bottom right, #1769aa, #69b7e8);
}

Multiple Color Stops

.multi-color-gradient {
  background-image: linear-gradient(90deg, #1769aa 0%, #69b7e8 50%, #e8f2fb 100%);
}

Color stops determine where colors appear along the gradient line. Additional stops provide more control over transitions.

Sharp Color Transitions

.split-background {
  background-image: linear-gradient(90deg, #1769aa 0 50%, #69b7e8 50% 100%);
}

Using adjacent stops at the same position creates a sharp transition instead of a gradual blend.

Linear gradients are useful for banners, cards, buttons, section backgrounds, and decorative accents.

Radial Gradients

The radial-gradient() function creates a gradient that transitions outward from a center or specified origin.

.radial-panel {
  background-image: radial-gradient(circle, #fff, #1769aa);
}

The circle keyword creates a circular gradient. Without an explicit shape, the default is an ellipse.

Positioning the Gradient

.top-left-highlight {
  background-image: radial-gradient(circle at top left, #fff, #1769aa);
}

The at keyword sets the center of the radial gradient.

Multiple Color Stops

.radial-multi-color {
  background-image: radial-gradient(circle, #fff 0%, #69b7e8 45%, #1769aa 100%);
}

Radial gradients are useful for highlights, spotlight effects, soft backgrounds, and circular color transitions.

Conic Gradients

The conic-gradient() function creates a gradient whose colors transition around a central point.

.conic-panel {
  background-image: conic-gradient(#1769aa, #69b7e8, #1769aa);
}

Unlike linear and radial gradients, conic gradients change color as the angle around the center changes.

Setting the Starting Angle

.rotated-conic {
  background-image: conic-gradient(from 90deg, #1769aa, #69b7e8, #1769aa);
}

Creating Color Segments

.segmented-conic {
  background-image: conic-gradient(#1769aa 0 25%, #69b7e8 25% 50%, #e8f2fb 50% 75%, #173b5e 75% 100%);
}

Explicit color-stop positions can create sharp segments instead of smooth transitions.

Conic gradients are useful for decorative circular patterns, color wheels, and chart-like visual effects. Data charts should still provide accessible text or data alternatives.

Repeating Gradients

CSS provides repeating versions of linear, radial, and conic gradients for creating patterns.

Repeating Linear Gradients

.striped-background {
  background-image: repeating-linear-gradient(45deg, #e8f2fb 0 15px, #69b7e8 15px 30px);
}

This creates diagonal stripes that repeat across the background.

Repeating Radial Gradients

.circular-pattern {
  background-image: repeating-radial-gradient(circle, #e8f2fb 0 12px, #69b7e8 12px 24px);
}

The colors repeat outward from the center, producing concentric rings.

Repeating Conic Gradients

.angular-pattern {
  background-image: repeating-conic-gradient(#1769aa 0deg 15deg, #69b7e8 15deg 30deg);
}

This produces repeating angular color segments around the gradient center.

Repeating gradients can replace small pattern images and are especially useful for stripes, grids, rings, and other geometric decorations.

Multiple Background Images

The background-image property accepts multiple images separated by commas.

.layered-background {
  background-image: url("foreground.png"), url("texture.png"), url("photo.jpg");
}

The first image appears on top, the second behind it, and the third at the bottom of the image stack.

Gradient Overlay

.hero {
  background-image: linear-gradient(rgb(0 0 0 / 50%), rgb(0 0 0 / 50%)), url("hero.jpg");
  background-position: center, center;
  background-size: cover, cover;
  background-repeat: no-repeat, no-repeat;
}

The semi-transparent gradient darkens the image beneath it, helping foreground text stand out.

Layered Decorative Gradients

.decorative-panel {
  background-image: radial-gradient(circle at top left, rgb(255 255 255 / 75%), transparent 60%), linear-gradient(135deg, #1769aa, #69b7e8);
}

The radial gradient creates a highlight over the linear gradient.

Using none in a Layer List

.optional-layer {
  background-image: none, url("pattern.png");
}

The none keyword creates an image layer with no visible image. It can be useful when coordinating lists of background properties.

Other background properties, including position, size, repetition, attachment, origin, and clipping, can use corresponding comma-separated values for individual layers.

Position, Size, and Repetition

The background-image property selects the image, while other background properties control its placement and appearance.

Background Position

.positioned-image {
  background-image: url("photo.jpg");
  background-position: center;
}

The background-position property determines where the image is placed within its positioning area.

Background Size

.cover-image {
  background-image: url("photo.jpg");
  background-size: cover;
}

The cover keyword scales the image proportionally until it covers the background positioning area. Some parts may be cropped.

.contain-image {
  background-image: url("photo.jpg");
  background-size: contain;
}

The contain keyword scales the image proportionally so it fits entirely within the positioning area.

Background Repetition

.single-image {
  background-image: url("logo.png");
  background-repeat: no-repeat;
}

Background images repeat in both directions by default. Use no-repeat to display one copy.

Combining the Properties

.banner {
  min-height: 300px;
  background-image: url("banner.jpg");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

This combination is commonly used for banners and full-width decorative sections.

Background Images and Colors

A background color can be used beneath an image to provide a fallback or fill transparent areas.

.image-panel {
  background-color: #173b5e;
  background-image: url("background.jpg");
}

If the image cannot be loaded, the background color remains visible.

Transparent Image Areas

.pattern-panel {
  background-color: #e8f2fb;
  background-image: url("transparent-pattern.png");
}

The background color shows through transparent areas of the image.

Using a Gradient as an Image

.gradient-panel {
  background-color: #1769aa;
  background-image: linear-gradient(135deg, #1769aa, #69b7e8);
}

The gradient is treated as an image and appears above the solid background color.

Gradients cannot be assigned directly to background-color. They must be specified through background-image or the background shorthand.

Using the background Shorthand

The background-image property can be combined with other background settings through the background shorthand.

.hero {
  background: #173b5e url("hero.jpg") center / cover no-repeat;
}

This declaration sets the background color, image, position, size, and repetition.

Using a Gradient

.gradient-banner {
  background: linear-gradient(135deg, #1769aa, #69b7e8);
}

Multiple Background Layers

.layered-banner {
  background: linear-gradient(rgb(0 0 0 / 35%), rgb(0 0 0 / 35%)), url("hero.jpg") center / cover no-repeat;
}

Shorthand Reset Behavior

.panel {
  background-image: url("pattern.png");
  background: #e8f2fb;
}

The final shorthand declaration resets background-image to none, removing the previously specified image.

To change only the image while preserving other background settings, use the background-image longhand.

.panel {
  background: #e8f2fb url("pattern-one.png") repeat;
  background-image: url("pattern-two.png");
}

The second declaration replaces the image without resetting the color or repetition settings.

background-image Example

The following example demonstrates four background images created entirely with CSS: a linear gradient, a radial gradient, a repeating pattern, and multiple layered gradients.

<div class="background-image-demo">
  <div class="image-box linear">Linear Gradient</div>
  <div class="image-box radial">Radial Gradient</div>
  <div class="image-box repeating">Repeating Gradient</div>
  <div class="image-box layered">Layered Gradients</div>
</div>
.background-image-demo {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 15px;
}
.image-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;
  text-shadow: 0 1px 3px rgb(0 0 0 / 50%);
}
.image-box.linear {
  background-image: linear-gradient(135deg, #1769aa, #69b7e8);
}
.image-box.radial {
  background-image: radial-gradient(circle, #69b7e8, #173b5e);
}
.image-box.repeating {
  background-image: repeating-linear-gradient(45deg, #1769aa 0 15px, #69b7e8 15px 30px);
}
.image-box.layered {
  background-image: radial-gradient(circle at top left, rgb(255 255 255 / 35%), transparent 65%), linear-gradient(135deg, #173b5e, #1769aa);
}

Each box uses the same layout styling but a different background-image declaration.

Browser Result
Linear Gradient
Radial Gradient
Repeating Gradient
Layered Gradients

The linear gradient creates a diagonal color transition. The radial gradient transitions outward from the center.

The repeating gradient produces diagonal stripes, while the layered example places a translucent radial highlight over a linear gradient.

All four backgrounds are generated by the browser without requiring external image files.

Accessibility and Performance

Background images can improve visual design, but they should not be required for understanding important content.

Decorative vs. Informative Images

CSS background images do not have an HTML alt attribute and are not a substitute for meaningful content images.

Use an HTML <img> element with appropriate alternative text when the image communicates important information.

Text Contrast

When placing text over a background image, ensure sufficient contrast across all parts of the image.

.hero {
  color: #fff;
  background-image: linear-gradient(rgb(0 0 0 / 60%), rgb(0 0 0 / 60%)), url("hero.jpg");
  background-position: center;
  background-size: cover;
}

The translucent dark gradient helps improve text readability over the photograph.

Image Performance

Large background images can increase download size and rendering costs. Choose appropriate image dimensions and modern compressed formats when practical.

CSS-generated gradients are often useful alternatives to small decorative image files.

Fallback Backgrounds

.banner {
  background-color: #173b5e;
  background-image: url("banner.jpg");
}

A solid background color provides a useful fallback when an image is unavailable or fails to load.

Important Considerations

Keep the following points in mind when using background-image:

  • Default value: The initial value is none.
  • Image sources: Background images can use external image resources or CSS-generated gradients.
  • Multiple layers: Commas separate background image layers, with the first image painted on top.
  • Background color: A background color is painted beneath all background image layers.
  • Default repetition: Background images repeat horizontally and vertically unless another repetition value is specified.
  • Positioning: Use background-position to control image placement.
  • Sizing: Use background-size to control image dimensions, including cover and contain.
  • Image paths: Relative URLs in external stylesheets are resolved relative to the stylesheet's location.
  • Layout: Background images do not establish an element's intrinsic dimensions.
  • Transparency: Transparent portions of an image reveal lower background layers or the background color.
  • Shorthand resets: The background shorthand resets the image to none when no image is specified.
  • Accessibility: Important images should use suitable HTML elements and alternative text rather than CSS backgrounds.
  • Performance: Optimize external image resources and avoid unnecessarily large background files.
  • Browser support: Newer image functions and formats have their own compatibility requirements.

Use background-image when adding or replacing decorative background imagery without changing other background properties.

Browser Support

The background-image property has Baseline Widely available status and is supported across modern versions of Chrome, Edge, Firefox, Safari, and other major browsers.

Basic image URLs and established gradient functions have broad browser support. Newer image functions, image formats, and advanced gradient features may have additional compatibility requirements.

Providing Fallbacks

.banner {
  background-color: #1769aa;
  background-image: url("banner.jpg");
}

The background color remains available when the image cannot be displayed.

For newer image functions, use a compatible image declaration before the newer declaration.

.responsive-banner {
  background-image: url("banner.png");
  background-image: image-set(url("banner-1x.png") 1x, url("banner-2x.png") 2x);
}

Browsers that do not support image-set() can retain the earlier image declaration.

Visit Can I Use? to check background feature compatibility. Search separately for individual gradient functions, image formats, or image-set() when needed.

Specifications

The background-image property is defined in the CSS Backgrounds and Borders Module Level 3 specification. Image functions and gradients are defined in the CSS Images specifications.

CSS Backgrounds and Borders Module Level 3

CSS Images Module Level 3

CSS Images Module Level 4