我有一个非常简单的 HTML/CSS 片段:
<div style="float: left; width: 180px; clear: both;">Foo</div>
<div style="float: left; width: 180px;">Bar</div>
两个 div 都向左浮动,但 Foo div 有 clear: both。这应该可以防止 Bar div 漂浮在它旁边……但事实并非如此。如果将该 HTML 放入浏览器,您会看到两个 div 是相邻的。
我对 clear 有什么误解?
最佳答案
你不想在 Foo 之后运行 clear,你想在 Bar 开始之前运行它
<div style="float: left; width: 180px;">Foo</div>
<div style="clear: both; float: left; width: 180px;">Bar</div>
clear CSS 属性指定一个元素是可以紧挨着它前面的 float 元素,还是必须向下移动(清除)在它们下面。 clear 属性适用于 float 和非 float 元素
关于css - 为什么我的清晰样式不能防止同级元素 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51509283/