liveBook · Manning

Background property

As explained in CSS in Depth, Second Edition

The CSS background shorthand property sets multiple background-related properties.

The CSS background property is a shorthand for eight related properties: background-color, background-image, background-size, background-repeat, background-position, background-origin, background-clip, and background-attachment. It keeps declarations concise, but specifying it resets unspecified background values to their initial values. section 13.1 section 1.4

Use individual properties when a background needs more than a few settings, especially because shorthand declarations can undo earlier values.

  • Use background-color for a solid color that renders behind background images. section 13.1
  • Use background-image for file-based images or generated gradients. section 13.1
  • Treat background-attachment: fixed cautiously because it can negatively affect page performance. section 13.1

A simple linear gradient

A 400-by-200-pixel element can render a horizontal gradient from white to blue: .fade { height: 200px; width: 400px; background-image: linear-gradient(to right, white, blue); }. The same direction can be expressed with 90deg.

How CSS in Depth, Second Edition covers it

How deep it goes
Defines eight constituent properties and reset behavior, then demonstrates gradients without detailing every constituent property.
Examples
Demonstrates a white-to-blue linear gradient on a fixed-size element using directional and degree notation.
What it assumes
Assumes basic CSS declarations and familiarity with solid-color backgrounds or background images.
What you can do after
Apply it via CSS while choosing between concise shorthand and safer individual declarations for complex backgrounds.
How it connects
Connects background-color, background-image, gradients, background positioning, background clipping, and CSS shorthand properties.

Also explained in

Compare how 2 books explain background property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.