background-image
Adds an image, gradient or multiple backgrounds through CSS.
Learn Background Images with practical HTML examples for navigation, images, alt text, media behavior, metadata, accessibility, SEO and live practice.
HTML links and media
A background image is set with CSS, not with the img element. That means it is usually decorative or part of visual presentation.
If the image communicates important content, use an img element with alt text instead. If it only creates atmosphere, texture, branding or layout polish, a CSS background can be the right choice.
Good background images need careful contrast, sizing, positioning and fallback colors.
Adds an image, gradient or multiple backgrounds through CSS.
Common values are cover and contain.
Controls which part of the image stays visible.
A gradient or pseudo-layer that keeps text readable.
Syntax and structure
A CSS background does not have alt text, so it should not be the only place important information exists.
<section class="hero">
<h1>Learn HTML visually.</h1>
</section>
<style>
.hero {
background:
linear-gradient(rgba(7, 17, 31, .76), rgba(7, 17, 31, .76)),
url("/assets/img/hero.webp") center / cover;
color: white;
}
</style>
<section class="hero"></section>
<style>
.hero {
background-image: url("sale-with-all-text-inside-image.jpg");
}
</style>
Rules that matter
Clickable and visual HTML affects how people move, scan, trust and share a page. The details are small, but the user experience impact is large.
Important content belongs in HTML, not only in a background image.
The section should still work while the image loads or fails.
Use overlays or positioning so text stays readable.
Cover crops images. Check important parts at multiple screen sizes.
Large backgrounds can damage performance quickly.
Use clear asset paths and optimized files.
Production thinking
This matters because visual polish should not hide information. A background image can make a page beautiful, but the page must still communicate without it.
Background images are not announced by screen readers. Any meaning in the image must also exist as real text or an img with alt.
Background images need real viewport testing. Cropping that looks perfect on desktop can destroy the subject on mobile.
Search engines do not get alt text from CSS backgrounds. Use real image markup when the image itself is content.
Live code lab
Edit the HTML or CSS, then use Run to refresh the preview. The preview is isolated, so links and forms stay inside this practice area.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read this page. Answer these questions out loud or write the answers in your own notes. If one answer feels vague, revisit the examples before moving on.