Skip to content

CSS3 Backgrounds

CSS provides powerful properties to control the background of elements, going beyond simple colors. You can use images, control their size and position, and even layer multiple backgrounds.

Key background properties covered in this chapter:

  • Multiple Background Images
  • background-size: Control the dimensions of background images.
  • background-origin: Define the starting position area for a background image.
  • background-clip: Determine how far the background (color or image) extends.

These advanced background properties are well-supported in all modern browsers.

PropertyChromeEdgeFirefoxSafariOpera
Multiple background imagesChrome 1+Edge 12+Firefox 3.6+Safari 3.2+Opera 10.5+
background-sizeChrome 4+Edge 12+Firefox 4+Safari 4.1+Opera 10.5+
background-originChrome 1+Edge 12+Firefox 4+Safari 3.2+Opera 10.5+
background-clipChrome 1+Edge 12+Firefox 4+Safari 3.2+Opera 10.5+

Note: Vendor prefixes (-webkit-, -moz-, -o-) are generally no longer needed for these properties.

CSS allows you to layer multiple background images on a single element using the background-image property. Separate the url() values with commas.

The images are stacked with the first image specified appearing on top (closest to the viewer), the second image behind it, and so on.

You can control the position, repetition, size, etc., of each image individually by providing comma-separated values for the corresponding background properties (background-position, background-repeat, background-size, etc.).

Example: A small logo on top of a repeating pattern background:

.container {
/* Layer images: logo first (top), pattern second (bottom) */
background-image: url('logo.png'), url('pattern.gif');
/* Control each image individually */
background-position: center center, top left;
background-repeat: no-repeat, repeat;
background-size: 50px auto, auto; /* Size the logo, pattern uses default size */
/* Ensure container has dimensions */
width: 300px;
height: 200px;
border: 1px solid #ccc;
}

You can also use the background shorthand property. Each layer is defined sequentially, separated by commas:

.container-shorthand {
background:
/* Layer 1: Logo */
url('logo.png') center center / 50px auto no-repeat,
/* Layer 2: Pattern */
url('pattern.gif') top left / auto repeat;
width: 300px;
height: 200px;
border: 1px solid #ccc;
}

The background-size property allows you to specify the size of background images, overriding their intrinsic dimensions.

You can use:

  • Length values: (e.g., px, em). background-size: 100px 50px; (width height). If only one value is given, the second defaults to auto.
  • Percentage values: Relative to the background positioning area (defined by background-origin). background-size: 50% auto;.
  • auto: Scales the image while preserving its aspect ratio.
  • cover: Scales the image (preserving aspect ratio) to be as large as possible to completely cover the background area. Some parts of the image may be cropped.
  • contain: Scales the image (preserving aspect ratio) to be as large as possible while ensuring the entire image fits within the background area. Some parts of the background area may remain empty.

Example resizing an image using pixels:

.resized-bg {
background-image: url('image.jpg');
background-repeat: no-repeat;
background-size: 200px 150px; /* Fixed size */
width: 300px;
height: 200px;
border: 1px solid #ccc;
}

Example using contain and cover:

.bg-contain {
background-image: url('wide-image.jpg');
background-repeat: no-repeat;
background-size: contain; /* Fits entire image inside */
background-color: lightblue; /* Shows where image doesn't cover */
width: 200px;
height: 200px;
border: 1px solid #ccc;
margin-bottom: 10px;
}
.bg-cover {
background-image: url('wide-image.jpg');
background-repeat: no-repeat;
background-size: cover; /* Covers the whole area, might crop image */
background-position: center center; /* Center the cropping */
width: 200px;
height: 200px;
border: 1px solid #ccc;
}

When using multiple backgrounds, provide comma-separated values for background-size corresponding to each image in background-image.

A common use case for background-size is creating a background image that covers the entire browser viewport.

Requirements:

  • Cover the entire page.
  • Scale the image proportionally.
  • Center the image.
  • Avoid causing scrollbars due to the image itself.
  • Image should remain fixed when scrolling (optional, using background-attachment: fixed).

A common technique involves styling the html or body element:

html {
/* Set background on html for full height */
background: url('background.jpg') no-repeat center center fixed;
/* Crucial part: Scale to cover */
background-size: cover;
/* Ensure html takes full height */
min-height: 100%;
}
body {
/* Reset default body margin */
margin: 0;
/* Ensure body can also span height if needed */
min-height: 100%;
}

Note: Setting background on html vs body can have subtle differences. Using html often works well for fixed backgrounds. Ensure body margin is removed.

The background-origin property determines the background positioning area. It specifies where the background-position property’s origin (0% 0%) is located.

  • padding-box (default): Origin is the top-left corner of the padding edge.
  • border-box: Origin is the top-left corner of the border edge.
  • content-box: Origin is the top-left corner of the content edge.

This affects where the background image starts being drawn from when using background-position.

Example: Positioning a small background image relative to the content box:

.origin-content {
border: 10px dashed gray;
padding: 25px;
width: 200px;
height: 100px;
background-image: url('icon.png');
background-repeat: no-repeat;
background-position: top left; /* Position relative to origin */
/* Set origin to content box */
background-origin: content-box;
background-color: lightyellow; /* Show padding/border areas */
}

The background-clip property determines how far the background (color or image) extends outward from the content.

  • border-box (default): Background extends to the outside edge of the border.
  • padding-box: Background extends to the outside edge of the padding (stops at the border).
  • content-box: Background is clipped to the content box (only painted within the content area).

This is useful for creating effects where the border appears separate from the background, or constraining the background visually.

Example: Clipping the background to the content box, leaving padding and border areas transparent (or showing underlying backgrounds):

.clip-content {
border: 10px dotted blue;
padding: 25px;
width: 200px;
height: 100px;
background-color: lightcoral;
/* Clip background to content area */
background-clip: content-box;
}

Practical Use: background-clip: padding-box; is often used on buttons or containers where you want the border to look distinct from the inner background fill.

PropertyDescription
background-imageSpecifies one or more background images for an element (comma-separated).
background-sizeSpecifies the size of the background image(s). Keywords cover and contain are particularly useful.
background-originSpecifies the positioning area for the background image(s) (padding-box, border-box, content-box).
background-clipSpecifies the painting area of the background (border-box, padding-box, content-box).
background (shorthand)Sets multiple background properties in one declaration. Comma-separate layers for multiple backgrounds.

Further Reading: MDN Backgrounds and Borders