CSS Backgrounds
Learn how to style element backgrounds with colors, images, gradients, and advanced background properties in CSS.
CSS backgrounds are used to add visual interest to elements by setting colors, images, gradients, or patterns. Understanding background properties is essential for creating attractive web designs.
Background Color
The background-color property sets the background color of an element.
.element {
background-color: lightblue;
}You can use different color formats:
.color-examples {
/* Named colors */
background-color: red;
/* Hexadecimal */
background-color: #ff5733;
/* RGB */
background-color: rgb(255, 87, 51);
/* RGBA (with transparency) */
background-color: rgba(255, 87, 51, 0.5);
/* HSL */
background-color: hsl(9, 100%, 60%);
}Background Image
The background-image property sets one or more background images for an element.
.hero {
background-image: url('hero-image.jpg');
}Multiple Background Images
You can layer multiple images:
.layered {
background-image:
url('overlay.png'),
url('main-image.jpg');
}Background Repeat
The background-repeat property controls how background images repeat.
.no-repeat {
background-image: url('pattern.png');
background-repeat: no-repeat;
}
.repeat-x {
background-repeat: repeat-x; /* Repeats horizontally */
}
.repeat-y {
background-repeat: repeat-y; /* Repeats vertically */
}
.repeat-both {
background-repeat: repeat; /* Repeats both directions (default) */
}Background Position
The background-position property sets the starting position of a background image.
.positioned {
background-image: url('logo.png');
background-position: center center;
}
/* Different position values */
.positions {
background-position: top left;
background-position: center;
background-position: 50% 50%;
background-position: 20px 40px;
}Background Size
The background-size property specifies the size of background images.
.cover {
background-image: url('image.jpg');
background-size: cover; /* Covers entire element */
}
.contain {
background-size: contain; /* Fits within element */
}
.custom-size {
background-size: 200px 100px; /* Specific dimensions */
}
.percentage {
background-size: 50% auto;
}Background Attachment
The background-attachment property determines if a background image scrolls with the page or stays fixed.
.fixed-bg {
background-image: url('background.jpg');
background-attachment: fixed; /* Parallax effect */
}
.scroll-bg {
background-attachment: scroll; /* Default behavior */
}Background Origin & Clip
Control where the background is positioned and clipped:
.box {
background-origin: padding-box; /* padding-box | border-box | content-box */
background-clip: padding-box; /* padding-box | border-box | content-box | text */
}Background Shorthand
Combine all background properties in a single declaration:
.shorthand {
background: #f0f0f0 url('image.jpg') no-repeat center center / cover;
/* color image repeat position / size */
}Practical Example
<!DOCTYPE html>
<html>
<head>
<style>
.hero-section {
background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
url('hero-bg.jpg') no-repeat center center / cover;
height: 400px;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.card {
background-color: white;
background-image: url('pattern.png');
background-repeat: repeat;
background-size: 50px 50px;
padding: 20px;
}
</style>
</head>
<body>
<div class="hero-section">
<h1>Welcome to Our Site</h1>
</div>
<div class="card">
<p>This card has a patterned background</p>
</div>
</body>
</html>Best Practices
Background Tips
- Always provide a fallback background color when using images
- Use
background-size: coverfor full-screen backgrounds - Optimize image sizes for better performance
- Consider using CSS gradients instead of images when possible
- Test background combinations for readability
Common Use Cases
- Hero Sections - Large header images with overlay text
- Patterns - Repeating patterns for texture
- Gradients - Modern color transitions
- Card Backgrounds - Subtle backgrounds for content cards
- Fixed Backgrounds - Parallax scrolling effects
How is this guide?
Sign in to share your feedback
Help us improve by sharing your thoughts on this guide.
Last updated on
CSS Colors
The color property of CSS helps to set the color of the HTML element(s). This helps to set the foreground color of text, text decorations, and borders.
CSS Borders
Master CSS border properties to add, style, and customize borders around elements with colors, styles, widths, and rounded corners.
© 2026CoderrShyamAll Rights Reserved.