简体   繁体   English

当子div被浮动时,父div失去自动高度

[英]When child divs are floated, parent divs lose auto-height

I have a parent div called main. 我有一个名为main的父div。 It has a bunch of child divs in it, which I floated left or right. 它里面有一堆子div,我向左或向右浮动。

When I finished floating the last one, the height of the main div completely vanished, throwing off the layout of my page. 当我完成浮动最后一个时,主div的高度完全消失,抛弃了我的页面布局。 I gave it a height:100%; 我给它一个高度: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? 如何将其保持在液体高度,只需要包含元素所需的量即可?

Contain your floats. 包含你的花车。

On the div called main add overflow:hidden; 在div上调用main add overflow:hidden; . That should contain it. 那应该包含它。

Are you using relative positioning for your floats? 您是否使用相对定位的浮子?

You'll need to put a fake div in to "hold open" the parent div otherwise. 你需要把一个假div放到“保持打开”的父div。 As soon as everything is floated you will lose all basis for height in the parent div. 一旦所有东西浮动,你将失去父div中所有高度的基础。 (Nothing is in there.) (那里什么都没有。)

inserting "overflow:hidden;" 插入“溢出:隐藏;” into parent div worked for me. 进入父母div为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM