CSS background Property

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 4 Examples
Backgrounds & Layout

What You’ll Learn

The background property is a shorthand that sets color, images, position, size, repeat, and more in one line. It is one of the most useful shortcuts in CSS for styling element backgrounds quickly.

01

Shorthand

Many values, one line.

02

Color

Solid fill backgrounds.

03

Images

Photos and gradients.

04

Position & Size

center / cover syntax.

05

Repeat

Tile or no-repeat.

06

Longhand

Individual properties.

Definition and Usage

The background CSS shorthand property sets all background-related longhand properties at once. Instead of writing six or more separate declarations, you can combine them into a single rule.

This is especially helpful for hero banners, cards, and sections where you need a background color, an image or gradient, centered placement, and cover sizing together.

💡
Beginner Tip

When size is included in the shorthand, it must come after position with a slash: background: ... center / cover;

📝 Syntax

The background shorthand accepts multiple values in a specific order:

syntax.css
selector {
  background: [color] [image] [repeat] [attachment] [position] / [size] [origin] [clip];
}

Basic Example

background.css
.hero {
  background: #fff7ed linear-gradient(135deg, #2563eb, #7c3aed) no-repeat center / cover;
}

Syntax Rules

  • Omitted values reset to their initial state for that layer.
  • background-size must follow background-position with a slash (/).
  • You can set only a color: background: #eff6ff;
  • Gradients count as background-image values.
  • Use longhand properties when you need to update one value independently.

⚡ Quick Reference

QuestionAnswer
Initial backgroundTransparent (when nothing is set)
Applies toAll background longhand properties
InheritedNo
AnimatablePartially (depends on sub-values)
Common useHero sections, cards, colored panels, pattern backgrounds

Default Value

When no background is declared, the element background is transparent. Each longhand property inside the shorthand has its own initial value, such as background-repeat: repeat and background-color: transparent.

💎 Property Values

The background shorthand can include these component values:

ComponentExampleMeaning
colorbackground: #eff6ff;Sets the background color behind images
imageurl("photo.jpg")Adds a background image or gradient
repeatno-repeatControls tiling behavior
position / sizecenter / coverPlaces and scales the background
attachmentfixedControls scroll behavior
origin / clipborder-boxControls positioning and painting areas

Shorthand Components

The background property combines these longhand properties. Learn each one individually for deeper control:

background-color

Sets the fill color behind images.

background-image

Adds photos, SVGs, or CSS gradients.

background-repeat

Controls whether images tile.

background-position

Places the background inside the element.

background-size

Scales backgrounds with cover, contain, or lengths.

background-attachment

Sets scroll, fixed, or local behavior.

background-origin

Defines where positioning starts.

background-clip

Controls how far the background paints.

Shorthand vs Longhand

ApproachWhen to useExample
background shorthandQuick hero sections and common one-line setupsbackground: #fff url(img.jpg) no-repeat center / cover;
Longhand propertiesWhen one value changes independently in media queriesbackground-size: contain; on mobile only
Color-only shorthandSimple colored panels without imagesbackground: #dbeafe;

👀 Live Preview

Three common ways to use the background shorthand:

color only
gradient hero
pattern tile

One property handles color, images, repeat, position, and size together.

Examples Gallery

Try the background shorthand with a hero banner, solid color, repeating pattern, and a longhand comparison.

📚 Common Background Setups

Start with the shorthand patterns you will use most in real layouts.

Example 1 — Hero Banner with cover

Set a fallback color, gradient image, placement, and cover sizing in one declaration.

hero-background.css
.hero {
  background: #fff7ed linear-gradient(135deg, #2563eb, #7c3aed) no-repeat center / cover;
  min-height: 12rem;
}
Try It Yourself

How It Works

The color acts as a fallback, the gradient is the image layer, no-repeat prevents tiling, and center / cover centers and fills the box.

Example 2 — Solid Color Background

The shorthand works with a color alone when no image is needed.

color-background.css
.card {
  background: #dbeafe;
  padding: 1.5rem;
  border-radius: 0.75rem;
}
Try It Yourself

How It Works

background: #dbeafe; is equivalent to background-color: #dbeafe;.

🎨 Patterns & Equivalents

Combine repeat and size in shorthand, and compare with longhand declarations.

Example 3 — Repeating Pattern Background

Tile a small dot pattern using repeat and an explicit size in one line.

pattern-background.css
.panel {
  background: #ecfdf5 radial-gradient(circle, #059669 0 5px, transparent 6px) repeat center / 24px 24px;
}
Try It Yourself

How It Works

The slash sets tile size to 24px 24px while repeat tiles the gradient across the panel.

Example 4 — Shorthand vs Longhand

These two rules produce the same visual result.

shorthand-longhand.css
/* Shorthand */
.shorthand {
  background: #eff6ff linear-gradient(135deg, #2563eb, #ec4899) no-repeat center / contain;
}

/* Longhand equivalent */
.longhand {
  background-color: #eff6ff;
  background-image: linear-gradient(135deg, #2563eb, #ec4899);
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
Try It Yourself

How It Works

Shorthand is concise; longhand is easier to read and override one value at a time in responsive layouts.

🧠 How the background Shorthand Works

1

You list the values you need

Include color, image, repeat, position, and size in one declaration.

One line
2

The browser maps each value

CSS assigns each part to its matching longhand property automatically.

Auto mapping
3

Omitted values reset

Any longhand not included returns to its initial value for that layer.

Defaults apply
=

Complete background styling

You control color, images, placement, and sizing with a single property.

Universal Browser Support

The background shorthand is supported in all modern browsers. The slash syntax for position and size is widely supported in current browsers.

Baseline · All browsers

Set complete backgrounds everywhere

Chrome, Firefox, Safari, Edge, and Opera support the background shorthand and its components.

99% Modern browser support
Google Chrome1+ · Desktop & Mobile
Full support
Mozilla Firefox3.6+ · Desktop & Mobile
Full support
Apple Safari4+ · macOS & iOS
Full support
Microsoft Edge12+ · Modern versions
Full support
Opera10.5+ · Modern versions
Full support

Fallback behavior

If slash syntax is unsupported in a legacy browser, split into longhand background-position and background-size declarations.

💻
Legacy browsers Use longhand properties as a fallback for complex shorthand values
Fallback
background shorthand property 99% supported

Bottom line: Use the background shorthand confidently in modern projects, especially for hero sections and colored panels.

Conclusion

The background shorthand is one of the most practical CSS properties for web design. It lets you set color, images, repeat behavior, placement, and size in a single declaration.

Continue with the individual background properties—starting with background-attachment—to learn each longhand value in depth.

💡 Best Practices

✅ Do

  • Use shorthand for hero banners: background: color image no-repeat center / cover;
  • Include a fallback background color before images
  • Use longhand when overriding one value in media queries
  • Keep shorthand readable by limiting the number of values
  • Learn individual background-* properties for advanced control

❌ Don’t

  • Forget the slash before background-size in shorthand
  • Assume shorthand and longhand can be mixed carelessly on the same layer
  • Put size before position in shorthand syntax
  • Use overly long one-line declarations that are hard to maintain
  • Skip testing backgrounds on mobile and high-DPI screens

Key Takeaways

Knowledge Unlocked

Five things to remember about the background shorthand

Use these points when writing background CSS.

5
Core concepts
🖼️02

Color + Image

Layer backgrounds.

Layers
📍03

center / cover

Position and size.

Syntax
📝04

Color Only

background: #dbeafe;

Simple
🛠05

Learn Longhand

Fine-grained control.

Next steps

❓ Frequently Asked Questions

The background property is a shorthand that sets multiple background-related values in one declaration, including color, image, position, size, repeat, attachment, origin, and clip.
When no background is specified, the background is transparent. Individual longhand properties each have their own initial values when reset.
Common order is color, image, repeat, attachment, position, then size after a slash. For example: background: #fff url(img.jpg) no-repeat center / cover;
The slash separates background-position from background-size. Size must come after position when using the slash syntax, such as center / cover.
Use shorthand for concise hero sections and common patterns. Use longhand properties when you need to change one value independently or improve code readability in complex layouts.

Practice in the Live Editor

Open the HTML editor, combine background values, and experiment with the shorthand instantly.

HTML Editor →

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.

5 people found this page helpful