CSS background Property

Beginner
7 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The background property is a shorthand that paints the area behind an element's content. In one line you can set background-color, background-image, background-position, background-size, background-repeat, background-attachment, background-origin, and background-clip.

List layers with commas. The first layer is on top. The color may appear only on the last layer. Any longhand you omit is reset to its initial value for that layer. The shorthand also resets background-blend-mode. It applies to every element, including ::first-letter and ::first-line, and it is not inherited.

Standard-track.

Not deprecated and not experimental. Baseline widely available. Prefer longhand when you need to change one piece alone, such as only the size in a media query.

CSS Syntax

syntax.css
background: <color> | <image> <position> / <size> <repeat> <attachment> | initial | inherit;

Recommended order for one layer: image, position / size, repeat, attachment, then color last. Size is valid only after position with a slash.

Property Values

ValueDescriptionTry it
<color>Solid fill. Example: background: #dbeafe;Try it
<image>A url() or gradient. Painted above the colorTry it
<position> / <size>Place then scale. Example: center / coverTry it
<repeat>Tile or not. Example: no-repeat, repeat-xTry it
<attachment>scroll (default), fixed, or localTry it
<visual-box>One value sets origin and clip. Two values: origin then clipTry it
layer, layerComma-separated layers. First is on top. Color only on the lastTry it
initialResets every background longhand to its initial valueTry it
inheritCopies this property from the parent elementTry it

How to Use background

Pick the element, give it a visible box, then write one shorthand line for the paint you need.

  1. Set background on the element that should show the paint. It is not inherited.
  2. Give that element a size when you use an image. Content or a height is enough.
  3. Write color only, or image with position / size and repeat. Put size after the slash.
  4. For stacked paint, separate layers with commas. Keep the color on the last layer.
PieceRequired?Role
ElementYesThe box that receives the paint
A visible areaFor imagesContent or a height so the image has somewhere to show
Color and/or imageUsuallyWhat you see. Gradients count as images
position / sizeNoWhere the image sits and how large it is
Longhand laterOptionalChange one piece after the shorthand without resetting the rest

Live Preview

One box. Change the background shorthand and watch the paint behind the label.

The label stays in the content. Only the shorthand paint behind it changes.

Background
Solid fill

background: #dbeafe

CSS Examples

Each snippet adds only what background needs: a box, and one shorthand line. Use Try It Yourself for a live editor.

Example 1 — Solid Color

Fill the box with one color. No image, no slash syntax.

CSS
<style>
  div {
    padding: 16px;
    background: #dbeafe;
  }
</style>

<div>Solid fill</div>
Try It Yourself

How It Works

  1. One value is enough. background: #dbeafe; sets the color and resets the other background longhands for that layer.
  2. No image is painted. After the shorthand, background-image is none, so you see only the solid fill.
  3. Padding shows the area. The words sit on top of the color. The paint is behind the content, not the text color.
  4. Same as the color longhand. For color alone, background-color: #dbeafe is equivalent and does not reset the image if one was set earlier.

Example 2 — Center / Cover

Place a gradient in the middle and scale it to cover the box. A dark color sits underneath.

CSS
<style>
  div {
    height: 140px;
    padding: 16px;
    color: #fff;
    background: #0f172a linear-gradient(135deg, #2563eb, #7c3aed) no-repeat center / cover;
  }
</style>

<div>Hero fill</div>
Try It Yourself

How It Works

  1. Color first as a fallback. #0f172a fills the box if the image fails, and it sits under the gradient.
  2. The gradient is the image. linear-gradient(135deg, #2563eb, #7c3aed) is a background-image value, not a color.
  3. Slash separates size. center / cover means position the image in the middle, then scale it with cover so it fills the box.
  4. No tiling. no-repeat shows the image once. Without it, a smaller image would tile by default.

Example 3 — Two Layers

Stack a dark tint over a color fade. The color belongs on the last layer only.

CSS
<style>
  div {
    height: 140px;
    padding: 16px;
    color: #fff;
    background:
      linear-gradient(rgba(15, 23, 42, 0.55), rgba(15, 23, 42, 0.55)),
      linear-gradient(135deg, #0ea5e9, #6366f1) #0f172a;
  }
</style>

<div>Tint on top</div>
Try It Yourself

How It Works

  1. Comma means two layers. The first gradient is painted on top. The second gradient is behind it.
  2. The tint lets the fade show. rgba(15, 23, 42, 0.55) is translucent, so you still see the lower layer.
  3. Color only on the last layer. #0f172a sits with the bottom gradient. Putting a color on an earlier layer is invalid.
  4. Order matters. Reverse the list and an opaque bottom layer can hide what you meant to show on top.

Edge Cases & Pitfalls

If the paint disappears, resets, or sits wrong, one of these is usually why.

reset

Omitted values are cleared

background: #fff; sets the color and resets image, position, size, and the rest to their initials. It also resets background-blend-mode. Set longhand after the shorthand when you need to keep earlier pieces.

slash

Size needs position / size

Write center / cover, not cover alone in the shorthand. Without the slash after a position, size is not accepted there.

color

Color only on the last layer

In a comma list, put the color with the final layer. The first layer is on top; the color is always the bottom paint.

a11y

Screen readers skip background images

Assistive technology is not told about background images. Put meaning in an img with alt. Keep text contrast at least 4.5:1 against the paint.

body

Document background often comes from body

If html is transparent with no image, browsers promote the body background to the canvas. Prefer setting the page background on body. Containment on html or body can stop that promotion.

Browser Support

background is a browser painting property. Node.js, Deno, and Bun do not apply it. Basic color and single-image use is older than multiple layers.

✓ Baseline · Widely available since 2015

background

Basic shorthand: every current browser and Internet Explorer. Multiple layers: Chrome 4+, Firefox 3.6+, Safari 3.1+, Edge 12+, Opera 11.5+, Internet Explorer 9+. background-size in the slash form needs modern engines.

98% Shorthand and layers
Google Chrome 1+ basic · 4+ layers
Full support
Mozilla Firefox 1+ basic · 3.6+ layers
Full support
Apple Safari 1+ basic · 3.1+ layers
Full support
Microsoft Edge 12+
Full support
Opera 3.5+ basic · 11.5+ layers
Full support
Samsung Internet 4+
Full support
Internet Explorer 4+ basic · 9+ layers
Partial
background Widely supported

Bottom line: Safe for color, single images, position / size, and multiple layers in current browsers. Internet Explorer 8 and older lack multiple backgrounds. Prefer longhand when you need to change one value without resetting the rest.

Key Takeaways

  • Role: one line for color, image, position, size, repeat, and related longhands behind the content.
  • Slash rule: size comes after position as center / cover. Color belongs only on the last layer.
  • Reset: omitted longhands, and background-blend-mode, return to their initial values.
  • Meaning: screen readers ignore background images. Keep a fallback color and strong text contrast.

One line: background paints the box. Use longhand when you must change only one piece without clearing the others.

Frequently Asked Questions

It is a shorthand that sets the background longhands in one declaration: color, image, position, size, repeat, attachment, origin, and clip. Values you omit are reset to their initial values for that layer.
With nothing set, the paint is transparent. The longhands start as background-image: none, background-color: transparent, background-repeat: repeat, background-position: 0% 0%, background-size: auto, background-attachment: scroll, background-origin: padding-box, and background-clip: border-box.
background-size and background-position share value types. Size is only allowed immediately after position, separated by a slash, such as center / cover. Without the slash, the browser cannot tell which value is which.
Only on the last layer in the comma list. The first layer is painted on top, closest to the reader. The color sits behind every image layer.
Yes. The shorthand also resets background-blend-mode to its initial value, even though blend-mode is not written in the shorthand. Set blend-mode after the shorthand if you still need it.
It is Baseline and widely available. Basic use works in every current browser and in Internet Explorer. Multiple layers need Internet Explorer 9+, Firefox 3.6+, or Opera 11.5+. It is a browser painting property. Node.js, Deno, and Bun do not apply it.

Next: background-attachment

Learn whether the background scrolls with the page, the element, or stays fixed.

background-attachment tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful