CSS backgrounds allow you to style the background of elements with colors, images, gradients, and patterns. Understanding background properties helps create visually appealing designs with depth, texture, and visual hierarchy.
The background-color property sets the background color of an element:
/* Basic background colors */
.header {
background-color: #2e7d52;
}
.sidebar {
background-color: rgb(255, 255, 255);
}
.footer {
background-color: rgba(0, 0, 0, 0.8);
}
/* Using CSS color names */
.warning {
background-color: yellow;
}
.success {
background-color: lightgreen;
}
.info {
background-color: lightblue;
}
| Format | Example | Use Case |
|---|---|---|
| Color Name | background-color: red; |
Quick prototyping |
| Hex Code | background-color: #ff0000; |
Standard web colors |
| RGB | background-color: rgb(255, 0, 0); |
RGB color control |
| RGBA | background-color: rgba(255, 0, 0, 0.5); |
Transparent colors |
| HSL | background-color: hsl(0, 100%, 50%); |
Intuitive color control |
The background-image property sets one or more background images for an element:
/* Single background image */
.hero-section {
background-image: url('hero-image.jpg');
}
/* Multiple background images */
.layered-bg {
background-image:
url('pattern.png'),
url('texture.jpg'),
url('gradient.png');
}
/* No background image */
.no-bg {
background-image: none;
}
| Format | Best For | Transparency | File Size |
|---|---|---|---|
| JPG/JPEG | Photographs | No | Small |
| PNG | Graphics, logos | Yes | Medium |
| WebP | Modern web | Yes | Very Small |
| SVG | Vector graphics | Yes | Variable |
The background-repeat property controls how background images repeat:
/* Repeat values */
.repeat-x {
background-repeat: repeat-x; /* Repeat horizontally */
}
.repeat-y {
background-repeat: repeat-y; /* Repeat vertically */
}
.no-repeat {
background-repeat: no-repeat; /* Don't repeat */
}
.repeat {
background-repeat: repeat; /* Repeat both directions (default) */
}
.space {
background-repeat: space; /* Space images evenly */
}
.round {
background-repeat: round; /* Scale and repeat to fit */
}
/* Multiple backgrounds with different repeat */
.mixed-repeat {
background-image: url('pattern.png'), url('border.png');
background-repeat: repeat, no-repeat;
}
The background-position property sets the starting position of background images:
/* Keyword positions */
.top-left {
background-position: top left;
}
.center {
background-position: center;
}
.bottom-right {
background-position: bottom right;
}
/* Percentage positions */
.fifty-percent {
background-position: 50% 50%;
}
/* Unit positions */
.pixels {
background-position: 20px 30px;
}
/* Mixed values */
.mixed {
background-position: 20px center;
}
/* Multiple backgrounds */
.multi-position {
background-image: url('icon.png'), url('pattern.jpg');
background-position: top right, center;
}
The background-size property controls the size of background images:
/* Size values */
.cover {
background-size: cover; /* Cover entire area */
}
.contain {
background-size: contain; /* Fit within area */
}
.auto {
background-size: auto; /* Original size (default) }
}
/* Specific dimensions */
.fixed-size {
background-size: 300px 200px;
}
.percentage {
background-size: 50% 100%;
}
/* Multiple backgrounds */
.multi-size {
background-image: url('pattern.png'), url('photo.jpg');
background-size: 100px, cover;
}
| Value | Behavior | Best For |
|---|---|---|
cover |
Scales to cover entire area | Hero images, full backgrounds |
contain |
Fits within area | Logos, icons, patterns |
The background-attachment property controls whether background images scroll with the page:
/* Attachment values */
.scroll {
background-attachment: scroll; /* Scroll with content (default) */
}
.fixed {
background-attachment: fixed; /* Fixed position */
}
.local {
background-attachment: local; /* Scroll with element content */
}
/* Parallax effect */
.parallax {
background-image: url('parallax.jpg');
background-attachment: fixed;
background-position: center;
background-repeat: no-repeat;
background-size: cover;
}
The background-clip property defines how far the background extends:
/* Clip values */
.border-box {
background-clip: border-box; /* Extend to border (default) */
}
.padding-box {
background-clip: padding-box; /* Extend to padding */
}
.content-box {
background-clip: content-box; /* Extend to content only */
}
.text {
background-clip: text; /* Clip to text shape */
-webkit-background-clip: text; /* WebKit prefix */
color: transparent;
}
The background-origin property specifies where the background positioning area starts:
/* Origin values */
.border-origin {
background-origin: border-box; /* From border edge */
}
.padding-origin {
background-origin: padding-box; /* From padding edge */
}
.content-origin {
background-origin: content-box; /* From content edge */
}
Gradients are smooth transitions between two or more colors:
/* Basic linear gradient */
.linear-gradient {
background: linear-gradient(red, blue);
}
/* Directional gradient */
.directional {
background: linear-gradient(to right, red, blue);
}
/* Angle gradient */
.angle {
background: linear-gradient(45deg, red, blue);
}
/* Multiple colors */
.multi-color {
background: linear-gradient(red, yellow, green, blue);
}
/* Color stops */
.stops {
background: linear-gradient(red 0%, yellow 25%, green 50%, blue 100%);
}
/* Repeating gradient */
.repeating-linear {
background: repeating-linear-gradient(
45deg,
red,
red 10px,
blue 10px,
blue 20px
);
}
/* Basic radial gradient */
.radial-gradient {
background: radial-gradient(circle, red, blue);
}
/* Elliptical gradient */
.elliptical {
background: radial-gradient(ellipse, red, blue);
}
/* Positioned gradient */
.positioned {
background: radial-gradient(circle at top left, red, blue);
}
/* Multiple colors */
.radial-multi {
background: radial-gradient(circle, red, yellow, green, blue);
}
/* Repeating radial */
.repeating-radial {
background: repeating-radial-gradient(
circle,
red,
red 10px,
blue 10px,
blue 20px
);
}
/* Conic gradient */
.conic-gradient {
background: conic-gradient(red, yellow, green, blue, red);
}
/* Positioned conic */
.conic-positioned {
background: conic-gradient(from 45deg at 50% 50%, red, yellow, green, blue);
}
The background shorthand property combines multiple background properties:
/* Basic shorthand */
.basic-shorthand {
background: #f0f0f0 url('image.jpg') no-repeat center center;
}
/* Full shorthand */
.full-shorthand {
background:
#ffffff
url('pattern.png')
no-repeat
scroll
top left
padding-box
padding-box;
}
/* Multiple backgrounds shorthand */
.multi-shorthand {
background:
url('pattern.png') repeat-x top left,
url('gradient.jpg') no-repeat center center;
}
/* Gradient shorthand */
.gradient-shorthand {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
.hero {
background-image: url('hero-bg.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
min-height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.gradient-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border-radius: 15px;
padding: 30px;
color: white;
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
}
.pattern-bg {
background-color: #f8f9fa;
background-image:
repeating-linear-gradient(
45deg,
transparent,
transparent 10px,
rgba(0, 0, 0, 0.1) 10px,
rgba(0, 0, 0, 0.1) 20px
);
}
.glass-effect {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 20px;
}
.text-bg {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
-webkit-background-clip: text;
background-clip: text;
color: transparent;
font-size: 3rem;
font-weight: bold;
}
.animated-gradient {
background: linear-gradient(270deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
background-size: 800% 800%;
animation: gradientShift 15s ease infinite;
}
@keyframes gradientShift {
0% { background-position: 0% 50%; }
50% { background-position: 100% 50%; }
100% { background-position: 0% 50%; }
}
/* Dots pattern */
.dots-pattern {
background-color: #e0e0e0;
background-image:
radial-gradient(circle, #333 1px, transparent 1px);
background-size: 20px 20px;
}
/* Stripes pattern */
.stripes-pattern {
background: repeating-linear-gradient(
45deg,
#606dbc,
#606dbc 10px,
#465298 10px,
#465298 20px
);
}
/* Grid pattern */
.grid-pattern {
background-color: #fff;
background-image:
linear-gradient(#ccc 1px, transparent 1px),
linear-gradient(90deg, #ccc 1px, transparent 1px);
background-size: 20px 20px;
}
| Feature | Browser Support | Notes |
|---|---|---|
| Basic backgrounds | All browsers | Excellent support |
| Linear gradients | IE10+, all modern | Prefixes needed for older browsers |
| Radial gradients | IE10+, all modern | Prefixes needed for older browsers |
| Conic gradients | Modern browsers | No IE support |
| background-clip: text | Modern browsers | Requires -webkit- prefix |
/* Problem: Element has no height */
.empty-element {
background-image: url('bg.jpg');
/* No height set */
}
/* Solution: Add height or content */
.empty-element {
background-image: url('bg.jpg');
min-height: 200px;
/* Or add padding/content */
}
/* Fallback for older browsers */
.modern-gradient {
background-color: #ff6b6b; /* Fallback */
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
/* Modern approach with @supports */
@supports (background: linear-gradient(45deg, red, blue)) {
.modern-gradient {
background: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
}
background-color. If the image fails to load (broken path, slow network), an element with only background-image set shows nothing at all. Always set a sensible background-color underneath.
background-size/background-position. Without them, a large image tiles or a small one gets cropped from the top-left — rarely what you actually want.
<img alt="..."> content is. Reserve them for decoration only.
Layering a linear-gradient on top of a background image (a common real-world hero-section pattern) so text stays readable over any photo:
.hero {
background-image:
linear-gradient(rgba(0,0,0,0.55), rgba(0,0,0,0.55)),
url('mountain.jpg');
background-size: cover;
background-position: center;
color: white;
padding: 60px 20px;
}
Rendered result (using a solid color in place of a real photo):
Q: Should photos that carry meaning use background-image or <img>?
A: Use <img> with a real alt for anything meaningful (a product photo, a diagram). Reserve background-image for purely decorative backdrops.
Q: How do I stop a background image from repeating?
A: Set background-repeat: no-repeat; — by default, CSS tiles a background image to fill the element.
Q: What's the difference between background-size: cover and contain?
A: cover fills the whole element (cropping overflow); contain fits the entire image inside without cropping (may leave empty space). See the MDN background-size reference for more.