绝对定位的 div 嵌套在另一个绝对定位的 div 中的问题
我不是 CSS 专家,尤其是在定位方面,我希望这个问题很容易解决。
这是我的简化 HTML:
<body>
<div style="height:100%;width:100%;">
<div id="map" style="bottom:250px;height:100%;overflow:hidden;position:absolute;width:100%;">
<!-- The controls div does not render correctly. -->
<div id="controls" style="left:10px;position:absolute;top:10px;">
</div>
<!-- The legend and logos divs do render correctly. -->
<div id="legend" style="bottom:45px;left:10px;position:absolute;">
</div>
<div id="logos" style="bottom:5px;left:10px;position:absolute;">
</div>
</div>
<div id="search" style="bottom:0;height:250px;position:absolute;width:100%;">
</div>
</div>
</body>
“controls”div 未正确呈现。事实上,它根本不显示。如果我取出“top:10px;”样式,并将其替换为“bottom:400px;”,它确实渲染正确。但这不是我想要的,因为如果用户调整浏览器窗口的大小,“地图”div 的高度就会调整。我也不想使用 JavaScript 来定位 div。
“legend”和“logos”div 都正确渲染。
我正在 Firefox 中进行测试。
I'm no CSS expert, especially when it comes positioning, and I'm hopeful that this one will be easy to solve.
Here is my simplified HTML:
<body>
<div style="height:100%;width:100%;">
<div id="map" style="bottom:250px;height:100%;overflow:hidden;position:absolute;width:100%;">
<!-- The controls div does not render correctly. -->
<div id="controls" style="left:10px;position:absolute;top:10px;">
</div>
<!-- The legend and logos divs do render correctly. -->
<div id="legend" style="bottom:45px;left:10px;position:absolute;">
</div>
<div id="logos" style="bottom:5px;left:10px;position:absolute;">
</div>
</div>
<div id="search" style="bottom:0;height:250px;position:absolute;width:100%;">
</div>
</div>
</body>
The "controls" div is not rendering correctly. In fact, it doesn't display at all. If I take out the "top:10px;" style, and replace it with "bottom:400px;", it does render correctly. This is not what I want, though, as the "map" div's height gets adjusted if the user resizes the browser window. I also do not want to use JavaScript to position the div.
The "legend" and "logos" divs are both rendering correctly.
I'm doing my testing in Firefox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将parent div 的位置设置为
relative
和childrenabsolute
。这个 CSS 技巧的解释如下:You should simply give the parent div a position set to
relative
and childrenabsolute
. This CSS trick is explained here:这就是我最终得到的结果。谢谢 Sarfraz 和 sholsinger,引导我走向正确的方向:
所以我基本上只是将“map”div 嵌套在绝对定位的 div 中,并将其设置为“position;relative;”。
我第一次发布的示例中有两个问题:
上述修复解决了这两个问题。
Here's what I ended up with. Thanks, Sarfraz and sholsinger, for leading me in the right direction:
So I basically just nested the "map" div in an absolutely positioned div and set it to "position;relative;".
There were two problems in the example I first posted:
The fixes above solve both of these problems.