Favorites

A Guide to CSS Background Properties

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee
CSS Backgrounds

When most developers think of CSS backgrounds, they reach for background-color, or put a background-image on a hero section and call it done. But backgrounds are quietly one of the richest areas of CSS: a set of nine properties that, combined, can produce effects that would otherwise need an image editor, extra HTML elements or JavaScript.

A single HTML element can have several background layers, each with its own size, position, repeat behaviour and blend mode, all stacked on top of a base colour, in just a few lines of CSS. That checkerboard pattern you once made with a PNG? Pure CSS. That two-tone photo effect? A couple of gradients and a blend mode. That gradient text? One property you might not know about.

What you'll learn: this guide walks through all nine background properties: what each one does, the values it accepts, and how they work together. Every property has a live demo you can try right here in the browser.

Colour and images

background-color

background-color paints a solid colour behind an element's content. It's always the bottom layer, underneath any background images, so it shows through wherever images are transparent, don't cover the whole element, or fail to load. That makes it a useful fallback too.

By default, the colour fills the content and padding areas, and extends underneath the border as well (you'll see it through dashed, dotted or semi-transparent borders). It never extends into the margin.

It accepts any CSS colour format:

background-color: tomato;
background-color: #c1440e;
background-color: rgb(193 68 14 / 0.8);
background-color: hsl(22deg 86% 41%);
background-color: oklch(55% 0.2 35);
background-color: transparent;  /* the default */
Results
background-color: #1e3a5f
100%

background-image

background-image places one or more images behind an element. They can be image files (PNG, JPEG, WebP, SVG and so on) loaded with url(), or images generated by CSS: linear-gradient(), radial-gradient(), conic-gradient() and their repeating versions. image-set() also lets the browser choose between versions of an image for different screen resolutions.

You can list several images, separated by commas. They're stacked in layers, with the first image on top, and the background colour underneath them all:

/* A single image */
background-image: url("texture.png");

/* A gradient */
background-image: linear-gradient(135deg, #c1440e, #f4c97a);

/* Several layers: the first one listed is on top */
background-image:
  url("dots.png"),
  radial-gradient(circle at 50% 50%, #ffffff, #eeeeee);
Results
linear-gradient(135deg, #1a56db, #3b82f6)

Note: gradients count as images in CSS, so you can layer gradients with each other or with url() images in the same background-image list. Background images are decorative: screen readers don't announce them, so never use one for an image that conveys important information.

Repeat, position and size

background-repeat

By default, a background image repeats in both directions until it fills the element. background-repeat changes that:

background-repeat: repeat;      /* default: tile in both directions */
background-repeat: no-repeat;   /* show the image once */
background-repeat: repeat-x;    /* tile horizontally only */
background-repeat: repeat-y;    /* tile vertically only */
background-repeat: space;       /* tile without cropping, spacing the tiles out */
background-repeat: round;       /* tile without cropping, stretching the tiles slightly */

/* Two values: horizontal, then vertical */
background-repeat: repeat space;
Results
repeat

Try space and round, and notice the difference at the edges: with repeat, the last tiles are cut off, while space adds gaps between whole tiles, and round resizes the tiles so a whole number of them fits.

background-position

background-position sets where a background image is placed. It's most useful with no-repeat images, or to choose which part of a large image stays visible. It accepts keywords (top, right, bottom, left, center), lengths and percentages, and the default is 0% 0%, the top-left corner.

background-position: center;
background-position: top right;
background-position: 50% 50%;
background-position: 20px 40px;

/* Offsets from any edge: 20px from the right, 30px from the bottom */
background-position: right 20px bottom 30px;

Press the buttons, or click anywhere in the box to place the image at that point:

Results
center

Percentages work in a clever way: 25% 75% lines up the point 25% across and 75% down the image with the point 25% across and 75% down the element. So 50% 50% centres the image, and 100% 100% puts it exactly in the bottom-right corner, without it overflowing.

background-size

Background images are shown at their natural size unless you set background-size:

background-size: auto;         /* natural size (default) */
background-size: cover;        /* fill the element, cropping if needed */
background-size: contain;      /* fit the whole image inside, leaving gaps if needed */
background-size: 200px 100px;  /* exact width and height */
background-size: 50% auto;     /* half the element's width, height in proportion */
Results
cover
  • cover scales the image until it covers the whole element, keeping its shape. Parts of the image may be cropped.
  • contain scales the image until the whole thing fits inside, keeping its shape. There may be empty space around it.
  • One length sets the width, and the height follows automatically to keep the image's shape.

Tip: for hero images, background-size: cover with background-position: center is the go-to combination. The image always fills the space and stays centred, whatever the screen size.

background-attachment

background-attachment controls what a background moves with when things scroll:

background-attachment: scroll;  /* default: moves with the element */
background-attachment: fixed;   /* stays fixed to the screen */
background-attachment: local;   /* moves with the element's scrolling content */
ValueBackground is attached toWhen the element's own content scrolls
scrollThe element's boxThe background stays still
localThe element's contentThe background scrolls with the content
fixedThe browser windowThe background stays still, even when the page scrolls

The difference between scroll and local only shows inside an element that scrolls. Scroll inside each box below: on the left, the stripes stay still while the text moves; on the right, they move with the text.

Results

background-attachment: scroll

Line 1 of the scrolling content.

Line 2 of the scrolling content.

Line 3 of the scrolling content.

Line 4 of the scrolling content.

Line 5 of the scrolling content.

Line 6 of the scrolling content.

Line 7 of the scrolling content.

Line 8 of the scrolling content.

Line 9 of the scrolling content.

Line 10 of the scrolling content.

Line 11 of the scrolling content.

Line 12 of the scrolling content.

background-attachment: local

Line 1 of the scrolling content.

Line 2 of the scrolling content.

Line 3 of the scrolling content.

Line 4 of the scrolling content.

Line 5 of the scrolling content.

Line 6 of the scrolling content.

Line 7 of the scrolling content.

Line 8 of the scrolling content.

Line 9 of the scrolling content.

Line 10 of the scrolling content.

Line 11 of the scrolling content.

Line 12 of the scrolling content.

fixed creates the classic "parallax" look, where page content slides over a background that stays still.

Use fixed with care: on many phones and tablets, including iPhones and iPads, browsers ignore background-attachment: fixed and treat it like scroll, partly because fixed backgrounds can make scrolling slow. For a dependable parallax effect, scroll-driven animations are a better choice today.

background-origin and background-clip

These two properties both refer to an element's three boxes (border box, padding box and content box), but they do different jobs:

  • background-origin sets the area that background images are positioned and sized against.
  • background-clip sets the area the background is painted in.

background-origin

background-origin: padding-box;  /* default: start at the inside of the border */
background-origin: border-box;   /* start at the outside edge of the border */
background-origin: content-box;  /* start at the inside of the padding */

The demo has a thick dashed border and 40px of padding, with a single image positioned at top left. Watch where "top left" moves to:

Results
padding-box

background-clip

background-clip: border-box;   /* default: paint under the border too */
background-clip: padding-box;  /* stop at the inside of the border */
background-clip: content-box;  /* stop at the inside of the padding */
background-clip: text;         /* paint only inside the text */
Results
Deep Diveborder-box

The text value is the creative one: it limits the background to the shape of the text, so you can fill letters with a gradient or an image. Make the text colour transparent so the background shows through:

.gradient-text {
  background: linear-gradient(135deg, #93c5fd, #3b82f6, #f0abfc);
  -webkit-background-clip: text;   /* for older browsers */
  background-clip: text;
  color: transparent;
}

Tip: the original version of this post also set -webkit-text-fill-color: transparent. With color: transparent, that isn't needed. Make sure every colour in the gradient contrasts well with the background behind the text, so it stays readable.

background-blend-mode

background-blend-mode controls how background layers mix with the layers beneath them, including the background colour, like the blending modes in an image editor. There are 16 blend modes, including multiply (darkens), screen (lightens), overlay (boosts contrast), luminosity and difference.

background-image:
  linear-gradient(135deg, rgb(255 0 128 / 0.9), rgb(0 100 255 / 0.9)),
  radial-gradient(circle at 30% 60%, gold, crimson, navy);
background-blend-mode: overlay;
Results
normal

With several layers, give a comma-separated list of blend modes, one for each layer. A popular use is tinting a photo: put a colour underneath it and use multiply or luminosity. Results depend heavily on the colours involved, so experiment.

The shorthand and multiple backgrounds

The background shorthand

The background shorthand sets all of the properties above in one declaration, except background-blend-mode, which must always be set on its own:

/* A hero image */
background: url("hero.jpg") center / cover no-repeat;

/* With a fallback colour */
background: url("hero.jpg") center / cover no-repeat #0d1b2a;

/* Several layers, separated by commas */
background:
  url("stars.png") center / 200px repeat,
  url("moon.png") 80% 20% / 80px no-repeat,
  linear-gradient(to bottom, #0d1b2a, #1a56db);

Most values can go in any order, with two rules worth remembering:

  • Size comes straight after position, separated by a slash: center / cover.
  • The background colour can only go in the last layer.

Watch out: the shorthand resets every background property it covers to its default value, including any you didn't mention. So if you set background-size and then background later, the size is reset to auto. Write the shorthand first, and individual properties after it.

Here's the shorthand you'll write most often, and its longhand equivalent:

/* Shorthand */
background: url("image.jpg") center / cover no-repeat;

/* The same, written out in full */
background-image: url("image.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;

Multiple backgrounds

Every background property except background-color accepts a comma-separated list, one value for each layer, with the first value applying to the top layer. That lets you build surprisingly complex effects on a single element:

/* A texture over a gradient */
background-image:
  url("noise.png"),                             /* layer 1 (top) */
  linear-gradient(135deg, #667eea, #764ba2);    /* layer 2 */
background-size: 200px 200px, cover;
background-blend-mode: overlay, normal;

All of these patterns are made from CSS gradients alone, with no image files:

Results
Mesh gradient: 3 radial layers on a colour

Quick reference

Here are all nine background properties, with their default values. Follow the links for full details, examples and browser support on our CSS property pages.

PropertyDefaultIntroduced in
background-colortransparentCSS1
background-imagenoneCSS1
background-repeatrepeatCSS1
background-position0% 0%CSS1
background-attachmentscrollCSS1
background-sizeautoCSS3
background-originpadding-boxCSS3
background-clipborder-boxCSS3
background-blend-modenormalCompositing and Blending Level 1

All nine work in every modern browser.

Wrapping up

CSS backgrounds go far beyond a colour and a picture. Layer images and gradients, control how they repeat, where they sit and how big they are, choose which box they're positioned against and painted in, and blend them together. With the shorthand for everyday use and multiple layers for the clever stuff, you can create patterns, textures and effects on a single element, without a single extra image file.

☕
Enjoying this site?

Help keep it running with a small coffee donation. Every bit of support is appreciated!

Buy us a coffee