Backgrounds: Colors, Images, Position, Size, Repeat, Clip and Origin
The background properties one at a time: background-color and the canvas, background-image, repeat with space and round, position keywords, percentages and edge offsets, size with cover and contain, origin, clip including text, and attachment.
kvi5hnuCode Sprout team member11 min readOn this page
Every element can paint a background behind its content: a color, one or more images, or both. The background properties decide what's painted, where images go, how big they are, whether they repeat, and which part of the box they cover. This article goes through them one at a time; article 49 combines them into multiple layers and the background shorthand.
The examples use gradients as images. A gradient is an image generated by the browser (articles 50 and 51), so it works anywhere url() does, and needs no download.
background-color
.notice { background-color: #f4efe6; }
The initial value is transparent, so by default you see whatever is behind the element. The color is painted under any background images, and shows through their transparent parts.
Two rules for colors:
- Set a background color whenever you set a text color, on the same element or a close ancestor. If you set dark text and rely on the default white page, a user whose default background is dark, or a dark-mode extension, gets dark text on dark.
- Give image backgrounds a fallback color similar to the image, so the text stays readable while the image loads or if it fails.
The canvas
The page's background isn't really the <html> element's box: the background of the root element is painted on the canvas, the whole area behind the document, even beyond the root's height. If the root has no background, the <body> background is used for the canvas instead. That's why body { background: #f4efe6; } fills the whole window even on a short page.
background-image
.hero { background-image: url("images/harbor.jpg"); }
.panel { background-image: linear-gradient(#fff, #f4efe6); }
The initial value is none. The image is placed according to the other properties below, and painted over the background color. A comma-separated list adds several layers (article 49).
Use background images for decoration. They have no alternative text, so screen readers skip them, and they may be removed in forced colors mode (article 35) or when printing. A product photo, chart or anything with meaning belongs in an <img> with alt text.
background-repeat
By default an image tiles in both directions to fill the background:
| Value | Effect |
|---|---|
repeat (initial) | tiles in both directions; tiles at the edges are cut off |
repeat-x, repeat-y | tiles along one axis only |
no-repeat | one copy |
space | as many whole tiles as fit, with the leftover space spread evenly between them; no clipping |
round | stretches or shrinks the tiles so a whole number fits exactly |
Two values set the axes separately: background-repeat: space round spaces tiles horizontally and rounds them vertically. repeat-x is the same as repeat no-repeat.
Repeating tiles start from the image's position, then repeat in both directions from there, so background-position: center with repeat puts a whole tile in the middle and cuts tiles off at both edges.
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(8.5rem, 1fr)); gap: 0.6rem; }
.box {
block-size: 6rem;
border: 2px solid #8a3b12;
background-color: #f4efe6;
background-image: radial-gradient(circle, #2f7d4f 0 40%, transparent 42%);
background-size: 1.7rem 1.7rem; /* one tile */
display: grid; place-items: end start; padding: 0.2rem 0.4rem; font-weight: 700;
}
.box span { background: white; padding: 0 0.25rem; }
background-position
background-position places the image within the positioning area (by default the padding box, see background-origin below). It takes up to four values.
Keywords: left, center, right and top, center, bottom. Keywords can come in either order, top right or right top. A single keyword sets one axis, and the other becomes center: top is center top, computed as 50% 0%.
Percentages align the same point of the image and of the area. 25% 75% puts the point 25% across and 75% down the image on the point 25% across and 75% down the area. So 0% 0% is the top-left corner, 50% 50% centers the image, and 100% 100% puts it in the bottom-right corner, all without the image overflowing.
Lengths move the image's top-left corner by that distance from the area's top-left corner. 20px 10px is 20px from the left and 10px from the top. Negative lengths pull it outside, partly hidden.
With one value, the second is center. With two, the horizontal value comes first, unless keywords make the axis clear.
Offsets from any edge
Four values, or three, give an offset from a named edge:
.card { background-position: right 1rem bottom 0.5rem; } /* 1rem from the right, 0.5rem from the bottom */
That puts a badge or icon a fixed distance from the bottom-right corner, at any element size. Chrome computes it as calc(100% - 16px) calc(100% - 8px), which is what you'd otherwise write by hand.

Compare 25% 75% with 20px 10px: a percentage moves the tile's matching point, so the tile never leaves the box, while a length moves its top-left corner.
The longhands background-position-x and background-position-y set one axis, which helps when only one needs to change, for example in a media query or an animation.
background-size
| Value | Effect |
|---|---|
auto (initial) | the image's natural size; gradients, which have none, fill the positioning area |
one length or percentage, such as 200px or 50% | the width; the height is auto, keeping the aspect ratio |
two values, such as 200px 100px | width and height; can distort the image |
cover | scales the image, keeping its ratio, until it covers the whole area; parts may be cut off |
contain | scales it until it fits inside the area; there may be empty space |
Percentages are of the positioning area, not of the image. cover is the usual value for full-bleed hero images, and contain for logos or illustrations that mustn't be cropped. The demo draws a 2 picture with a canvas and uses it as the background image:
body { font: 0.8rem system-ui, sans-serif; margin: 1rem; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(8.5rem, 1fr)); gap: 0.6rem; }
.box {
block-size: 7rem;
border: 2px solid #8a3b12;
background-color: #f4efe6;
background-image: var(--picture);
background-repeat: no-repeat;
background-position: center;
display: grid; place-items: end start; padding: 0.2rem 0.4rem; font-weight: 700;
}
.cover { background-size: cover; }
.contain { background-size: contain; }
.half { background-size: 50%; } /* width 50% of the box, height keeps the 2:1 ratio */
With a picture of 1600 × 800 pixels in a 300 × 300 box, cover scales it to 600 × 300 and crops the sides, and contain scales it to 300 × 150, with empty bands above and below. Gradients have no natural size or ratio, so with cover and contain they just fill the area.

The dashed parts outside the brown border are the image area the box clips away: with cover here, 27px at the top and the bottom.
For a responsive hero image in HTML, <img> with object-fit: cover (article 38) does the same job, and adds alt text and responsive image loading (article 53).
background-origin and background-clip

These two are often confused:
background-originsets the positioning area: the box thatbackground-positionand percentagebackground-sizeare measured in. Initial:padding-box. Alsoborder-boxandcontent-box.background-clipsets the painting area: how far the background, color and images, extends. Initial:border-box, so the background shows under a dashed or semi-transparent border. Alsopadding-box,content-boxandtext.
So by default an image at 0 0 starts inside the border, but repeating tiles still paint under the border. With background-clip: padding-box, nothing is painted under the border, which is useful for semi-transparent borders:
.frame {
border: 10px solid rgb(0 0 0 / 0.2);
background: #f4efe6 padding-box; /* in the shorthand, one box keyword sets both origin and clip */
}
Backgrounds follow border-radius: the painting area has the same rounded corners.

The semi-transparent dashes make the difference visible: with the initial border-box, stripes show through and between them.
background-clip: text
text paints the background only where there's text, so a gradient or image fills the letters. The text color must be transparent, or it covers the background:
body { margin: 1rem; font-family: system-ui, sans-serif; }
h1 {
font-size: 3rem;
margin: 0;
color: #8a3b12; /* fallback where clipping to text isn't supported */
}
@supports (background-clip: text) {
h1 {
background-image: linear-gradient(90deg, #8a3b12, #f4c95d 60%, #2f7d4f);
background-clip: text;
color: transparent;
}
}
Unprefixed background-clip: text works in Chrome 120, Safari 14 and Firefox; Firefox's support is listed as partial: it fails with background-attachment: fixed and in multi-layer backgrounds that mix clip values. Keep the transparent color inside @supports (article 80), so text never becomes invisible, and check the contrast of every part of the gradient against the page: WCAG contrast applies to the lightest part of the text. In forced colors mode, browsers draw the text in a system color.
A newer value, border-area, paints the background only under the border, so a gradient border can follow border-radius. It's in Chrome 150 and Safari 18.2, not Firefox.
background-attachment
| Value | The background... |
|---|---|
scroll (initial) | is fixed to the element, and moves with it when the page scrolls, but not when the element's own content scrolls |
local | scrolls together with the element's content, when the element itself scrolls |
fixed | is fixed to the viewport: positioned and sized relative to it, and painted only where the element is |
fixed creates a parallax-like effect where content slides over a still image. It has costs: it can force the browser to repaint on every scroll, and iOS Safari ignores it. An <img> with position: fixed or sticky is more reliable.
local is useful for scroll shadows: a shadow at the top of a scrolling list that's covered by a matching white layer once the content scrolls back to the top. That technique uses multiple backgrounds, in article 49.
What's current
background-clip: textis unprefixed in all browsers (Chrome since 120); Firefox's support is listed as partial.background-clip: border-areais in Chrome 150 and Safari 18.2.- Four-value offsets (
right 1rem bottom 0.5rem),space,round,local, and thebackground-position-xand-ylonghands work everywhere. - Image formats such as AVIF and WebP work in
url()everywhere;image-set()picks a resolution or format (article 53).
Common questions
Why is my whole page colored when I only styled <body>?
If the root element has no background, the body's background is painted on the whole canvas.
Why does my background image not show?
Check the URL: in a stylesheet, relative URLs are resolved against the stylesheet's address, not the page's. Also check the element has a size: an empty <div> with no height has no area to paint.
What's the difference between cover and contain?
cover fills the area completely and may crop; contain shows the whole image and may leave empty space.
How do I keep an icon 1rem from the right edge of a button?
background-position: right 1rem center;, with background-repeat: no-repeat.
Why does my background show under the border?
background-clip is border-box by default. Use padding-box to stop the background at the border.
Summary
background-coloris painted under the images. The root's (or body's) background fills the canvas.background-imagetakesurl()or a gradient; use it for decoration, not content.background-repeat:repeat,repeat-x,repeat-y,no-repeat,spaceandround, one value per axis.background-position: keywords, percentages that align the same point of image and area, lengths, and offsets from named edges.background-size:auto, lengths, percentages of the area,coverandcontain.background-originsets where images are measured from (padding box);background-clipsets where the background is painted (border box), ortext.background-attachment:scroll,localfor scrolling content,fixedfor the viewport.
Practice questions
Where does an image with background-position: 100% 0% go, and does any of it overflow?
Answer
In the top-right corner, with its right edge on the area's right edge. Percentages align the same point of the image and the area, so nothing overflows (if the image is smaller than the area).
Write a rule that puts a 1.25rem icon, icons/arrow.svg, 0.75rem from the right of a button, centered vertically, once.
Answer
.button {
background-image: url("icons/arrow.svg");
background-repeat: no-repeat;
background-position: right 0.75rem center;
background-size: 1.25rem;
}
A card has a 6px semi-transparent border, and its background color shows through, making the border look muddy. Fix it.
Answer
.card { background-clip: padding-box; }
The background stops at the padding edge, so only what's behind the card shows through the border.
A 1200 × 400 pixel image is the background of a 600 × 600 box with background-size: contain. What size is it drawn, and where's the empty space?
Answer
600 × 200 pixels. It's scaled down to fit the width, leaving 400 pixels of empty height, above and below with background-position: center, or below with the default 0% 0%.
Previous: Logical Properties and Writing Modes: inline, block, start and end

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.