【发布时间】:2016-05-09 15:39:32
【问题描述】:
我正在尝试将菜单覆盖在图像上,但图像的容器是可变的。我不知道图像会在哪里或有多大。如何在不知道在哪里设置顶部、左侧坐标的情况下将绝对/固定的东西定位在图像上?
body {
box-sizing: border-box;
border: 0;
padding: 0;
}
.main-container {
height: 500px;
width: 100%;
background-color: #282828;
padding: 0;
}
.moving-container {
display: inline-block;
margin-top: 100px;
background-color: red;
width: 100%;
height: 100px;
}
.content {
z-index: 1;
width: 100%;
height: 50%;
background-color: white;
}
.overlay {
z-index: 2;
width: 300px;
height: 150px;
background-color: blue;
}
<body>
<div class="main-container">
<div class="moving-container">
<div class="content">
</div>
<div class="overlay">
</div>
</div>
</div>
</body>
这是example that doesn't work。您可以看到作为覆盖对象的蓝色边框框是如何被推到白色全跨度 div 下方的。
如果我使用绝对位置或固定位置,我会得到this。
我尝试将图像设置为元素的背景,然后元素的内部将是菜单,但不知道元素有多大,例如。固定的 px 值,则不会出现背景图片(比菜单本身大)。
那我该怎么办?我遇到过翻译,但我之前没有实现过。
在上面的示例中,我有一个固定的高度尺寸可以使用,其中宽度仅为 100%。
感谢您的帮助。
【问题讨论】: