CSS Backgrounds
In this page:
selector {
background-color: color;
background-image: url("image.jpg");
background-repeat: repeat | no-repeat;
}
background-color
The background-color property fills the space behind an element's content with a solid color, using any of the color formats CSS supports. This is one of the most frequently used CSS properties, forming the base layer beneath any background image.
- A subtle, light background-color is an easy way to visually separate one section of a page from another.
- Accepted values:
- color — any CSS color: name, hex, rgb(), hsl()
- transparent — no color (initial value)
- currentcolor — the element's text color
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: background-color
<style>
.box {
background-color: lightblue;
}
</style>
<div class="box">Filled with a solid background color</div>
background-image
The background-image property places an image behind an element's content, referenced with url(). It can be combined with a background-color as a fallback while the image loads.
- Always set a background-color close to your image's dominant color, so the layout still looks reasonable if the image fails to load.
- Accepted values:
- url("...") — an image file
- none — no image (initial value)
- linear-gradient() — a straight-line color gradient
- radial-gradient() — a circular or elliptical gradient
- conic-gradient() — a gradient swept around a center point
- several values separated by commas — stacked layers
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: background-image
<style>
.box {
background-color: #ddd;
background-image: url('https://placehold.co/300x150');
}
</style>
<div class="box">Image with a fallback color behind it</div>
background-repeat and background-position
By default, a background image tiles to fill its element. background-repeat controls whether it tiles at all, and background-position controls exactly where the image starts. Getting these two properties wrong is a common cause of a background image looking stretched, cut off, or misaligned.
- Set background-repeat: no-repeat whenever you want a single, clean image instead of a tiled pattern.
- Accepted values:
- repeat | no-repeat | repeat-x | repeat-y | space | round — tiling for background-repeat
- left / center / right — horizontal position keywords
- top / center / bottom — vertical position keywords
- length — offset from the top-left, e.g. 20px 10px
- percentage — 50% 50% centers the image
- edge offsets — e.g. right 10px bottom 20px
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: background-repeat and background-position
<style>
.box {
background-image: url('https://placehold.co/50x50');
background-repeat: no-repeat;
background-position: center;
height: 150px;
}
</style>
<div class="box">Single centered image, no tiling</div>
background-size
background-size controls how large a background image renders. Special keywords like cover scale the image to fill its element completely, while contain scales it to fit fully inside without cropping.
- Use background-size: cover for full-width hero banners, it fills the space nicely regardless of the container's exact dimensions.
- Accepted values:
- auto — the image's natural size (initial value)
- cover — scales to fill the box, may crop
- contain — scales to fit fully inside, may leave gaps
- length — e.g. 200px or 10rem
- percentage — relative to the background positioning area
- two values — width then height, e.g. 50% auto
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: background-size
<style>
.box {
background-image: url('https://placehold.co/300x150');
background-size: cover;
height: 150px;
}
</style>
<div class="box">Fills the element completely</div>
Shorthand background Property
The background shorthand property lets you set color, image, repeat, position, and size all in one declaration, instead of writing five separate properties. Using the shorthand can accidentally reset properties you didn't explicitly include, so it's worth double-checking the full value list.
- Once you're comfortable with the individual background properties, the shorthand version keeps your CSS shorter without losing clarity.
- Accepted values:
- color — e.g. #eee
- image — url() or a gradient
- position / size — e.g. center / cover
- repeat — e.g. no-repeat
- attachment — scroll, fixed or local
- origin and clip — border-box, padding-box or content-box
- inherit | initial | unset | revert — CSS-wide keywords accepted by every property
Example: Shorthand background Property
<style>
.box {
background: #ddd url('https://placehold.co/300x150') no-repeat center / cover;
height: 150px;
}
</style>
<div class="box">Five properties in one declaration</div>
- Forgetting to set a fallback background-color when using a background-image, in case the image fails to load.
- Not setting background-size, which can leave a background image far too large or oddly cropped.
- Overusing background-repeat when a single, well-positioned image would look cleaner.
- background-color sets a solid color behind an element's content.
- background-image, background-repeat, background-position, and background-size control how an image background behaves.
- The background shorthand property can combine several of these settings into a single declaration.
All standard CSS background properties covered here are supported by every modern browser.
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- CSS Colors
- CSS RGB Colors
- CSS HEX Colors
- CSS HSL Colors
- CSS Color Keywords
- CSS Backgrounds
- CSS Background Color
- CSS Background Image
- CSS Background Repeat
- CSS Background Attachment
- CSS Background Shorthand
- CSS Multiple Backgrounds
- CSS Background Size
- CSS Background Origin
- CSS Background Clip
- CSS Borders
- CSS Border Style
- CSS Border Width
- CSS Border Color
- CSS Border Sides
- CSS Border Shorthand