Using two background images for one div in IE

This post is an extension of my previous post on how to use two background images for one div. However, the CSS trick we covered will not work correctly in Internet Explorer (no surprise there, right? :) ). Here is one way that I have used to work around that.

Step 1

First things first, if you are not using a separate stylesheet for IE, do so now. Create the new stylesheet, save it as iestyle.css (or whatever you want it to be called), and then use this line of code in the <head> section of your web page to reference the stylesheet:

<!--[if IE]>         <link rel="stylesheet" type="text/css" href="iestyle.css" /> <![endif]-->

(For more information on conditional stylesheets, you can read this article – http://css-tricks.com/how-to-create-an-ie-only-stylesheet).

Step 2

Now, let’s say these are the CSS codes for the div that we are using two background images for:

.node {
color: #789F4D;
width:220px;
border: 1px solid grey;
padding:10px;
background: url(images/topbg.jpg) no-repeat left top, url(images/background.jpg) no-repeat right bottom;
background-color:#f6f6f2;
}
What we are going to do is create a new div which will display inside the initial div if the site is being viewed in IE.
Open your iestyle.css file. Add the CSS codes for the new div:
.node-second {
color: #789F4D;
width:220px;
border: 1px solid grey;
background: url(images/topbg.jpg) no-repeat right bottom;
 }
You will also have to add and then rewrite the styles for the original div in the IE stylesheet:
.node {
 color: #789F4D;
 width:220px;
 background: url(images/background.jpg) no-repeat left top;
 background-color:#f6f6f2;
 }

Notice the biggest difference here is that, in the IE stylesheet, the original div only has one background and the second div contains the second background.  The idea is that, if the website is being viewed in Internet Explorer, the two images will be in their own divs (the only way to make them both display, that I know of), but you want the two divs to be styled in such a way that the affect will be exactly the same as when the site is viewed in Mozilla or another browser.

Step 3

Now, simply set up the new div inside the initial div in your HTML.
<div class="node">
<div class="node-second">
Content here. Content here.
</div>
</div>
What this does is adds the second div inside the original div in the HTML page. The styles for both divs are determined by which browser the website is being viewed in:
  • If the browser is Mozilla or Chrome, etc., the div uses the CSS trick we covered in the previous post and displays two background images.
  • If the browser is Internet Explorer, the div uses the styles in our iestyle.css file and still displays two background images (one of which is contained in the new div).
I hope this works for you. As always, just let me know if you have any questions!

Related Posts:

6 Responses to “Using two background images for one div in IE”

  1. Jacques says:

    Hi Libby,

    Thanks a lot! Works great!

    I want to use this: http://www.designfestival.com/cicada/break-it-down/?id=80 – works with 3 background images – but, of course, not in IE.

    Using your solution, I added this to my IE.css:

    (I realize I could have put 2 backgrounds together in 1 element, but I thought this would be clearer):

    html{
    font-size:100%;
    min-height:101%;
    background-color: #99161c;
    background: url(../../pictures/test/tuscany_base.jpg);
    }

    div-IE-1{
    background: url(../../pictures/test/tuscany_mid.png)
    }

    div-IE-2{
    background: url(../../pictures/test/tuscany_high.png)
    }

    Now in my html, I just wrap those 2 empty divs around my main content:


    ...
    < body >

    < div id="div-IE-1" >
    < div id="div-IE-2" >
    ...
    < /body >

    Spent/wasted some time on looking for a solution, but yours is easiest.

    Thanks again!

  2. [...] Note: To make sure that your site is cross-browser compatible, make sure that you also use this IE fix to display two background images in one div in Internet Explorer. [...]

  3. abdul says:

    please canu give me a simple sample page….. im trying the way u said for more than two days now…. im running out of time also…. i still dont understand how u have done this…. please help me with a simple sample page im getting frustrated now…….

  4. patrick says:

    thats awesome…worked like a charm

Leave a Reply

Your email address will not be published. Required fields are marked *

*


*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">