How to add a background image
background-image paints an image behind the element's content, padding and border. By default it starts at the top left corner of the padding box, keeps its natural size and repeats to fill the box:
In a real project the url() points at a file: url("images/hero.jpg"). The path is relative to the stylesheet that contains it (or to the page, for a <style> element). The examples on this page use small SVG images written inline as data: URLs so they run anywhere.
Two things to know from the start:
- A background does not give an element size. An empty
divwith a background image is 0px tall. Give it content, padding, aheightor anaspect-ratio. - A background image is decoration. Screen readers skip it and it has no alt text. An image that carries meaning belongs in an
<img>element.
background-size: cover, contain and lengths
background-size scales the image. The two keywords keep its proportions:
The image is twice as wide as it is tall and each box is square.
| Value | Result |
|---|---|
auto (default) | The image's own size |
cover | Scales until the box is completely filled; the excess is cropped |
contain | Scales until the whole image fits; gray bands remain on two sides |
200px | 200px wide, height in proportion |
50% auto | Half the box's width, height in proportion |
100% 100% | Exactly the box's size; distorts the image if the proportions differ |
cover is the usual choice for hero banners and cards. contain is for logos and illustrations that must never be cut off.
background-position and background-repeat
background-position places the image inside the box, with keywords (top, center, bottom, left, right), percentages or lengths. background-repeat controls tiling. Try some values:
Try positions top right, bottom 10px right 20px and 25% 75%, and repeat values repeat-x, repeat-y, space (tiles with gaps so none are cut) and round (tiles resized so a whole number fits).
Layering: several backgrounds and a text overlay
background-image takes a comma-separated list. The first layer is drawn on top. The common use is a semi-transparent gradient over a photo, so white text stays readable:
aspect-ratio gives the empty box a height, and background-size: cover applies to both layers. For more gradient recipes, see CSS gradients.
The background shorthand
background sets every background property in one line. Size must come right after position, separated by a slash, and the color goes in the last layer:
.hero {
background: url("hero.jpg") center / cover no-repeat, #1f2937;
}
/* the same as */
.hero {
background-image: url("hero.jpg");
background-position: center;
background-size: cover;
background-repeat: no-repeat;
background-color: #1f2937;
}
The fallback color shows while the image loads, and if it fails. Any property you leave out of the shorthand is reset to its default, so put the shorthand first and individual properties after it.
Other background properties
| Property | What it does |
|---|---|
background-attachment: fixed | The image stays put while the page scrolls (ignored on iOS Safari) |
background-origin | Whether position starts at the border, padding or content edge |
background-clip | Where the background is painted, including text to fill letters |
background-blend-mode | Blends layers together, like multiply |
Common mistakes
- A wrong path.
url()in a stylesheet is relative to the CSS file, not to the HTML page. - An empty element. It has no height, so the image has nowhere to show.
- Forgetting
no-repeatwithcontainor a small image: it tiles. - Stretching with
100% 100%when you meantcover. - Important images as backgrounds. Use
<img>with alt text for content; backgrounds are invisible to screen readers. - Missing quotes around a
data:URL containing spaces or quotes. Always writeurl("...").
Frequently Asked Questions
How do I add a background image in CSS?
Use background-image: url("photo.jpg"); on the element. The path is relative to the CSS file (or to the page, for a style attribute or <style> element). Give the element a size, because a background never makes an empty element grow.
How do I make a background image cover the whole element?
Use background-size: cover; with background-position: center;. The image scales to fill the box completely, keeping its proportions, and the overflow is cropped.
What is the difference between cover and contain?
cover fills the whole box and crops what does not fit. contain shows the whole image and leaves empty space on two sides when the proportions differ.
How do I stop a background image from repeating?
Add background-repeat: no-repeat;. By default a background image tiles in both directions to fill the element.
Why is my background image not showing?
The usual causes are a wrong path in url() (it is relative to the CSS file), an element with no height because it has no content, or a later background shorthand that reset the image.