The CSS background shorthand property is a powerful and efficient way to set all the background properties for an element in a single declaration. This concise syntax enhances code readability and reduces file size, leading to faster website loading times. By combining multiple background-related values, you can control the color, image, position, size, repeat, and attachment of an element's background with just one line of code.
Example 1: Setting a Solid Background Color
/* Sets the background color of the body to a light gray */
body {
background: #f0f0f0;
}
Explanation
This code sets a simple, solid background color for the <body> element. The hexadecimal color code #f0f0f0 represents a light shade of gray.
Example 2: Adding a Background Image
/* Places a background image that covers the entire element */
.hero-section {
background: url('hero-image.jpg') no-repeat center center/cover;
}
Explanation
This example sets a background image for an element with the class .hero-section. The url() function specifies the image source, no-repeat prevents it from tiling, center center positions it, and cover scales it to fill the element.
Example 3: Tiling a Background Image
/* Creates a repeating pattern background */
.pattern-bg {
background: url('pattern.png') repeat;
}
Explanation
Here, a small image named pattern.png is set as the background. The repeat value causes the image to tile horizontally and vertically, creating a seamless pattern.
Example 4: Fixed Background Image
/* A fixed background image that stays in place during scroll */
.parallax-effect {
background: url('background.jpg') no-repeat center center fixed;
min-height: 500px;
}
Explanation
This code creates a parallax-like effect by setting the background-attachment to fixed. The background image remains stationary in the viewport as the user scrolls down the page.
Example 5: Gradient Background
/* Applies a linear gradient from top to bottom */
.gradient-box {
background: linear-gradient(to bottom, #4a90e2, #0052a3);
}
Explanation
This example demonstrates how to apply a linear gradient as a background. The linear-gradient() function creates a smooth color transition from the starting color #4a90e2 to the ending color #0052a3.
Example 6: Multiple Background Images
/* Layers two background images */
.multi-bg {
background: url('foreground.png') no-repeat top left, url('background-sky.jpg') no-repeat bottom right;
height: 600px;
}
Explanation
The background shorthand allows for layering multiple background images, each with its own properties. In this case, foreground.png is placed on top of background-sky.jpg.
Example 7: Background Color and Image Combined
/* A semi-transparent color over a background image */
.overlay {
background: url('bg.jpg') no-repeat center/cover, rgba(0, 0, 0, 0.5);
background-blend-mode: overlay;
}
Explanation
This code layers a semi-transparent black color (rgba(0, 0, 0, 0.5)) on top of a background image. The background-blend-mode property is used to blend the color and image together.