On this page
CSS Backgrounds
What Are CSS Backgrounds?
CSS backgrounds paint the area behind an element's content and padding (and border, depending on settings). Our focus keyword is CSS backgrounds, and you will learn every background property, plus trending techniques: CSS background-size cover vs contain, multiple backgrounds, background-clip text (gradient text), background-blend-mode, CSS patterns without images, and responsive hero sections.
The Background Properties
| Property | Purpose | Common values |
|---|---|---|
background-color | Solid color behind everything | #fff, transparent |
background-image | Image or gradient layer | url(hero.jpg), linear-gradient() |
background-repeat | Tiling behavior | repeat, no-repeat, repeat-x, space, round |
background-position | Where the image sits | center, top right, 50% 20% |
background-size | Image scaling | auto, cover, contain, 200px 100px |
background-attachment | Scroll behavior | scroll, fixed, local |
background-origin | Where position starts | padding-box, border-box, content-box |
background-clip | Where the paint is cut off | border-box, padding-box, content-box, text |
background-blend-mode | How layers blend | multiply, overlay, screen |
background-color
The color is painted below all image layers. Always set a background color as a fallback in case an image fails to load, and to guarantee text contrast.
background-image
Accepts url(), gradients and image-set() for high-DPI images. Use CSS backgrounds for decorative images. If the image carries meaning, use an HTML <img> with alt text instead, because backgrounds are invisible to screen readers and search engines.
background-size: cover vs contain
coverscales the image to fill the whole box and crops the overflow. Best for hero sections.containscales the image so it fully fits inside the box, possibly leaving empty space.autouses the natural size. Two values set width and height, for example40px 40px.
background-position and repeat
Position accepts keywords, lengths and percentages. Use center to focus the subject. The four-value syntax right 20px bottom 10px offsets from edges. For patterns, use repeat. For a single logo, use no-repeat.
background-attachment
fixed pins the background to the viewport for a simple parallax effect. It can be janky on mobile browsers, and iOS Safari historically ignores it, so test carefully.
background-clip and origin
background-clip: text with a transparent text color creates gradient text. Add -webkit-background-clip: text for older Safari and Chrome versions. padding-box and content-box help make borders and inner frames.
Multiple Backgrounds
Separate layers with commas. The first layer is on top. Only the last layer may contain a background-color in the shorthand.
.hero {
background:
linear-gradient(to top, rgb(0 0 0 / 70%), transparent) ,
url(hero.jpg) center / cover no-repeat,
#0f172a;
}The background Shorthand
Syntax order: background: color image position / size repeat attachment origin clip. Position and size must be written with a slash between them: center / cover. Remember that the shorthand resets every unspecified sub-property to its initial value, which can accidentally erase earlier settings.
background-blend-mode
Blend an image with the background color or another layer. Example: background-color: #2563eb; background-image: url(photo.jpg); background-blend-mode: multiply; gives a duotone look, a very trendy effect.
Patterns Without Images
Gradients can draw stripes, grids and dots using tiny repeating tiles, saving HTTP requests. Combine background-size with gradients to build any pattern.
Performance and Accessibility
- Compress images and use modern formats (WebP, AVIF). Use
image-set()for retina versions. - Large hero backgrounds affect LCP. For above-the-fold hero images, an
<img>withfetchpriority='high'is often faster than a CSS background. - Always ensure text over images has enough contrast, using overlay gradients.
- Do not put essential content in background images.
Best Practices
- Set a fallback
background-color. - Use
coverpluscenterfor hero backgrounds. - Add a dark gradient overlay for readable text.
- Use the shorthand carefully, or use longhand properties for clarity.
Common Mistakes
- Forgetting the slash in
center / cover. - Placing a color in a non-final layer of the shorthand.
- Expecting a background image to show without giving the element a height.
- Using
background-attachment: fixedon mobile without testing.
Summary
Backgrounds combine color, images and gradients in stackable layers. Learn size, position, repeat, clip and blend modes, and you can build heroes, patterns and gradient text with pure CSS. Try the examples below.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Press Run to execute.
Style .hero so it is 240px tall, uses two background layers (a dark gradient overlay from bottom to transparent on top, and a blue-to-purple linear gradient below), and has a fallback background color. The heading must be white and readable.
Press Run to execute.
Show expected output
A 240px hero with a layered gradient and readable white text.This is a self-check — compare your result with the expected output above.
Use background-clip: text to give the h1 a horizontal gradient from orange to pink. Include the -webkit- prefixed version too.
Press Run to execute.
Show expected output
The heading text shows an orange to pink gradient.This is a self-check — compare your result with the expected output above.
Was this page helpful?