Course contentsShow
← Web Development
Lesson 197 of 2,9823. CSS FundamentalsFree lesson

Background Shorthand

Combine all background properties into the background shorthand, understanding proper syntax order.

Background Shorthand

What you'll learn: How to combine multiple background properties into one efficient background declaration.

The Power of Shorthand

You've learned about background-color, background-image, background-repeat, background-position, background-size, and background-attachment as individual properties. The background shorthand lets you set all of these in a single line, reducing code and improving readability.

How It Works

The background shorthand follows a specific order, though most values are optional:

.element {
  background: color image repeat position / size attachment;
}

Important syntax notes:

  • A forward slash / separates position from size (e.g., center / cover)
  • Values can be omitted, and defaults will apply
  • Order matters for some values, but CSS is flexible with others

Practical Examples

/* Long way */
.hero {
  background-color: #333;
  background-image: url('banner.jpg');
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* Shorthand way */
.hero {
  background: #333 url('banner.jpg') no-repeat center / cover;
}
/* Simple color and image */
.card {
  background: white url('pattern.png');
}

/* Fixed attachment with positioning */
.parallax {
  background: url('sky.jpg') no-repeat center / cover fixed;
}

Think of It Like Ordering Coffee

Instead of saying "medium, hot, latte, with oat milk, extra shot," you give all the details at once. Same efficiency with background!

Key Takeaway: The background shorthand combines color, image, repeat, position, size, and attachment in one line, with the crucial / between position and size when both are specified.