LESSON 4.4
CSS Background Images
Powerful image techniques for layouts and design
Learn how to set, size, position, and repeat background images using CSS to create hero sections, textured backgrounds, and more.
๐ก The Concept
Why Background Images Matter in Web Design
CSS background images give you control over how images appear behind your content. Understanding the key properties โ and when to use them โ is what separates a plain layout from a polished one.
Adding a CSS Background Image
The fundamental property for adding a background image in CSS is background-image. You apply it to any element: the body, a div, a section, a header. Then you point it to an image file using the url() function as the CSS value.
This is what the code looks like.
body {
background-image: url(images/my-background.jpg);
}
You can also use an absolute URL:
body {
background-image: url(https://example.com/images/my-background.jpg);
}
When you first add a background image with no other properties, the default behavior may be unexpected.
Understanding Default Background Behavior
By default, a CSS background image will repeat both horizontally and vertically to fill the entire element, much like tiles covering a floor. If the image is smaller than the element, it tiles across the space.

This is rarely what you want for a photographic background. The additional background properties give you full control over how the image is displayed.
Essential Background Image Properties
background-repeat
The background-repeat property controls whether and how the image tiles. The most common values are:
repeat, the default; tiles in both directionsno-repeat, shows the image only oncerepeat-x, tiles horizontally onlyrepeat-y, tiles vertically only
For most full-page or section backgrounds, use:
body {
background-image: url(images/my-background.jpg);
background-repeat: no-repeat;
}

background-repeat: no-repeat prevents the image from tiling.background-size
The background-size property controls how the image is scaled within its container. In everyday practice, two values cover almost every situation.
cover โ for hero sections and photo backgrounds
cover scales the image to completely fill the element, cropping the edges as needed. It is the standard choice whenever you want a photo to fill a section or the full page without empty space or distortion.
body {
background-image: url(images/my-background.jpg);
background-repeat: no-repeat;
background-size: cover;
}

background-size: cover, the image fills the entire element; some cropping may occur at the edges.Because cover crops the image, you will often pair it with background-position to control which part stays visible. For example, if the subject of your photo is in the lower right, background-position: bottom right keeps it in frame. More on that below.
100% 100% โ for abstract and pattern backgrounds
When your background is an abstract image, a texture, or a decorative pattern โ not a photo with a focal point โ 100% 100% stretches the image to exactly fill the element in both dimensions. This works well with images that can be stretched without looking distorted. Photos of real subjects will look wrong; abstract art and textures generally work fine.
body {
background-image: url(images/abstract-texture.jpg);
background-size: 100% 100%;
}
contain โ good to know, rarely used
contain scales the image to fit entirely within the element without any cropping. The full image is always visible, but empty space may appear around it. This is occasionally useful for logos or icons used as backgrounds, but not practical for photos or decorative backgrounds.

background-size: contain, the entire image fits inside the element, but empty space may appear around it.background-position
When an image is cropped, especially with cover, background-position controls which part stays visible. That’s what background-position is for.
body {
background-image: url(images/my-background.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
}
Common values include:
center center, centers the image both horizontally and vertically (most common)top center, aligns to the topbottom right, anchors to the bottom-right corner- Pixel or percentage values:
50% 25%
This is especially useful when the focal point of the photo is not centered, or when you need to keep text away from the subject.
background-attachment
The background-attachment property controls whether the background image scrolls with the page or stays fixed in place as the user scrolls. The two main values are:
scroll, the default; the background scrolls with the contentfixed, creates a parallax-like effect where the background stays in place
body {
background-image: url(images/my-background.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
background-attachment: fixed;
}
The fixed attachment creates a parallax-like look popular on single-page and portfolio sites. Note that background-attachment: fixed does not always work well on mobile devices, so test thoroughly.
The Background Shorthand Property
Once you are comfortable with the individual properties, you can combine them into a single shorthand declaration. The background shorthand is concise:
body {
background: url(images/my-background.jpg) no-repeat center center / cover;
}
The general order is:
background: [image] [repeat] [position] / [size] [attachment];
Note the forward slash (/) between position and size โ this is required in the shorthand. The individual properties are always fine to use when they make the code easier to read.
Putting It All Together: A Practical Example
Here is a common real-world scenario: a hero section with a full-width background image and centered text on top.
<!-- HTML -->
<header class="hero">
<h1>Welcome to My Website</h1>
<p>Building beautiful things on the web.</p>
</header>
/* CSS */
.hero {
background-image: url(images/hero-background.jpg);
background-repeat: no-repeat;
background-size: cover;
background-position: center center;
height: 500px;
color: white;
text-align: center;
}
This is one of the most common patterns in web design. The background image fills the entire header, and the text sits centered on top of it.
CSS Gradients as Backgrounds
You can also use a CSS gradient as a background โ no image file needed. Gradients are covered in detail in the CSS Gradients lesson.
Video Demonstration
In this video I demonstrate how to use background-image in CSS.
Exercise: Try It Yourself
<h1>Background Play</h1>
Step 2. Add this image URL as your background image in the CSS panel:
https://webdevstudents.com/wp-content/uploads/2022/09/hd-wallpaper-2468874_1280-300x300.jpg
Step 3. In the CSS panel, add a background-image rule to the body using that URL:
Step 4. Try these background-repeat values one at a time:
background-repeat: no-repeat;
background-repeat: repeat-x;
background-repeat:repeat-y;
Step 5. Now try these background-size values:
background-size: cover;
background-size: 100% 100%;
Click Run. Your result should look something like this:

โ Frequently Asked Questions
Yes. CSS allows multiple background images on one element by listing them separated by commas. The first image in the list sits on top, and the others layer behind it. This is how the gradient overlay technique works โ the gradient is listed first, the photo second, and the gradient renders on top of the photo. You can also layer two photos, a pattern over a photo, or any combination. Each image can have its own repeat, position, and size values, which gives you a great deal of flexibility without needing extra HTML elements.
Most mobile browsers disable the fixed attachment behavior for performance reasons. Rendering a fixed background while scrolling requires the browser to repaint the background on every scroll frame, which is expensive on mobile hardware. iOS Safari in particular ignores background-attachment: fixed entirely and treats it as scroll. If the parallax effect matters for your design, you will need a JavaScript-based solution for mobile, or use a media query to fall back to background-attachment: scroll on smaller screens.
There are a few reliable techniques. The gradient overlay is the most common: layer a semi-transparent dark (or light) gradient over the image using linear-gradient(rgba(0,0,0,0.5), rgba(0,0,0,0.5)) as the first background value. You can also add a semi-transparent background color to the text container itself โ a dark box behind white text, for example. A third option is to use text-shadow to add a subtle dark outline around the letters. The best choice depends on the photo: if the area behind the text is already dark or blurred, a simple text-shadow may be enough.
Yes, significantly. Background images are loaded by the browser just like any other resource, and a large uncompressed photo can easily be several megabytes โ which translates directly into slower load times, especially on mobile connections. The recommended approach is to resize the image to the largest size it will actually display (usually 1600โ2000px wide for a full-width background) and then compress it using a tool like Squoosh or TinyPNG. The WebP format offers the best combination of quality and small file size for modern browsers. A well-optimized background image should typically come in under 200โ300KB.
Not with the background-image property โ that only accepts image files and gradients. To use a video as a background, the standard approach is to place an HTML <video> element inside the section and position it with CSS so it fills the container and sits behind the other content. You set it to autoplay, mute, and loop so it behaves like a background. This is covered in more advanced HTML and CSS, but it is good to know the distinction: background images are a CSS property, while background videos require an HTML element.
It depends on the image. JPEG is the right choice for photographs with lots of colors โ it compresses well and produces small file sizes. PNG is better when you need transparency, such as a logo or an illustration with a transparent background. WebP offers better compression than both JPEG and PNG at equivalent quality and is supported by all modern browsers, making it a good default for new projects. For full-width photo backgrounds where file size matters most, JPEG or WebP are the practical choices.
If the image cannot be found or takes too long to download, the browser shows whatever is behind the element โ usually plain white. This can make text unreadable if your design assumes a dark photo behind white text. The fix is to always set a background-color on the same element as your background-image. Choose a color that keeps your text readable on its own โ something dark works well behind white text. The browser displays the color immediately and swaps in the image once it loads.
No โ CSS background images are completely invisible to screen readers and other assistive technology. This is by design: they are treated as pure decoration, not content. That is exactly why background images should only be used for decorative purposes. If an image communicates something meaningful โ a product photo, a diagram, a portrait โ it belongs in the HTML as an <img> tag with descriptive alt text. Also keep in mind that any text placed over a background image needs sufficient color contrast to remain readable for visitors with low vision.
โ Key Takeaways
CSS background images are placed with CSS rather than HTML, which gives you more control over how they are positioned and sized. This lesson covered background-image, background-size, and background-position. Next, you will learn how to prepare images for the web, including how to resize and compress them. Optimized images load faster and improve the whole experience for visitors.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
