Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overlapping li elements with background and z-index

Tags:

html

css

z-index

I have a ul with tree list items in an horizontal view. All the list items have the same background image:

Background image is the same for all li

I want to overlap the background images so it looks like this:

This is what I try to accomplish

Here is my jsFiddle

CSS:

    div#menu ul li{
    height:30px;
    display: inline;
    list-style-type: none;
    width: 60px;
    background: url(http://i.stack.imgur.com/adwVj.jpg);
    background-size: 100%;
    background-repeat: no-repeat;
    padding-right:  5px;
    padding-left:30px;
    z-index:2;        
}

div#menu ul li:first-child{
    padding-left:20px;
    z-index:3;            
}
div#menu ul li:last-child{
    padding-left:35px;    
    margin-left:-30px   
    z-index:1;       
}

HTML:

<div id="menu">
       <ul>
           <li>Account</li>
           <li>Registreren</li>
           <li>Winkelwagen</li>
       </ul>
</div>

It goes wrong with the z-index!

like image 418
This_is_me Avatar asked Oct 01 '12 09:10

This_is_me


1 Answers

you should first give at least position: relative to your list-items, otherwise z-index has no effect. then just use

div#menu ul li + li {
   left : -20px;   
}

so the labels will remain close together (this rule will be applied starting from the second <li> element)

Example fiddle : http://jsfiddle.net/Faffz/3/

like image 122
Fabrizio Calderan Avatar answered Sep 27 '22 18:09

Fabrizio Calderan