当子div浮动时,父div失去自动高度
我有一个名为 main 的父 div。它里面有一堆子div,我向左或向右浮动。
当我完成最后一个浮动时,主 div 的高度完全消失,破坏了页面的布局。我给它的高度:100%;就目前而言,但对于大多数分辨率来说它会过高。如何将其保持在流体高度,仅达到容纳元素所需的高度?
I have a parent div called main. It has a bunch of child divs in it, which I floated left or right.
When I finished floating the last one, the height of the main div completely vanished, throwing off the layout of my page. I gave it a height:100%; just for now, but it will be overly tall on most resolutions. How can I keep it at a fluid height, only as much as needed to contain the elements?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控制住你的漂浮物。
在名为 main 的 div 上添加
overflow:hidden;
。那应该包含它。Contain your floats.
On the div called main add
overflow:hidden;
. That should contain it.您是否对浮动使用相对定位?
否则,您需要放入一个假 div 来“保持打开”父 div 。一旦所有内容都浮动,您将失去父 div 中高度的所有基础。 (里面什么也没有。)
Are you using relative positioning for your floats?
You'll need to put a fake div in to "hold open" the parent div otherwise. As soon as everything is floated you will lose all basis for height in the parent div. (Nothing is in there.)
插入“溢出:隐藏;”进入父 div 为我工作。
inserting "overflow:hidden;" into parent div worked for me.