DIV重叠 如何优先显示(div浮在重叠的div上面)

如果有2个div有重叠,默认是根据html解析顺序,最后加载的优先级最高(浮在最上面)。

 

问题:

如果想把前面加载的div显示在最上面?关键字:z-index

 

举例:

--原来的页面:first div是被second div盖住了:

<html>
<head>
<title>Test Div Overlap</title>
<style>
.first{
width: 366px;height: 384px;top:100px;left:100px;
position: relative;/*absolute*/
background-color:#CCCCCC;
text-align:right;
/*
z-index:2;
*/
}

.second{
width: 366px;height: 384px;top:0px;
position: absolute;
background-color:#999999;
}
</style>
</head>
<body>
<div id="first" class="first"><a href="#" title="Overlap by Second">Overlap by Second</a>&nbsp;&nbsp;first</div>
<div id="second" class="second">second&nbsp;</div>
</body>
</html>

解决方案:

1.需要将first的position设置为relative

2.将.first{}里面的z-index:2的注释取消

结果:

发现first div浮在second的上面了。

 

解释:

默认z-index是0,如果设置了z-index的值高于其他,就可以优先显示。

posted on 2014-03-20 16:33  tomcat and jerry  阅读(37229)  评论(0编辑  收藏  举报

导航