liveBook · Manning

Background property

As explained in Tiny CSS Projects

The CSS background property is a shorthand property for setting an element's background layers and related background properties.

The CSS `background` property lets you set related background values in one declaration. Instead of writing separate rules for an image, its position and size, and a color, you can write `background: url("bg.jpeg") left top / cover blue;`. section 9.3.1

Read that declaration from left to right:

  • `url("bg.jpeg")` supplies the image; `left top` positions it; `/ cover` sizes it to cover the element; and `blue` supplies a color behind it. section 9.3.1
  • The image appears over the color. The color can show through a transparent image, fill uncovered space, or remain visible while an image loads or fails to load. section 9.3.1
  • `cover` fills the element without distorting the image, but may clip it when their shapes differ. Use `contain` when avoiding clipping matters more. section 9.3.1

Choose the amount of control you need

A card can use the shorthand for a positioned photo and fallback color. For a curved gradient behind a profile picture, separate `background-image`, `background-size`, `background-position`, and `background-repeat` declarations make each adjustment clear: the gradient is enlarged, moved upward, and prevented from repeating. section 9.3.1 section 6.6

The shorthand also works for simpler jobs: `background: #010101;` gives a page a dark backdrop, while `background: none;` removes a button’s default gray background before adding custom styles. section 5.5 section 8.2.2

Listing 9.12 Background image for the front and back of the card
.card-item__side{
  height: 100%;
  width: 100%;
  background: url("bg.jpeg") left top / cover blue;
}

See the complete shorthand declaration applied to the rule shared by both sides of a credit card. section 9.3.1

How Tiny CSS Projects covers it

How deep it goes
Breaks down one shorthand declaration and explains image sizing and color fallback, without developing a full property reference.
Examples
Shows a photographic credit card, a radial-gradient profile card, a dark page background, and buttons with their default background removed.
What it assumes
Assumes familiarity with CSS rules and declarations, while introducing shorthand through its individual background properties.
What you can do after
Operate/configure it to position and size a card image over a fallback color, or remove a button’s default background.
How it connects
Connects background-image, background-size, background-position, and background-color with radial gradients and button styling.

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 Tiny CSS Projects. The section links point to the parts of the book it draws on.