Master Code
On The Go

Learn. Practice. Build.

Home › Code Snippets › CSS Snippets › Reference › background

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.

Solid color
background-color: #3b82f6
Linear gradient
linear-gradient(135deg, blue, purple)
Radial gradient
radial-gradient(circle, blue, dark)
Conic gradient
conic-gradient(...) — colorful pie
Two-color diagonal (the most common)
linear-gradient(to right, green, blue)

💡 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-colorSolid color fill. Any color value.
background-imageImage or gradient. url(...), linear-gradient(), etc.
background-positionPlacement of the image: center, top left, 50% 50%.
background-sizecover, contain, auto, or specific dimensions.
background-repeatno-repeat, repeat, repeat-x, repeat-y.
background-attachmentscroll, fixed, local. Parallax effect.
background-originWhere the background starts: padding-box, border-box, content-box.
background-clipWhere the background shows: text, padding-box, border-box.
1

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%
    );
}
2

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).
3

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.

4

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.

5

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-imageurl('image.jpg')
background-positioncenter
background-sizecover (after the /)
background-repeatno-repeat
background-attachmentfixed

Order doesn't strictly matter, but the / between position and size is required. Example: background: url(...) center / cover no-repeat;.

⚠️ Common Mistakes

♿ Accessibility Notes

❓ 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;.

🎯 What's Next?

← All CSS Reference