Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How can I apply two background images in CSS

Posted on 2016-11-02
4
49 Views
Last Modified: 2016-11-03
Hi all,

I am trying to do something in CSS that is proving to be more trouble than I thought!
I have an image displayed within a DIV using CSS.
What I want to do is display a particular image image, then overlay with a second one to display a status.

I was hoping that something like this would do it:
.myicon.Unit1 { background-image:url(../Img/Unit1.png); }
.myicon.Unit2 { background-image:url(../Img/Unit2.png); }

.myicon.IN { background-image:url(../ImgIN.png); }
.myicon.CR { background-image:url(../ImgCR.png); }
.myicon.MA { background-image:url(../ImgMA.png); }
.myicon.MI { background-image:url(../ImgMI.png); }
.myicon.WA { background-image:url(../ImgWA.png); }
.myicon.CA { background-image:url(../ImgCA.png); }
.myicon.UN { background-image:url(../ImgUN.png); }

Open in new window


Unfortunately as CSS styles essentially replace one another, this does not work and I need to do this:

.myicon.Unit1.OK { background-image:url(../Img/Unit1.png),url(../Img/OK.png); }
.myicon.Unit1.IN { background-image:url(../Img/Unit1.png),url(../Img/IN.png); }
.myicon.Unit1.CR { background-image:url(../Img/Unit1.png),url(../Img/CR.png); }
.myicon.Unit1.MA { background-image:url(../Img/Unit1.png),url(../Img/MA.png); }
.myicon.Unit1.MI { background-image:url(../Img/Unit1.png),url(../Img/MI.png); }
.myicon.Unit1.WA { background-image:url(../Img/Unit1.png),url(../Img/WA.png); }
.myicon.Unit1.CA { background-image:url(../Img/Unit1.png),url(../Img/CA.png); }
.myicon.Unit1.UN { background-image:url(../Img/Unit1.png),url(../Img/UN.png); }

.myicon.Unit2.OK { background-image:url(../Img/Unit2.png),url(../ImgOK.png); }
.myicon.Unit2.IN { background-image:url(../Img/Unit2.png),url(../ImgIN.png); }
.myicon.Unit2.CR { background-image:url(../Img/Unit2.png),url(../ImgCR.png); }
.myicon.Unit2.MA { background-image:url(../Img/Unit2.png),url(../ImgMA.png); }
.myicon.Unit2.MI { background-image:url(../Img/Unit2.png),url(../ImgMI.png); }
.myicon.Unit2.WA { background-image:url(../Img/Unit2.png),url(../ImgWA.png); }
.myicon.Unit2.CA { background-image:url(../Img/Unit2.png),url(../ImgCA.png); }
.myicon.Unit2.UN { background-image:url(../Img/Unit2.png),url(../ImgUN.png); }

Open in new window


Now, as I have potentially a LOT of different unit images, the CSS will become huge!

Is there any better, and more concise way to do this, or is this the only option?

Any help would be gratefully appreciated.

Many thanks,

James
0
Comment
Question by:jatkin
4 Comments
 
LVL 4

Expert Comment

by:Elizabeth Anderson
ID: 41869998
CSS3 helps you to add multiple background images for an element with the background-image property.

The following example has two background images:

#example1 {
    background-image: url(img_flwr.gif), url(paper.gif);
    background-position: right bottom, left top;
    background-repeat: no-repeat, repeat;
}

Open in new window

0
 
LVL 55

Accepted Solution

by:
Julian Hansen earned 250 total points
ID: 41870018
Here is a sample that describes how to overlay two images in the same space.

The mechanisms of these methods are discussed in this article
https://www.experts-exchange.com/articles/28823/3-Methods-for-creating-image-overlays-in-HTML-using-CSS-and-PHP-GD2.html
1
 
LVL 22

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
ID: 41871588
I don't know of a way to assign the common background images separately from the unique ones. I can see how tedious it might be to assign the common images to each individual element. The only alternative I can think of is to have separate overlaying divs and assigning them separate background images. But that may be just as tedious. You might be able to simplify it by using a ::before selector to insert the common background images. Something along these lines:
.myicon.Unit1::before, .myicon.Unit2::before {
	background-image:url(../Img/Unit1.png);content:'';width:100px;height:100px;position:absolute;left:0;top:0;
}
.myicon.Unit2 { background-image:url(../Img/Unit2.png); }

.myicon.IN, .myicon.CR, .myicon.MA, .myicon.MI, .myicon.WA, .myicon.CA, .myicon.UN {
	background-image:url(../ImgIN.png);position:relative;
}
.myicon.CR { background-image:url(../ImgCR.png); }
.myicon.MA { background-image:url(../ImgMA.png); }
.myicon.MI { background-image:url(../ImgMI.png); }
.myicon.WA { background-image:url(../ImgWA.png); }
.myicon.CA { background-image:url(../ImgCA.png); }
.myicon.UN { background-image:url(../ImgUN.png); }

Open in new window

I did not have the opportunity to test this.
0
 
LVL 4

Author Closing Comment

by:jatkin
ID: 41871804
Hi all,
Nesting the DIVs works great for my case:
[code]<div id=theID>
   <div class='myicon WA'></div>
   <div class='myicon Unit1'></div>
</div>[/code]
Thanks a lot for all the feedback.
James
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Make Line Items Look Good Over Mobile 2 80
Place text over image using CSS 6 48
How to show span when clicked on? 10 31
Customizing jQuery UI Accordion CSS 3 28
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question