background-image
Sets an image or gradient as an element’s background.
- Initial value
none- Inherited
- No
- Animatable
- no
- Applies to
- all elements
- JavaScript
element.style.backgroundImage = "linear-gradient(#000, #333)"
Example
#.hero {
background-image:
linear-gradient(to right, rgba(0, 0, 0, 0.8), transparent),
url("/img/hero.webp");
background-size: cover;
background-position: center;
}<section style="padding: 28px 20px; border-radius: 12px; color: #fff; background-image: linear-gradient(to right, rgba(0,0,0,.85), transparent), url('/collections/covers/jak-zostac-programista.webp'); background-size: cover; background-position: center;">
<h2 style="margin: 0">Become a programmer</h2>
<p style="margin: 6px 0 0; max-width: 26ch;">The text on the photo is readable thanks to the gradient.</p>
</section>Definition and usage
#The background-image property accepts an image via url() or a gradient: linear-gradient(), radial-gradient() or conic-gradient(). In CSS, gradients are images too, so they need no files.
You can provide several layers separated by commas. The first in the list sits on top. This lets you, for example, lay a semi-transparent gradient over a photo so the text on it stays readable.
A background image is decoration: it has no alternative text and screen readers skip it. Insert content that matters for understanding the page as an <img>.
Syntax
#background-image: none | url(…) | <gradient> [, …]*;Values
#- No background image.
url("/collections/covers/jak-zostac-programista.webp")
url()
A photo as the background.linear gradient
A smooth transition between colours along a line.radial gradient
Colours radiating from the centre.multiple layers
A darkening gradient on top and a photo underneath.
Values in practice
#none
.demo { background-image: none; }url("/collections/covers/jak-zostac-programista.webp")
.demo { background-image: url("/collections/covers/jak-zostac-programista.webp"); }linear-gradient(…)
.demo { background-image: linear-gradient(135deg, #7c3aed, #0ea5e9); }radial-gradient(…)
.demo { background-image: radial-gradient(circle at 30% 30%, #f59e0b, #7c3aed 70%); }gradient + photo
.demo { background-image: linear-gradient(to top, rgba(0,0,0,.85), transparent), url("/collections/covers/jak-zostac-programista.webp"); }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Always protect text on a photo with a semi-transparent gradient layer. Photos have both light and dark areas.
- For large background images use WebP or AVIF, they are much lighter than JPEG.
Related entries
#- background-sizeSets the size of the background image: covering everything, fitting entirely, or specific dimensions.
- background-positionSets the position of the background image within the element.
- background-repeatDecides whether and how the background image repeats to fill the element.
- backgroundA shorthand that sets all background properties in one declaration.
- linear-gradient()Creates a smooth colour transition along a straight line. It is used as an image, most often in background or background-image.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.