[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How can I apply two background images in CSS

Posted on 2016-11-02
4
Medium Priority
?
69 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:James Atkin
[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 59

Accepted Solution

by:
Julian Hansen earned 1000 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 1000 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:James Atkin
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
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 …
Suggested Courses

650 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