← Back to CSS Course | Chapter 2: Colors & Backgrounds | Lesson 11 of 21

CSS Background Shorthand

The background shorthand lets you set color, image, repeat, position, size, and attachment all in one line instead of six separate declarations.
Syntax
css
selector {
  background: color image repeat attachment position / size;
}

Shorthand Syntax and Property Order

The background shorthand accepts: background-color background-image background-position/background-size background-repeat background-attachment background-origin background-clip, in roughly that order, with position and size separated by a forward slash when both are given.

Any longhand not included in the shorthand is reset to its initial value, not left alone.

Note:
  • Accepted values:
  • background — combined value, e.g. #eee url(bg.png) no-repeat center / cover
  • color — any CSS color
  • image — url(), a gradient, or none
  • position / size — written as position / size
  • repeat — repeat, repeat-x, repeat-y, no-repeat, space, round
  • attachment — scroll, fixed, local
  • origin and clip — border-box, padding-box or content-box
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Shorthand Syntax and Property Order

css
<style>
.box {
  height: 150px;
  background: #fff url('https://placehold.co/200x100') center/cover no-repeat fixed;
}
</style>
<div class="box">color, image, position/size, repeat, attachment — all in one shorthand</div>

Why Order and the Slash Matter

position must come before size, separated by /, because the parser needs to distinguish two length values meant as a position from two meant as a size -- background: url(x.jpg) center/cover no-repeat sets position to center and size to cover, while swapping their order is invalid.

Note:
  • Accepted values:
  • color — e.g. #eee
  • image — url() or a gradient
  • position / size — e.g. center / cover
  • repeat — e.g. no-repeat
  • attachment — scroll, fixed or local
  • origin and clip — border-box, padding-box or content-box
  • inherit | initial | unset | revert — CSS-wide keywords accepted by every property

Example: Why Order and the Slash Matter

css
<style>
.box {
  height: 150px;
  background: url('https://placehold.co/200x100') center/cover no-repeat;
}
</style>
<div class="box">Position comes before the slash, size after</div>

Shorthand Resets Unspecified Longhands

Writing background: red only sets background-color and silently resets background-image, background-repeat, background-position, and every other background-* property to their defaults -- this is the most common shorthand gotcha, accidentally wiping out an image set elsewhere.

Example: Shorthand Resets Unspecified Longhands

css
<style>
.box {
  background-image: linear-gradient(135deg, #1e88e5, #8e24aa);
  height: 60px;
}
.reset {
  background-image: linear-gradient(135deg, #1e88e5, #8e24aa);
  background: red;
  height: 60px;
}
</style>
<div class="box">Keeps its gradient image</div>
<div class="reset">Shorthand silently wiped the image, only red remains</div>

Partial Shorthand Is Still Valid

You don't have to include every value; background: url('bg.jpg') no-repeat center is perfectly valid and only touches image, repeat, and position, but still resets color/size/attachment/origin/clip to initial since they weren't mentioned.

Example: Partial Shorthand Is Still Valid

css
<style>
.box {
  height: 150px;
  background: url('https://placehold.co/200x100') no-repeat center;
}
</style>
<div class="box">Only image, repeat, and position specified — rest reset to defaults</div>

When to Use Shorthand vs Longhand

Shorthand is more compact for a fully-specified background but longhand is safer when only tweaking one aspect of an existing background (e.g. changing just background-position in a media query) since it won't accidentally reset the others.

Example: When to Use Shorthand vs Longhand

css
<style>
.full {
  background: #333 linear-gradient(135deg, rgba(255,255,255,.2), transparent) center/cover no-repeat;
  height: 80px;
  color: #fff;
  padding: 8px;
}
@media (max-width: 600px) {
  .full {
    background-position: top;
  }
}
</style>
<div class="full">Longhand tweak in the media query, shorthand elsewhere</div>
Live Example
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Writing the shorthand in the wrong order, such as putting size before position without the /, which is invalid.
  2. Forgetting that the shorthand resets values not written, so background: red clears an earlier background-image.
  3. Writing background-size without a slash after position, as in center cover, instead of center / cover.
Browser Support

Supported in all modern browsers: Chrome, Firefox, Safari, Edge and Opera.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.