html - 如何将一个 div 放在另一个 div 之上?

标签 html css

我基本上希望始终在最前面放置一个居中的图像(“drob”div),如果它们重叠,则其他 div 位于其后面。

代码如下:

body {
background-image:url('background.jpg');
}

#container {
width: 1040px;
height: 768px;
margin: 0 0 0 75px;
}


#navbar {
float: right;
height: auto;
width: 150px;
margin: 0 0 0 10px;
color: #FF0;
list-style: none;

}


#note {
width: auto;
height: auto;
float: left;
margin: 20px 0 0 -110px;

}


#drob {
height: auto;
width: auto;
float: left;
margin: 50px 0 0 -20px;

}


#gallery {
float: left;
width: 675px;
height: auto;
margin: -125px 0 0 -20px;

}


#gallery img {
float: left;
margin: 0 0 0 5px;  

}


#video {
height: auto;
width: auto;
float: left;
margin: 150px 0 0 -20px;

}


<div id="container">
<div id="navbar">
    <ul>
        <li>Home</li>
        <li>About Denard</li>
        <li>Stats</li>
        <li>Media</li>
    </ul>
</div>
<div id="video">
<iframe width="450" height="265" src="http://www.youtube.com/embed/ZmmCAI8U_Vc" 
frameborder="0" allowfullscreen></iframe>
</div>
<div id="drob">
<img src="images/drob.png" height="560" width="404" alt="Denard Robinson" />
</div>
<div id="note">
    <a href="#"><img src="images/note.png" height="464" width="331" alt="note" /></a>
</div>
<div id="gallery">
    <img src="denard1.jpg" height="145" width="215" alt="denard1" />
    <img src="denard2.jpg" height="145" width="215" alt="denard2" />
    <img src="denard3.jpg" height="145" width="215" alt="denard3" />
</div>

</div>    


</body>

最佳答案

您需要设置重叠 div 的 z-index。

具有较低 z-index 的 div 将位于具有较高索引的 div 之后。

您可以在 http://reference.sitepoint.com/css/z-index 找到更多关于 z-index 的信息

关于html - 如何将一个 div 放在另一个 div 之上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7424353/

相关文章:

html - 挤压浏览器窗口时,Box-shadow 在右边被切掉

Javascript 滚动 Div 循环

javascript - 如何禁用特定类的日期选择器箭头

CSS 径向渐变

带有更多内容的 HTML5 缓存 list 出现奇怪的错误

jquery - 禁止在 div 中复制?

css - overflow hidden 的 Div,无法处理聊天邀请图像脚本

html - 为什么在调整浏览器窗口大小时 h1 元素中的内容一直重叠?

html - 包含 div 的 CSS 内容溢出

html - 为什么显示内联不起作用?