everything about web

A Comprehensive Guide to the World of Web Design & Development:

CSS, HTML, Performance, Web Design, and all Aspects of Front-End Development

Advertisements

Examples: Combining Multiple Background Images in CSS

The use of background images is an essential aspect of web design, and it's a powerful tool for adding visual interest and personality to a website. However, it's not always necessary to use just one background image. By combining multiple background images, you can create unique and engaging designs that set your website apart from the rest.

The use of background images is an essential aspect of web design, and it’s a powerful tool for adding visual interest and personality to a website. However, it’s not always necessary to use just one background image. By combining multiple background images, you can create unique and engaging designs that set your website apart from the rest.

In this article, we’ll be exploring the various techniques for combining multiple background images in CSS.

Syntax

To display a combination of multiple images inside a div, let’s start with adding in multiple image sources from the CSS background-image, then separating them by commas:

background-image: url(bg1.png), url(bg2.png),…;

The first (leftmost) image will have the highest stack order (highest z-index). In this example, bg1.png will display on top of bg2.png.

.banner{
  background-image: url(bg1.png), url(bg2.png),...;
  background-position: right bottom, left top, ...;
  background-repeat: no-repeat, repeat, ...;
  background-size: contain, 50px,...;
  background-origin: padding-box, content-box,...;
  background-clip: padding-box, content-box,...;
}

Example

The following example has 3 individual images: tree.png, sky.png, and background.png, let’s combine all of them into a single background:

How to create multiple background image in CSS
tree.png

+

How to create multiple background image in CSS
sky.png

+

How to create multiple background image in CSS
background.png

HTML & CSS:

<style>
 .banner{
    width: 652px;
    max-width: 100%;
    height: 153px;
    border: 5px solid black;

    background-image: url(cloud.png), url(tree.png), url(bg.png);
    background-position: right top, left top, center center;
    background-repeat: no-repeat, repeat, no-repeat;
    background-size: auto, contain, cover;
    background-origin: content-box, padding-box, border-box;
 }

</style>
<div class="banner">Hello!</div>

Result:

background shorthand 

As per usual, you can combine the declaration for multiple background properties with a background shorthand, for example:

Note: The background-color can only be applied to the last background.

HTML & CSS:

<style>
 .banner{
   width: 652px;
   max-width: 100%;
   height: 153px;
   border: 5px solid black;
   background: url(cloud.png) right top/auto no-repeat  padding-box,
               url(tree.png) left top/contain repeat  content-box,
               linear-gradient(0deg, rgba(34,193,195,1) , rgba(253,187,45,1)) center/cover no-repeat border-box;
 }

</style>
<div class="banner">Hello!</div>

Result:

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

If you found this article helpful and would like to show your support, you can buy me a coffee using the link below. Thanks a bunch!

Choose an amount

$5.00
$10.00
$15.00

Or enter a custom amount

$

Thank you for your contribution!

Donate

Topics:

CSS (27) css pseudo (3) debug (2) design (4) fonts (2) html (6) image (8) layout (5) optimization (10) performance (9) responsive (8) Software-development (1)


Advertisements

Recent Post:

Advertisements

Related Posts

Advertisements
Advertisements
%d bloggers like this: