W3docs

CSS background Property

CSS background is a shorthand property which allows to set all background style properties. Find a lot of examples here and try them yourself.

The CSS background property is a shorthand that lets you set every background style of an element in a single declaration instead of writing each property on its own line. Behind the scenes it expands to the following longhand properties:

Why use the shorthand

Writing one background line is shorter and easier to read than five or six separate properties, and it makes your intent obvious at a glance. There is one important side effect to be aware of: the shorthand resets every background longhand you don't mention back to its initial value. For example, background: red; also resets any previously set background-image, background-position, and so on. Because of this, declare the shorthand first and any overrides (like a separate background-size) afterward.

Ordering and special rules

The values inside background can appear in almost any order, but two rules matter:

  • background-size must follow background-position, separated by a slash (/). For example: background: url(bg.png) center / cover;center is the position and cover is the size.
  • When multiple background images are layered, background-color must come last, in the final layer only, because the color paints behind all the images.
Initial ValueSee individual properties.
Applies toAll elements. It also applies to ::first-letter.
InheritedNo.
AnimatableYes. Background-color, background-position, and background-size are animatable.
VersionCSS1+ new properties in CSS3
DOM Syntaxobject.style.background = "blue url(img.jpeg) bottom left repeat";

Syntax

background: bg-color bg-image bg-position/bg-size bg-repeat bg-origin bg-clip bg-attachment | initial | inherit;

Examples

A simple background color

The most basic use of the shorthand is to set just the background color. Any value the shorthand accepts can be omitted; the others fall back to their initial values.

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        background: #8ebf42;
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <p>Here the background color is set to green.</p>
  </body>
</html>

Result

![CSS background Property](/uploads/media/default/0001/03/5774b8353b8a0b810579a25a3cdcc2580213b1ff.png "CSS background property result" =420x)

Using a background image

You can pass a url() to the shorthand to paint an image into the background.

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        background: url("/uploads/media/default/0001/01/a1d4caa225542c577689287a36f4209808b08c19.jpeg");
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <p>Here a background image is used.</p>
  </body>
</html>

Combining several values

This example sets the color, image, repeat behavior, attachment, and position in one declaration. The image is centered, not repeated, and fixed in place while the page scrolls.

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        background: #ccc url("https://api.w3docs.com/uploads/media/default/0001/01/0710cad7a1017902166203def268a0df2a5fd545.png") no-repeat fixed center;
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <p>In this example background property specifies the background-color, the background-image property to set the image to the background, background-repeat to specify the image to be non repeated, background-attachment to specify the image to be fixed and background-position to specify the image to be in center.</p>
  </body>
</html>

Setting the background size

Because background-size cannot be combined with the rest of the shorthand without a slash, it is often cleaner to set it on its own line after the shorthand. Here cover scales the image to fill the whole element.

<!DOCTYPE html>
<html>
  <head>
    <style>
      body {
        background: #eee url("/uploads/media/default/0001/01/4982c4f43023330a662b9baed5a407e391ae6161.jpeg") no-repeat center 100px;
        background-size: cover;
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <p>Here the size for the background is set to cover.</p>
  </body>
</html>

Clipping the background

The background-clip property controls how far the background paints within the element. With padding-box, the background stops at the inside edge of the border, so it does not show through a dotted or dashed border.

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        border: 8px dotted #ccc;
        padding: 25px;
        background: #ccc;
        background-clip: padding-box;
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <div>
      <p>The background-clip for this div element is set to padding-box.</p>
    </div>
  </body>
</html>

Positioning the background origin

The background-origin property sets the area the image is positioned relative to. With padding-box (the default), the image starts from the inner edge of the border.

<!DOCTYPE html>
<html>
  <head>
    <style>
      div {
        border: 10px double #ccc;
        padding: 25px;
        background: url("https://api.w3docs.com/uploads/media/default/0001/01/0710cad7a1017902166203def268a0df2a5fd545.png");
        background-repeat: no-repeat;
        background-origin: padding-box;
      }
    </style>
  </head>
  <body>
    <h2>Background property example</h2>
    <p>Here background-origin: padding-box (default) is set.</p>
    <div></div>
  </body>
</html>

Values

ValueDescription
background-colorSets background color.
background-imageSets one or more images.
background-positionSpecifies the position of the background images.
background-sizeSets the size of the background image.
background-repeatSpecifies how to repeat the background images.
background-originSpecifies the positioning area of the background images.
background-clipSpecifies the painting area of the background images.
background-attachmentSpecifies whether the image is fixed or not.
initialSets this property to its default value.
inheritInherits this property from its parent element.

If you need finer control, reach for the individual longhand properties:

Practice

Practice
What are the properties of CSS background?
What are the properties of CSS background?
Was this page helpful?