CSS Backgrounds
Master background properties to create stunning visual effects
CSS Background Properties
CSS background properties allow you to add background colors, images, gradients, and effects to elements. These properties are essential for creating visually appealing web designs.
Background Properties:
- background-color: Sets the background color
- background-image: Sets a background image
- background-repeat: Controls how background images repeat
- background-position: Sets the position of background images
- background-size: Controls the size of background images
- background-attachment: Controls scrolling behavior
1. Background Color
The background-color property sets the background color of an element.
/* Solid colors */
.header {
background-color: #3498db;
}
.section {
background-color: rgb(52, 152, 219);
}
.card {
background-color: rgba(52, 152, 219, 0.1);
}
/* Named colors */
.warning {
background-color: orange;
}
.success {
background-color: lightgreen;
}
2. Background Image
The background-image property sets one or more background images for an element.
/* Single background image */
.hero {
background-image: url('hero-image.jpg');
}
/* Multiple background images */
.complex-bg {
background-image:
url('overlay.png'),
url('background.jpg');
}
/* Using gradients as background images */
.gradient-bg {
background-image: linear-gradient(45deg, #ff6b6b, #4ecdc4);
}
3. Background Repeat
The background-repeat property controls how background images are repeated.
/* No repeat */
.no-repeat {
background-image: url('pattern.png');
background-repeat: no-repeat;
}
/* Repeat horizontally only */
.repeat-x {
background-image: url('border.png');
background-repeat: repeat-x;
}
/* Repeat vertically only */
.repeat-y {
background-image: url('sidebar.png');
background-repeat: repeat-y;
}
/* Repeat in both directions (default) */
.repeat-all {
background-image: url('texture.png');
background-repeat: repeat;
}
4. Background Position
The background-position property sets the starting position of background images.
/* Keyword positions */
.top-left {
background-position: top left;
}
.center {
background-position: center center;
}
.bottom-right {
background-position: bottom right;
}
/* Percentage positions */
.custom-position {
background-position: 25% 75%;
}
/* Pixel positions */
.exact-position {
background-position: 100px 50px;
}
/* Mixed units */
.mixed-position {
background-position: left 20px top 10px;
}
5. Background Size
The background-size property specifies the size of background images.
/* Cover - scales to cover entire container */
.cover {
background-image: url('large-image.jpg');
background-size: cover;
}
/* Contain - scales to fit within container */
.contain {
background-image: url('logo.png');
background-size: contain;
}
/* Specific dimensions */
.fixed-size {
background-image: url('icon.png');
background-size: 100px 100px;
}
/* Percentage sizing */
.percentage-size {
background-image: url('pattern.png');
background-size: 50% 50%;
}
/* Auto sizing */
.auto-size {
background-image: url('image.jpg');
background-size: auto 200px;
}
6. Background Attachment
The background-attachment property controls whether background images scroll with content.
/* Fixed background (parallax effect) */
.parallax {
background-image: url('mountains.jpg');
background-attachment: fixed;
background-position: center;
background-size: cover;
}
/* Scrolling background (default) */
.scroll {
background-image: url('texture.png');
background-attachment: scroll;
}
/* Local attachment */
.local {
background-image: url('pattern.png');
background-attachment: local;
}
7. Background Shorthand
The background property is a shorthand for setting multiple background properties at once.
/* Shorthand syntax */
.shorthand {
background: #f0f0f0 url('image.jpg') no-repeat center center / cover;
}
/* Equivalent to: */
.longhand {
background-color: #f0f0f0;
background-image: url('image.jpg');
background-repeat: no-repeat;
background-position: center center;
background-size: cover;
}
/* Complex shorthand */
.complex {
background:
linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)),
url('hero.jpg') no-repeat center center / cover;
}
8. CSS Gradients
CSS gradients are smooth transitions between colors that can be used as background images.
/* Linear gradients */
.linear-gradient {
background: linear-gradient(to right, #ff6b6b, #4ecdc4);
}
.diagonal-gradient {
background: linear-gradient(45deg, #667eea, #764ba2);
}
.multi-color {
background: linear-gradient(
to bottom,
#ff6b6b 0%,
#4ecdc4 50%,
#45b7d1 100%
);
}
/* Radial gradients */
.radial-gradient {
background: radial-gradient(circle, #ff6b6b, #4ecdc4);
}
.elliptical-gradient {
background: radial-gradient(ellipse at center, #667eea, #764ba2);
}
9. Multiple Backgrounds
You can apply multiple background images to a single element.
.multiple-backgrounds {
background:
/* Top layer */
url('overlay.png') repeat,
/* Middle layer */
linear-gradient(45deg, rgba(255,0,0,0.3), rgba(0,0,255,0.3)),
/* Bottom layer */
url('texture.jpg') no-repeat center center / cover;
}
/* Separate properties for multiple backgrounds */
.separate-multiple {
background-image:
url('pattern.png'),
url('background.jpg');
background-repeat:
repeat,
no-repeat;
background-position:
top left,
center center;
background-size:
50px 50px,
cover;
}
Practical Examples
Hero Section with Overlay:
.hero-section {
height: 100vh;
background:
linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)),
url('hero-bg.jpg') no-repeat center center / cover;
display: flex;
align-items: center;
justify-content: center;
color: white;
text-align: center;
}
.hero-content h1 {
font-size: 3rem;
margin-bottom: 1rem;
}
.hero-content p {
font-size: 1.2rem;
max-width: 600px;
}
Card with Gradient Background:
.gradient-card {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 2rem;
border-radius: 12px;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
}
.pattern-overlay {
background:
url('data:image/svg+xml,') repeat,
linear-gradient(45deg, #ff6b6b, #4ecdc4);
background-size: 20px 20px, cover;
}
Best Practices
Background Best Practices:
- Optimize images: Use appropriate file formats and sizes
- Provide fallbacks: Always include background-color as fallback
- Consider accessibility: Ensure sufficient contrast with text
- Use CSS gradients: They're scalable and load faster than images
- Test on different devices: Backgrounds may behave differently