CSS backgrounds

Quick reference for CSS background-color, layers, position, size, and using a gradient instead of a network image.

リファレンスページの本文は現在英語です。

A background is a stack of layers plus a color. The color is painted behind the image layers. If an image layer cannot be fetched or decoded, that layer is ignored and later layers or the color can still show. These examples do not use url() and do not request network images. They use CSS gradients, which are image values that do not depend on a file.

This page does not redo typography, color syntax, or the box model. Box-sizing and padding still matter because background-origin and background-clip can follow the padding box or the content box, and a border-box clip is the usual default.

background-clip: text (often with a -webkit- prefix) is a compatibility-sensitive way to paint a gradient through glyphs. It is mentioned only as a limit and is not treated as a universally unprefixed feature here.

Examples were parsed for syntax and structure. Painting and failed-image fallback were not measured in a browser.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Color, image layers, and gradients

Each image layer has its own repeat, position, size, origin, and clip in the expanded longhands. The background shorthand lists layers from front to back, then the color at the end. A linear-gradient() is a background-image, not a background-color.

Syntax or propertyInitial / defaultInheritedApplies toNotes
background-colortransparentnoall elementsPainted behind all image layers. If an image fails, this color can still show. Common values: #F3EFE4, rgb(), transparent.
background-imagenonenoall elementsCommon values: none, linear-gradient(...), radial-gradient(...), a comma-separated list of layers. url() is allowed by the language but is not used in these examples and would not be fetched from this documentation page.
background-repeatrepeatnoall elementsCommon values: repeat, no-repeat, repeat-x, repeat-y, space, round. Gradients are often paired with no-repeat when they should not tile.
background-position0% 0%noall elementsCommon values: top, center, bottom left, or offsets such as 0 0 and 50% 0. Percentages are relative to the positioning area minus the image size, which is not the same as a margin percentage.
background-sizeautonoall elementsCommon values: auto, cover, contain, or a width and height (100% 50%). cover and contain scale an image while preserving aspect ratio; they are less meaningful for a simple two-stop gradient but still valid.
background-clipborder-boxnoall elementsCommon values: border-box, padding-box, content-box. text is a supported value in some engines, often behind -webkit-background-clip, and is a compatibility limit here.
background-originpadding-boxnoall elementsDefines the positioning area for background-position. border-box, padding-box, or content-box. It does not by itself clip the paint; that is background-clip.
background-attachmentscrollnoall elementsscroll, local, or fixed. fixed is relative to the viewport unless a containing block (for example a transform) changes that. It was not scrolled here.
backgroundsee longhandsnoall elementsShorthand for the layer list and the color. A missing color in the shorthand resets background-color to transparent. List the color last when combining a gradient and a fallback color.

A gradient layer over a fallback color

background-color: #F3EFE4;
background-image: linear-gradient(...);
background-repeat: no-repeat;
background-size: 100% 50%;

The color sits behind the gradient. There is no url() image, so the example does not depend on a network file.

  • If this had used url() and the file had failed, the #F3EFE4 color could still show. That failure mode is not demonstrated with a request.
html
<div class="hero">
  <p>Gradient over a solid color, no image request</p>
</div>
<style>
.hero {
  background-color: #F3EFE4;
  background-image: linear-gradient(180deg, #0B5853, transparent);
  background-repeat: no-repeat;
  background-position: top;
  background-size: 100% 50%;
  padding: 1.5rem 1rem 2.5rem;
  color: #1B1814;
  font-family: sans-serif;
}
</style>

What this markup means

.hero has a solid fallback color and a linear-gradient as the only image layer, covering the top half and fading to transparent. No url() appears in the rule, so the documentation checker never requests an image. The color would also be the visible fallback if a url() layer had failed.

Related: Colors, Box model

MDN documentation → · CSS specification →