CSS background properties control an element’s background color, image, size, position, and behavior. Key ones include background (shorthand), background-size, background-origin, and background-clip.
background: Shorthand to set image, position, repeat, size, attachment, and color in one line.background-size: Controls image scaling (e.g., cover, contain, or custom width/height).background-origin: Decides which box (border, padding, content) the image is positioned from.background-clip: Defines how far the background is painted (up to border, padding, content, or text).Common Background Properties (Cheat Sheet)
| Property | Description | Example |
|---|---|---|
background |
Shorthand for all background values | background: url(bg.jpg) no-repeat center/cover; |
background-size |
Controls image size | background-size: contain; |
background-origin |
Sets image reference box | background-origin: border-box; |
background-clip |
Limits background drawing | background-clip: content-box; |
background (Shorthand)Set multiple background-related values in a single declaration.
/* Shorthand: image, repeat, position, size, attachment, color */
.hero-section {
background: url("bg.jpg") no-repeat center/cover fixed #eef;
}
background-sizeControl how the image fits inside the element box.
/* 'cover' fills the box, 'contain' shows the whole image */
.banner {
background-image: url("banner.jpg");
background-repeat: no-repeat;
background-position: center;
background-size: cover;
}
.logo-box {
background-image: url("logo.png");
background-repeat: no-repeat;
background-position: center;
background-size: contain;
}
background-originDecide from where the background image’s position is calculated.
/* Same image, different origin boxes for positioning */
.card-padding {
border: 10px solid #4caf50;
padding: 20px;
background-image: url("pattern.png");
background-origin: padding-box; /* default */
}
.card-border {
border: 10px solid #4caf50;
padding: 20px;
background-image: url("pattern.png");
background-origin: border-box;
}
.card-content {
border: 10px solid #4caf50;
padding: 20px;
background-image: url("pattern.png");
background-origin: content-box;
}
background-clipControl how far the background painting extends, and create gradient text.
/* Clip background to content area and to text only */
.box-clip {
border: 10px solid #4caf50;
padding: 20px;
background: url("texture.png") no-repeat center;
background-clip: content-box;
}
.gradient-title {
font-size: 3rem;
font-weight: 800;
background-image: linear-gradient(45deg, #ec4899, #8b5cf6);
background-repeat: no-repeat;
background-size: cover;
background-position: center;
-webkit-background-clip: text;
background-clip: text;
-webkit-text-fill-color: transparent;
}
This box combines multiple background properties:
background-image uses a gradient overlay + photo.background-size: cover makes the photo fill the box.background-origin: padding-box starts painting inside the padding edge.background-clip: border-box lets the background appear under the border too.Because of the dark gradient overlay, the white text and highlighted code stay readable even on top of the image.
background shorthand to keep related properties in one clean line.background-size: cover is ideal for full-width hero sections and banner images.background-size: contain is better when the entire image must be visible.background-origin with borders and padding to avoid awkward image offsets.background-image with background-clip: text and transparent text fill.background-size: contain and compare it with cover.background-origin values; add borders and padding to see the difference clearly.background-clip: text for a modern “glassmorphism” feel.background shorthand and overlay it with a semi-transparent color using an inner element.