Solved

How can I apply two background images in CSS

Posted on 2016-11-02
4
55 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
[X]
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
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 56

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSS: Making responsive table look nicer 7 37
tiny glitch in my main slider 3 47
Change box shadow 1 34
Navigation Menu Does Not Show on iPad 4 30
Creating a CSS block that only applies to printing By default, all of your CSS applies to every possible view of your page - whether on screen, printed, landscape, touch-screen, or whatever.  You can, however, add CSS that only applies under certai…
This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
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 add a full-size background image to a webpage using CSS3. Create a new HTML document with an internal stylesheet.: In CSS, define the html element to have a background image. Use a high resolution image.: In t…

762 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