height:auto;
子元素使用float:left;
或时如何获取父元素float:right
?
父母
#parent {
width:100px;
height:auto;
padding-bottom:10px;
background:#0F0;
}
child_left
#child_left {
width:50%;
height:50px;
float:left;
background:#00F;
}
元素的默认高度为auto
,但似乎您正在寻找一个clearfix。如果元素的子元素是浮动的,则它们实际上会从文档流中移出,因此,如果父元素没有定义的尺寸,则子元素将自身折叠。您可以添加overflow:auto
到父元素:
#parent {
width:100px;
overflow:auto;
padding-bottom:10px;
background:#0F0;
}
另外,您也可以使用clearfix:
#parent:after {
clear:both;
display:table;
content:'';
}
您也可以有一个clearfix类,这两种方法都可以。如果要避免使用这些选项,则只需首先在父元素上设置尺寸。显然,这并不总是最好的选择,因为某些元素的子元素尺寸会有所不同。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句