The background-size property controls how large a background image is drawn, background-position controls where it sits, and background-repeat controls whether it tiles. For a full-bleed picture, the usual combination is background-size: cover, background-position: center and background-repeat: no-repeat. This guide explains css background-size cover and its siblings.
Each property takes simple values, but their interaction (especially with tiling and the shorthand) trips up many beginners.
background-size accepts keywords, lengths and percentages.
If you give one value, it is the width and the height becomes auto, which keeps the aspect ratio. Percentages refer to the background positioning area, which is the padding box by default.
| Keyword | What it does | Side effect |
|---|---|---|
cover | Scales the image, keeping its ratio, until it fills the whole box | Parts of the image may be cropped |
contain | Scales the image, keeping its ratio, until it fits entirely inside the box | Empty space may remain, so the image can tile |
Use cover for hero banners and cards. Use contain for logos or patterns that must never be cut off, and pair it with background-repeat: no-repeat to avoid unwanted copies.
background-position sets the starting point of the image inside the box. It accepts keywords (left, center, right, top, bottom), lengths and percentages.
With cover, the position decides which part survives the crop. For a portrait photo in a wide banner, center top often keeps faces visible, while center may cut off the head. Percentages align the same percentage point of the image with that point of the box, so 50% 50% always centers.
By default a background repeats in both directions. background-repeat changes that.
| Value | Result |
|---|---|
repeat | Tiles horizontally and vertically (default) |
no-repeat | One copy only |
repeat-x | Tiles horizontally only |
repeat-y | Tiles vertically only |
space | Tiles whole copies and spreads the gap evenly |
round | Tiles and stretches copies so a whole number fits |
Small seamless textures use repeat; photos should nearly always use no-repeat.
The size must follow the position, separated by a slash. Writing cover without center / before it is invalid and the declaration is dropped. Remember that the shorthand resets omitted sub-properties, so place it before any individual overrides such as background-position: top.
The first box tiles a small image at a fixed 24 pixel size. The second box scales one photo to cover its area and keeps the part that sits 30 percent from the top. Resize the window to see how cover re-crops the photo.
background-attachment: fixed keeps the image still while the page scrolls. It is often disabled or poorly supported on mobile browsers, so test before relying on it.background-origin decides whether percentages are measured from the padding box, border box or content box.background-clip decides how far the painting extends; text clips the background to the text shape.img with object-fit: cover and object-position gives the same cropping behavior with real alt text.It scales the image proportionally to the smallest size that still fully covers the element, cropping any overflow. The box never shows empty space.
Set background-repeat: no-repeat, or include no-repeat in the background shorthand.
cover scales the image up to fill the box, so a small file looks soft on a large element. Use a larger source image or srcset-style alternatives with image-set().
Use background-position: center, which means 50% horizontally and 50% vertically.
Use background-size to choose between natural size, fixed dimensions, cover and contain; use background-position to decide which part of the image shows; and use background-repeat to control tiling. The common hero recipe is center / cover no-repeat. Keep the shorthand order in mind so one declaration does not silently reset another.