Solved

How can I apply two background images in CSS

Posted on 2016-11-02
4
30 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
Comment Utility
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 51

Accepted Solution

by:
Julian Hansen earned 250 total points
Comment Utility
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 21

Assisted Solution

by:Kim Walker
Kim Walker earned 250 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Firefox Display problems 5 41
css question 1 31
(URGENT) Help HTML and CSS Resizing Images 2 20
HTML CSS 7 15
When applying CSS to your HTML, there are many different ways to select which element(s) the CSS applies to.  Some of these selectors are more commonly known and used than others - Here are the more common ones: #X - Matches an ID of X .X - Matche…
Why do we like using grid based layouts in website design? Let's look at the live examples of websites and compare them to grid based WordPress themes.
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now