我正在尝试为我正在处理的页面设置一个简单的水平选项卡结构,但我在浮动 div 与 z-index 的组合方面遇到了一些麻烦。

在浏览器中查看以下代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<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>

为什么左侧 div 的橙色边框不与右侧 div 的绿色边框重叠?

有帮助吗?

解决方案

Z-index属性不适用于静态定位的元件。为了使用z索引的CSS还必须包括不是静止的其它任何位置值(即相对的,绝对的,固定的)。

.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; }

会给你你想要的东西,我认为。我加入的位置是:相对于;并改变了。左的z索引为3(2)和改变.right的z索引为2(3)。

其他提示

z-index对元件没有效果,即不位于(例如position:absolute;

上使用position属性元素。添加到position:relative .left

消极的 margin-left?

.right { float: left; width: 396px; background-color: #09c; border: 2px solid green; z-index: 3; margin-left: -5px;}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top