CSS Reference
background
Controls everything behind an element's content — color, image, gradient, and layering.
Last updated: October 2026
Definition: The background property is a shorthand for eight sub-properties. It controls everything behind an element's content — solid colors, images, gradients, and multiple layers. It does not affect foreground text or borders.
🎬 Live Examples
Five background types, side by side.
💡 Gradients are treated as images — they use the background-image property.
Basic Syntax
/* Color only */
.box {
background-color: #3b82f6;
}
/* Gradient */
.hero {
background: linear-gradient(135deg, #3b82f6, #8b5cf6);
}
/* Full shorthand */
.card {
background: url('pattern.png') no-repeat center / cover #0f172a;
}
🎯 All background Sub-Properties
| Property | Purpose |
|---|---|
background-color | Solid color fill. Any color value. |
background-image | Image or gradient. url(...), linear-gradient(), etc. |
background-position | Placement of the image: center, top left, 50% 50%. |
background-size | cover, contain, auto, or specific dimensions. |
background-repeat | no-repeat, repeat, repeat-x, repeat-y. |
background-attachment | scroll, fixed, local. Parallax effect. |
background-origin | Where the background starts: padding-box, border-box, content-box. |
background-clip | Where the background shows: text, padding-box, border-box. |
Gradients
The most useful background feature after solid colors.
/* Linear — angle and two colors */
.hero {
background: linear-gradient(135deg, #3b82f6, #8b5cf6);
}
/* Directional keywords */
.button {
background: linear-gradient(to right, #10b981, #3b82f6);
}
/* Three-color gradient */
.rainbow {
background: linear-gradient(90deg, #ef4444, #f59e0b, #10b981);
}
/* Radial — from center outward */
.glow {
background: radial-gradient(circle, #3b82f6, #020617);
}
/* Conic — around a center point */
.pie {
background: conic-gradient(#3b82f6, #8b5cf6, #ec4899, #3b82f6);
}
/* Hard color stops (stripes, not smooth blends) */
.stripes {
background: linear-gradient(
45deg,
#3b82f6 25%,
#1e40af 25% 50%,
#3b82f6 50% 75%,
#1e40af 75%
);
}
Background Image with Cover
The classic hero background pattern.
.hero {
background-image: url('/images/hero.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
min-height: 100vh;
}
/* Shorthand */
.hero {
background: url('/images/hero.jpg') center / cover no-repeat;
}
background-size values:
cover— scale to cover, crop excess. Best for hero images.contain— scale to fit, leave empty space. Best for logos.auto— natural size.100% 100%— stretch to fill (may distort).
Multiple Backgrounds
Layer several images, gradients, or colors with commas.
.card {
background:
url('icon.svg') no-repeat top right,
linear-gradient(135deg, #1e293b, #0f172a);
}
/* Text overlay pattern with a dark scrim */
.hero {
background:
linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.7)),
url('/images/hero.jpg') center / cover no-repeat;
}
The first layer is on top. The last layer is at the bottom.
background-clip: text — Gradient Text
Fill text with a gradient or image.
.gradient-text {
background: linear-gradient(90deg, #3b82f6, #8b5cf6);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
}
💡 Note: The -webkit- prefix is still recommended for Safari compatibility. Add a fallback solid color for browsers that don't support it.
Sticky Background Attachment
.parallax {
background: url('/images/bg.jpg') center / cover fixed;
}
background-attachment: fixed creates a parallax effect where the background doesn't scroll with the page. Note: it's often broken on mobile — test carefully or disable it below a certain breakpoint.
🎯 background Shorthand Order
| Property | Example |
|---|---|
| background-color | #0f172a |
| background-image | url('image.jpg') |
| background-position | center |
| background-size | cover (after the /) |
| background-repeat | no-repeat |
| background-attachment | fixed |
Order doesn't strictly matter, but the / between position and size is required. Example: background: url(...) center / cover no-repeat;.
⚠️ Common Mistakes
- Forgetting
background-size: cover. Without it, large images render at natural size and can break layout. - Not adding
background-repeat: no-repeat. By default, backgrounds repeat. Small images will tile. - Text on busy images without a scrim. Add a dark gradient overlay for readability.
- Using gradients in
background-color. Gradients are images. Usebackground-imageorbackground. - Relying on
background-attachment: fixedon mobile. It's broken on iOS Safari. Disable on mobile or use a different parallax technique. - Background images with no alt text equivalent. Background images are decorative only — they aren't announced by screen readers. Use
<img>for content images. - Not compressing background images. A hero image at 5MB destroys your page speed. Use WebP or AVIF at the right size.
♿ Accessibility Notes
- Background images are invisible to screen readers. If the image conveys meaning, use
<img>with alt text instead. - Ensure text contrast against backgrounds. A busy image behind text often fails WCAG. Add a scrim (dark or light overlay) with a gradient.
- Test with high-contrast modes. Some users enable forced colors. Backgrounds may be removed by the OS — content must still make sense.
- Don't rely on color alone. A red background as an error indicator needs accompanying text and icon.
- Avoid
background-attachment: fixed. Users with vestibular disorders can experience motion sickness. Respectprefers-reduced-motion.
❓ Frequently Asked Questions
What is the CSS background property?
The background property is a shorthand for eight sub-properties: background-color, background-image, background-position, background-size, background-repeat, background-attachment, background-origin, and background-clip. It sets everything behind an element's content.
What is the difference between background-color and background-image?
background-color sets a solid color fill. background-image sets an image, gradient, or multiple layers. They can be combined — the color shows through transparent areas of the image.
How do I create a gradient in CSS?
Use background or background-image with linear-gradient(), radial-gradient(), or conic-gradient(). Example: background: linear-gradient(135deg, #3b82f6, #8b5cf6). Gradients are treated as images, not colors.
What is background-size: cover vs contain?
cover scales the image to cover the entire element, possibly cropping it. contain scales the image to fit inside the element, possibly leaving empty space. cover is used for hero backgrounds; contain for logos and icons.
How do I add multiple background images?
Separate them with commas in the background shorthand. The first layer is on top. Example: background: url('top.png') no-repeat, url('bottom.png') no-repeat;.