π Introduction
The background-size property in CSS controls how a background image is scaled inside an element. It allows you to stretch, fit, cover, or auto-scale images to create hero sections, cards, banners, and responsive designs. Mastering background-size is essential for modern UI layouts. π¨β¨
π― What Does background-size Do?
- Controls the width and height of background images
- Defines whether images should stretch or maintain aspect ratio
- Works with multiple background layers
- Helps create responsive hero sections, patterns, and textures
β¨ Syntax
Syntax
selector {
background-size: auto | cover | contain | <length> | <percentage> | <length> <length>;
}Note
100px 50px), the first is width, the second is height.π§© Available Values
1. auto (default)
Keeps the image at its original size.
auto
background-size: auto;2. cover
Scales the image so it completely covers the element while maintaining aspect ratio. This is perfect for hero images & full backgrounds.
cover
background-size: cover;3. contain
Scales the image to make sure the entire image is visible. Some empty space may appear, but the whole image fits.
contain
background-size: contain;4. Length Values
Length Example
background-size: 200px 150px;5. Percentage Values
Percentages are relative to the elementβs dimensions, not the image size.
Percentage Example
background-size: 100% 50%;6. Single Value
When only one value is given, it applies to the width. Height becomes auto.
Single Value
background-size: 300px;7. Multiple Background Layers
Multiple Sizes
background-image: url("pattern.png"), url("texture.jpg");
background-size: 50px 50px, cover;π§© Practical Examples
1. Fullscreen Hero Banner
Hero Section
.hero {
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
}2. Showing Full Image Inside a Box
Contain Example
.profile {
background-image: url("portrait.jpg");
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}3. Fixed Background Icon
Icon Example
.icon {
background-image: url("icon.png");
background-size: 32px 32px;
}4. Responsive Tile Pattern
Tile Example
.pattern {
background-image: url("tile.png");
background-size: 20% auto;
}5. Layered Background Effects
Layered Background
.layer {
background-image:
linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)),
url("bg.jpg");
background-size: cover, contain;
}π Comparison: cover vs contain
| Value | Behavior | Best For |
|---|---|---|
| cover | Fills entire area, image may crop | Hero sections, banners |
| contain | Shows entire image, may leave space | Logos, product photos |
πΌοΈ Visual Demo
Further details can be found at: https://dummyimage.com/900x260/eeeeee/000000&text=background-size+demo
π Conclusion
The background-size property gives you full control over how background images scale and fit inside elements. From responsive hero sections to precise image icons, knowing when to use cover,contain, percentages, or fixed sizes lets you create beautiful, flexible designs. Master it β and your backgrounds will always look perfect. π