html - Why is CSS clear property not clearing floats?
问题描述
I have some very simple HTML and CSS: https://jsfiddle.net/79gdnyt1/8/
HTML
<div>
<img src="http://placehold.it/120x110">
<span>This text will wrap around the image.</span>
<span class="clear">This text isn't allowed to wrap around floated elements, and therefore will render underneath them.</span>
</div>
CSS
img {
float: right;
}
.clear {
clear: right;
}
Why is the element with the class clear
not displaying underneath the floated image?
解决方案
clear
only applies to block-level elements.
https://developer.mozilla.org/en-US/docs/Web/CSS/clear
Initial value none
Applies to block-level elements
Inherited no
Media visual
div {
background: #888;
padding: 15px;
}
img {
float: right;
margin: 0px 20px 20px 0px;
}
.clear {
display: block;
clear: right;
}
<div>
<img src="http://placehold.it/120x110">
<span>This text will wrap around the image.</span>
<span class="clear">This text isn't allowed to wrap around floated elements, and therefore will render underneath them.</span>
</div>
推荐阅读
- css - 使用 Java 以编程方式在 Vaadin 14 中通过 CSS 指定背景图像
- c++ - 从类对象调用函数时出现致命错误
- javascript - FB Instant Game 资产加载;停留在 0%
- python - 用 df2 中一列中的值减去 df1 中所有列的值
- c# - C# 中不区分大小写的文本搜索:突出显示匹配短语时如何保持原始大小写?
- java - System.out.format("height is %3.1f percent greater than length\n", percent); 是什么意思?用Java做?
- javafx - TableView updateItem 上的 setRowFactory - 样式 -fx-font-weight:粗体不起作用
- javascript - 如何使用硬件键盘键触发 html 按钮及其 onclick 功能?
- javascript - 谷歌优化防闪烁片段超时
- jquery - JQuery在第一次点击时不触发,但在第二次点击时触发