With CSS, you can control the background of elements. You can set a background-color
to fill it with a solid color, a background-image
to fill it with (you guessed it) an image, or even both:
body {
background-color: red;
background-image: url(pattern.png);
}
Here’s an example where I’m using an SVG image file as the background, embedded right in the CSS as a data URL.
See the Pen background color and image together by Chris Coyier (@chriscoyier) on CodePen.
That’s just a single image there, repeated, but we can actually set multiple background images if we want. We do that by separating the values with commas.
body {
background-image:
url(image-one.jpg),
url(image-two.jpg);
}
If we leave it like that, image-one.jpg
will repeat and entirely cover image-two.jpg
. But we can control them individually as well, with other background properties.
body {
background-image:
url(image-one.jpg),
url(image-two.jpg);
background-position:
top right, /* this positions the first image */
bottom left; /* this positions the second image */
background-repeat:
no-repeat; /* this applies to both images */
}
See how background-position
also has comma-separated values? Those will apply individually to each image respectively. And then how background-repeat
has only one value? We could have done two values in the same way, but by using just one value, it applies to both.
Here’s an example using four separate images, one in each corner, offset by a smidge:
See the Pen Example of multiple backgrounds by Chris Coyier (@chriscoyier) on CodePen.
It’s too bad you can’t rotate or flip background images or else we could have used just one. We can rotate and flip entire elements (or pseudo elements) though, so in cases like that, we can get away with using a single image!
See the Pen Flipping Image So You Can Use Just One by Chris Coyier (@chriscoyier) on CodePen.
Just a few other things to be aware of here:
- The stacking order of multiple background is “first is on top.”
- Gradients are applied through
background-image
, so they can be used as part of all this. For example, you could set a transparent gradient over a raster image.
See the Pen Tinted Image w/ Multiple Backgrounds by Chris Coyier (@chriscoyier) on CodePen.
Awesome stuff! I subscribed!
Is there a way to specify fallback images if the format isn’t supported? I would like to use WebP images in CSS, with a JPG fallback.
https://stackoverflow.com/questions/37588017/fallback-background-image-if-default-doesnt-exist
Probably gonna need a spot of JS: https://css-tricks.com/using-webp-images/#article-header-id-4 or, use an image CDN like Cloudinary in which you can set up to make sure it serves to best format it can with a single url.
Hi Dusan Milko. The StackOverflow question you linked to doesn’t answer my question. I want a fallback if the image format of the first image is not supported. The StackOverflow question talks about a fallback if the first image doesn’t exist.
Thanks Chris Coyier for answering my question.
Thank you Chris! I never thought this would be possible and so simple. And thanks for your work and tutorials, i really appreciate what you doing. I’ve learn a lot from your tips and tricks.
Hi, is there going to be a “collection” of these CSS basics? I feel confident in my knowledge of the basics, but it’s always nice to revisit every now and then.
That’s a great idea! Might make sense for us to at least tag them in a way that groups them together.
Nice idea to give a gradient to the background image. Thanks for sharing.
hello there, how about multiple background and auto change in interval second. any trick for that?
I would probably consider a slider for something like that. The right approach might depend on knowing more context, but CodePen has a ton of examples to work from: https://codepen.io/search/pens?q=image+slider&limit=all&type=type-pens
div#bg {background-image:
url(image-one.jpg),
url(image-two.jpg);
background-attachment: fixed, fixed;
background-position: center, center;
background-repeat: no-repeat, no-repeat;
background-size: cover, cover;
min-height: 100%;}
that i ask is how to change beetween imgae one to image two in some interval with css3
Can it be done with inline styling?