tags:

views:

2115

answers:

4

I'm trying to set up a simple horizontal tab structure for a page I'm working on, and I'm running into some trouble with floating div's combined with z-index that I'm hoping someone can help me with.

Viewing the following code in a browser:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
        #main { width: 500px; z-index: 1;}

        .left { float: left; width: 96px; background-color: red; border: 2px solid orange; z-index: 2; margin-right: -2px }
        .right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 3; }

        .clear { clear: both; }
</style>
</head>

<body>
    <div id="main">
        <div class="left">
            LEFT
        </div>
        <div class="right">
            RIGHT
            <br />
            RIGHT
        </div>
        <div class="clear"></div>
    </div>
</body>
</html>

Why doesn't the left div's orange border overlap the right div's green border?

+4  A: 

z-index property will not apply to statically positioned elements. In order to use z-index the CSS must also include any position value other than static (ie relative, absolute, fixed).

.left { float: left; width: 96px; background-color: red; border: 2px solid orange; z-index: 3; margin-right: -2px; position: relative; }
.right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 2; position: relative; }

Will give you what you want I think. I added position: relative; and changed the z-index of the .left to 3 (from 2) and changed the z-index of .right to 2 (from 3).

Josh
Thanks for the CSS lesson, I will remember that rule about z-index from now on.Cheers
Michael
A: 

It does...what browser are you trying it on?

I just tried it on firefox and chrome and opera and IE 8 and it works on all of them.

Mussnoon
I read it wrong...sorry.
Mussnoon
+1  A: 

z-index has no effect on elements that are not positioned (eg position:absolute;)

see: http://www.w3schools.com/Css/pr_pos_z-index.asp

edit: Josh beat me to it

Brian B
A: 

negative margin-left?

.right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 3; margin-left: -5px;}
BenB