Backgrounds in CSS control what appears behind the content of an element. A background can be a solid color, an image, a gradient, or even multiple layers combined together. Backgrounds are used for page sections, cards, buttons, hero areas, banners, badges, forms, and layout surfaces. A clean background makes content easier to read. A noisy background can make even good content difficult to use.
The most common background properties are background-color, background-image, background-repeat, background-position, background-size, background-attachment, and the shorthand property background. These properties can be used separately for clarity or combined when the design is stable.
Background Color
The background-color property sets a solid color behind an element. It paints the content box and padding area by default. It is one of the most commonly used CSS properties because almost every design has surfaces such as body backgrounds, cards, callout boxes, buttons, navigation bars, and form fields.
body {
background-color: #f5f5f5;
}
.card {
background-color: #ffffff;
}
In this example, the page gets a light gray background while cards remain white. This creates separation without needing a heavy border or shadow. Subtle background contrast is often enough to guide the eye.
Background Image
The background-image property places an image behind an element. The image is loaded using the url() function. Unlike an HTML <img> element, a background image is decorative from the document structure point of view. If the image carries important content, use a real image element instead.
.hero {
background-image: url("hero.jpg");
background-color: #111160;
}
It is common to include a fallback background color. If the image fails to load, the color still appears. This also helps while the image is loading.
Background Repeat
By default, background images repeat both horizontally and vertically. This is useful for small patterns but usually unwanted for large hero images. The background-repeat property controls this behavior.
.hero {
background-image: url("hero.jpg");
background-repeat: no-repeat;
}
.pattern {
background-image: url("pattern.png");
background-repeat: repeat;
}
| Value | Meaning |
|---|---|
| repeat | Repeats horizontally and vertically |
| no-repeat | Shows the image once |
| repeat-x | Repeats only horizontally |
| repeat-y | Repeats only vertically |
| space | Repeats with spacing so images are not clipped |
| round | Repeats and scales so images fit |
Background Position
The background-position property controls where the background image sits inside the element. It is often used with background-repeat: no-repeat and background-size.
.banner {
background-image: url("banner.jpg");
background-repeat: no-repeat;
background-position: center center;
}
The value center center centers the image horizontally and vertically. You can also use values such as top right, bottom left, 50% 50%, or length values such as 20px 40px.
Background Size
The background-size property controls how large the background image appears. The most common values are cover and contain.
.hero {
background-image: url("hero.jpg");
background-size: cover;
background-position: center;
}
.logo-area {
background-image: url("logo.svg");
background-size: contain;
background-repeat: no-repeat;
}
The value cover scales the image so the whole element is covered. Some parts of the image may be cropped. The value contain scales the image so the full image is visible. Empty space may remain inside the element. For hero sections, cover is common. For logos and icons, contain is often better.
Background Attachment
The background-attachment property controls whether the background scrolls with the element or stays fixed relative to the viewport. The value fixed can create a parallax-like effect, but it should be used carefully because it can behave differently on mobile devices and may affect performance.
.section {
background-image: url("texture.jpg");
background-attachment: fixed;
}
For most practical layouts, the default scrolling behavior is enough. Fixed backgrounds should be tested on real mobile browsers before being used in a production design.
Background Shorthand
The background shorthand can set multiple background properties in one declaration. It is compact, but it can also reset values you did not intend to change. Use it when you understand the full background setup.
.hero {
background: #111160 url("hero.jpg") center / cover no-repeat;
}
This shorthand sets the color, image, position, size, and repeat behavior. Notice the slash between position and size. Without the slash, the browser cannot tell where position ends and size begins.
CSS Gradients as Backgrounds
CSS gradients are background images generated by the browser. They do not require an external image file. Gradients are useful for hero sections, overlays, buttons, subtle cards, and decorative backgrounds.
.hero {
background-image: linear-gradient(135deg, #111160, #2c8ffa);
color: white;
}
Gradients can be linear, radial, or conic. A linear gradient moves in a direction. A radial gradient expands from a center point. A conic gradient rotates around a center point. For most UI work, linear gradients are the most common.
Multiple Background Layers
CSS allows multiple background layers separated by commas. The first layer is painted on top, and later layers are painted below it. This is useful for combining overlays with images.
.hero {
background-image:
linear-gradient(rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0.55)),
url("hero.jpg");
background-size: cover;
background-position: center;
}
Here, the gradient overlay sits above the image and darkens it. This helps white text remain readable. Without the overlay, text over a bright image may fail contrast and become hard to read.
Background Origin and Background Clip
Two advanced but useful background properties are background-origin and background-clip. The background-origin property decides where the background image positioning area starts. The background-clip property decides how far the background painting area extends.
.box {
padding: 24px;
border: 8px solid #111160;
background-color: #e6e7ef;
background-clip: padding-box;
}
With background-clip: padding-box, the background does not paint under the border. This is useful when the border should stay visually separate from the background. The common values are border-box, padding-box, and content-box.
Background Blend Mode
The background-blend-mode property controls how background layers blend with each other. It can create tinted image effects without editing the original image file.
.hero {
background-image:
linear-gradient(#111160, #111160),
url("hero.jpg");
background-blend-mode: multiply;
background-size: cover;
}
Blend modes can be useful for visual style, but they should not be used randomly. Always check whether text remains readable and whether the result looks consistent across images.
Responsive Backgrounds
Backgrounds need responsive testing. A background that looks perfect on desktop may crop badly on mobile when background-size: cover is used. Important parts of the image can disappear because the browser crops the image to fill the element.
For responsive designs, choose images with safe empty space around the subject, use background-position intentionally, and adjust the background at breakpoints when needed. Sometimes the best mobile design is not the same image. A simpler gradient or solid background can be more readable on small screens.
@media (max-width: 700px) {
.hero {
background-position: center top;
}
}
Background Shorthand Pitfalls
The background shorthand is powerful, but it resets many background-related longhand properties. If you first set background-size and later write a simple background declaration, the size may be reset unless it is included again in the shorthand. This can create confusing bugs where an image suddenly repeats or loses its cover behavior.
.hero {
background-size: cover;
background: url("hero.jpg") center no-repeat;
}
In this example, the later shorthand does not include the size, so the earlier background-size can be reset. A safer shorthand would include center / cover. When debugging background issues, always check whether a shorthand declaration is overwriting a longhand declaration.
Backgrounds on html and body
Page backgrounds are commonly applied to the body, but the html element can also matter. Browsers have special behavior for canvas backgrounds, and the body background may visually cover the viewport. In normal projects, setting the body background is usually enough, but full-page designs should be tested at short content height and long content height.
For example, if the page content is shorter than the viewport, a section background may not fill the full screen unless minimum heights are set correctly. This is a layout issue, not only a background issue.
Background Image Performance
Large background images can slow pages down. Use appropriately sized images, compress them, and avoid loading giant desktop images for small mobile layouts when possible. A decorative background should not make the main content feel slow. For simple decorative effects, CSS gradients are often lighter than image files.
Also remember that background images do not reserve space by themselves. The element still needs padding, height, content, or layout rules. If an element has no height, the background may technically exist but appear invisible because there is no painted area to show.
Always size the background container intentionally.
Backgrounds and Accessibility
Backgrounds should support content, not fight it. If text appears over a background image or gradient, check contrast carefully. Avoid placing long paragraphs directly over busy images. Use overlays, solid panels, or simpler background areas when readability matters.
- Use solid backgrounds for long reading sections.
- Use overlays when placing text over images.
- Avoid low-contrast text and background combinations.
- Do not use background images for meaningful content.
- Test responsive cropping when using background-size cover.
- Provide a fallback background color.
Common Background Mistakes
- Using background images for important content instead of real images.
- Forgetting
background-repeat: no-repeaton large images. - Using
coverwithout checking how the image crops on mobile. - Using the background shorthand and accidentally resetting other values.
- Putting text over a busy image without an overlay.
- Using fixed backgrounds without testing mobile behavior.
Backgrounds in CSS FAQ
What is the difference between background-color and color?
background-color controls the area behind an element. color controls the text color inside the element.
Should important images be CSS backgrounds?
No. If the image is meaningful content, use an HTML image with proper alt text. CSS background images are best for decoration.
What does background-size cover do?
It scales the image until the element is fully covered. Some parts of the image may be cropped.
Can CSS have multiple backgrounds?
Yes. Multiple background layers can be listed with commas. The first layer appears on top.
Continue learning CSS in order
Follow the topic sequence with the previous and next lesson.