docs.rodeo

MDN Web Docs mirror

background-image

{{CSSRef}} 

The background-image CSS property sets one or more background images on an element.

{{EmbedInteractiveExample("pages/css/background-image.html")}} 

The background images are drawn on stacking context layers on top of each other. The first layer specified is drawn as if it is closest to the user.

The borders of the element are then drawn on top of them, and the {{cssxref("background-color")}}  is drawn beneath them. How the images are drawn relative to the box and its borders is defined by the {{cssxref("background-clip")}}  and {{cssxref("background-origin")}}  CSS properties.

If a specified image cannot be drawn (for example, when the file denoted by the specified URI cannot be loaded), browsers handle it as they would a none value.

[!NOTE] Even if the images are opaque and the color won’t be displayed in normal circumstances, web developers should always specify a {{cssxref("background-color")}} . If the images cannot be loaded—for instance, when the network is down—the background color will be used as a fallback.

Syntax

/* single image */
background-image: linear-gradient(black, white);
background-image: url("cat-front.png");

/* multiple images */
background-image:
  radial-gradient(circle, #0000 45%, #000f 48%),
  radial-gradient(ellipse farthest-corner, #fc1c14 20%, #cf15cf 80%);

/* Global values */
background-image: inherit;
background-image: initial;
background-image: revert;
background-image: revert-layer;
background-image: unset;

Each background image is specified either as the keyword none or as an {{cssxref("<image>")}}  value.

To specify multiple background images, supply multiple values, separated by a comma.

Values

Accessibility

Browsers do not provide any special information on background images to assistive technology. This is important primarily for screen readers, as a screen reader will not announce its presence and therefore convey nothing to its users. If the image contains information critical to understanding the page’s overall purpose, it is better to describe it semantically in the document.

Additionally, it is important to ensure that the contrast ratio between the background image and the foreground text is high enough that people with low vision can read the page content.

Color contrast ratio is determined by comparing the luminance of the text and background color values. To meet Web Content Accessibility Guidelines (WCAG), a ratio of 4.5:1 is required for body text content and 3:1 for larger text such as headings. Large text is defined as 24px or larger, or bolded 18.66px or larger.

Formal definition

{{cssinfo}} 

Formal syntax

{{csssyntax}} 

Examples

Layering background images

Note that the star image is partially transparent and is layered over the cat image.

HTML

<div>
  <p class="cats-and-stars">This paragraph is full of cats<br />and stars.</p>
  <p>This paragraph is not.</p>
  <p class="cats-and-stars">Here are more cats for you.<br />Look at them!</p>
  <p>And no more.</p>
</div>

CSS

p {
  font-weight: bold;
  font-size: 1.5em;
  color: white;
  text-shadow: 0.07em 0.07em 0.05em black;
  background-image: none;
  background-color: transparent;
}

div {
  background-image: url("mdn_logo_only_color.png");
}

.cats-and-stars {
  background-image: url("star-transparent.gif"), url("cat-front.png");
  background-color: transparent;
}

Result

{{EmbedLiveSample('Layering_background_images')}} 

Specifications

{{Specifications}} 

Browser compatibility

{{Compat}} 

See also

In this article

View on MDN