我有一个在div里设置了float:right
的元素,但是它让最外层的div无法环绕。这是它的jsfiddle。
我试图让提交按钮在div内向右浮动,但是设置这个属性似乎导致了问题。如何让父元素包裹浮动元素?
overflow: hidden;
,这将使它包含其中的任何浮动元素。<div class="element">
<label></label>
<input type="submit" id="submit" value="Post Assessment">
<div style="clear:both"></div>
</div>
<br style="clear: both;" />
。Note: If an element contains only floated elements, its height collapses to nothing. If you want it to always be able to resize, so that it contains floating elements inside it, set the value of the element's display property to flow-root.
#container { display: flow-root; }
https://developer.mozilla.org/en-US/docs/Web/CSS/clear
在overflow
方法中,他们说了以下内容:
使用
overflow
来创建一个新的BFC的问题在于,overflow
属性是用来告诉浏览器如何处理溢出内容的。有些情况下,当你仅仅使用这个属性来创建一个BFC时,可能会出现不需要的滚动条或被裁剪的阴影。此外,对于未来的开发人员来说,这样做可能不太容易理解,因为可能不明显为什么要使用overflow
来达到这个目的。如果你这样做了,最好在代码中加上注释来解释。
https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_flow_layout/Introduction_to_formatting_contexts
所以我更喜欢使用diplay: flow-root
方法。使用
text-align:right
使用“float: right”代替
<br style="clear:both;" />
,如果要跨浏览器使用,必须使用 div。 - Sam Dufel