A cheat sheet for web design students. Learn the four key properties that control a background image (background-image, background-repeat, background-position and background-size), and find out how to combine them in a single background shorthand declaration.
The short version
First choose the image, then decide how it repeats, where it’s positioned and how it’s sized. Here’s the most common full-width setup in a single line, starting with a colour as a fallback:
background: #14213d url("../images/hero-mountains.jpg") center / cover no-repeat;On this cheat sheet
When to use a background image
Backgrounds are for decoration
A CSS background image sits behind an element’s content, and it should only be used for decoration, such as textures, patterns, banner backgrounds, and small icons beside text. An image that carries meaning, such as a product photo, a chart or a team photograph, belongs in the HTML, marked up with <img>.
Use a CSS background for
Decorative textures and patterns
A banner or hero backdrop behind text
Small icons or bullets beside text
Use an HTML img for
Photographs and illustrations that are content
Anything that needs alt text
Images people may want to save or print
Background images have no alt text, so screen readers ignore them. Choose your image formats using cheat sheet 8, and remember that SVG (cheat sheet 7) works well for patterns and icons.
The background-image property
Choose the image
The background-image property takes one or more images. Most commonly, that’s an image file, written with url(). The default value is none.
.hero { background-image: url("../images/hero-mountains.jpg"); }
.banner { background-image: linear-gradient(#2a5bd7, #176b3f); } /* gradients are images too */
.card { background-image: url("../images/tile.png"), linear-gradient(#bfd9ff, #2a5bd7); } /* two layers */
url(image)Shown at its natural size, from the top left, and tiledlinear-gradient()A generated image with no filetwo layersThe first listed is on top
The path in url() is relative to the CSS file, not to the HTML page. This is the same document-relative idea that we saw in cheat sheet 2. So, a stylesheet in a css/ folder reaches an image in the images/ folder with ../images/.
Put the path in quotes. They’re optional for simple names, but they’re always safe.
Separate multiple images with commas. The first image in the list is the top layer.
By default, an image is shown at its natural size, tiled to fill the box, starting from the top-left corner. The next three properties change that behaviour.
Always add a background-color too, because it will show while the image loads, or if the image fails to load.
CSS file is at
Image is at
Write
css/main-styles.css
images/hero-mountains.jpg
url("../images/hero-mountains.jpg")
main-styles.css (top folder)
images/hero-mountains.jpg
url("images/hero-mountains.jpg")
Anywhere, on a server
images/hero-mountains.jpg
url("/images/hero-mountains.jpg") (root-relative)
The background-repeat property
Tile the image, or don’t
If an image is smaller than its box, the browser repeats it to fill the space. The background-repeat property controls this behaviour, and its default value is repeat.
Value
Effect
repeat (default)
Tiles in both directions. Tiles at the edges are clipped.
repeat-x
Tiles horizontally only, in a single row
repeat-y
Tiles vertically only, in a single column
no-repeat
Shows the image once
space
Tiles as many whole copies as fit and spreads the gaps evenly. Nothing is clipped.
round
Tiles whole copies and stretches or squeezes them slightly so they fit exactly
Two values set the horizontal and vertical repeat separately, so repeat no-repeat is the same as repeat-x. For photographs and logos, you’ll almost always want no-repeat. Use repeat for small, seamless tiles that join without visible edges.
The background-position property
Place the image in the box
The background-position property sets where the image starts inside its box. Its default value is 0% 0%, the top left corner. Give it two values, horizontal first, then vertical.
Kind of value
Examples
Notes
Keywords
left, center, right, top, bottom
right bottom is the bottom-right corner. Order doesn’t matter for keywords.
Lengths
20px 20px, 2rem 0
The distance from the top-left corner. A bare 0 needs no unit, but any other number does.
Percentages
50% 50%, 100% 0
Lines up that point of the image with the same point of the box. 50% 50% centres it. 100% aligns the image’s right edge with the box’s right edge.
Edge offsets (four values)
right 10px bottom 10px
An offset measured from a chosen edge
0 0The default: top leftcenterright bottom50% 100%Centred, at the bottom20px 20pxright 10px bottom 10px
.hero { background-position: center; } /* centre both ways */
.banner { background-position: center top; } /* keep the top of a tall photo in view */
.note { background-position: left center; padding-left: 2rem; } /* icon beside text */
If you give just one value, it sets the horizontal position, and the vertical position becomes center. Positioning matters most when the image is cropped, as it is with cover (see the next section), because it decides which part of the image stays visible.
The background-size property
Scale the image
The background-size property sets how big the image is drawn. Its default value is auto, which is the image’s natural size. The brown frame on the demo image below makes any cropping easy to see.
Value
Effect
auto (default)
The image’s natural size
cover
Scales the image, keeping its proportions, until it covers the whole box. Any excess is cropped. The usual choice for hero backgrounds.
contain
Scales the image, keeping its proportions, until it fits entirely inside the box. Empty space may be left, and it is filled by tiling unless you use no-repeat.
One length or %: 100px, 50%
Sets the width. The height follows automatically, so the proportions are kept. A percentage is of the box, not of the image.
Two values: 100px 40px, 100% 100%
Sets width and height. The image is stretched if the proportions differ.
autoNatural size, so it is croppedcoverFills the box, crops the restcontainShows it all, leaving gaps100pxWidth set, height follows50%Half the box’s width100% 100%Stretched out of shape*
Watch outbackground-size: 100% 100%; /* distorts */
background-size: 500px; /* a small raster
image goes blurry */
A box needs a size for cover to work. An empty div has no height, so its background will be invisible. Give it a min-height, or put some real content inside it. Note too that scaling a raster image up makes it blurry, so start with an image that’s at least as large as its display size.
*SVG background images may not stretch. For more information, see Scaling SVG backgrounds at MDN Web Docs.
The background shorthand
Combine the properties in one declaration
The background shorthand sets all these properties in a single declaration. The values go in the order shown below, and every part is optional.
background: colorimageposition/ sizerepeat;
background: #14213d url("../images/hero-mountains.jpg") center / cover no-repeat;
/* The banner above */
.hero {
background:
linear-gradient(rgb(20 33 61 / 45%), rgb(20 33 61 / 45%)), /* top layer: a dark overlay */
#14213d url("../images/hero-mountains.jpg") center / cover no-repeat; /* bottom layer, with the colour */
}
Rules of the shorthand:
Size must follow position, with a slash between them, as in center / cover. A size with no position isn’t valid, so write 0 0 / cover or center / cover.
Most of the other parts can come in any order, but if you keep to the order above, your CSS will be consistent and readable.
Separate multiple layers with commas. The first layer is on top, and the colour can only appear in the last layer.
Colours and images sit side by side, with no commas between them, within a single layer.
The big gotcha: shorthand resets everything
Any part that you leave out is reset to its default value, which wipes out anything you set earlier. This catches out a lot of students.
Do: set everything you need.hero {
background: #14213d url("hero.jpg")
center / cover no-repeat;
}
/* or tweak one property afterwards */
.hero:hover {
background-size: 110%;
}
Don’t: the second rule undoes the first.hero {
background-color: #14213d;
background-size: cover;
background-repeat: no-repeat;
}
.hero {
background: url("hero.jpg");
}
/* colour, size and repeat are
all reset to their defaults */
Property
Default (what the shorthand resets to)
background-color
transparent
background-image
none
background-position
0% 0% (top left)
background-size
auto
background-repeat
repeat
background-attachment
scroll
Use the shorthand when
You’re setting up a background from scratch.
You want compact, readable CSS.
You’re defining several layers together.
Use the longhand when
You’re changing one property, such as in a hover state or media query.
You don’t want to reset the others.
You want to be as explicit as possible while learning.
Related properties
Property
What it does
Values
background-color
The colour behind (and visible through) the image. Always provide one as a fallback, and for contrast.
scroll (default), fixed (stays still: often janky on mobile), local
background-origin
The area that position and size are measured from
padding-box (default), border-box, content-box
background-clip
How far the background extends
border-box (default), padding-box, content-box
Common patterns
Goal
CSS
Full-width hero photo
background: #14213d url("../images/hero.jpg") center / cover no-repeat; with a min-height
Photo with readable text on top
A semi-transparent gradient layer above the image, as in the banner above
Seamless texture
background: #eef2f7 url("../images/paper.png"); (the default repeat)
Icon beside text
background: url("../images/info.svg") left center / 1.25rem no-repeat; plus padding-left: 2rem;
Whole image visible in a fixed area
background: url("../images/logo.svg") center / contain no-repeat;
Decoration in a corner
background: url("../images/leaf.svg") right bottom / 6rem no-repeat;
Text, contrast and accessibility
Make sure text stays readable. Text over a busy photograph is hard to read, so add a dark or light overlay layer (as in the banner), or set the text on a solid panel. Aim for the contrast ratios mentioned in cheat sheet 5.
Always set a fallback colour that gives the text good contrast, even when the image doesn’t load.
Never put essential information in a background image. Screen readers can’t see it, it won’t print by default, and if the download fails, it vanishes.
Mind the file size. Large hero images are a common cause of slow pages, so apply the lessons from cheat sheet 8.
Common mistakes
Mistake
Fix
The image doesn’t appear because the path is relative to the HTML page
Write the path relative to the CSS file, such as ../images/hero.jpg
A photo repeats like wallpaper
Add no-repeat
cover on an empty element shows nothing
Give the element a min-height or content
A later background shorthand wipes out earlier longhands
Set every part you need, or use longhands to adjust
Tip: in your browser’s dev tools, edit the background-position and background-size values live in the Styles panel to see exactly how each one moves and scales the image.