html - 为什么 CSS 清除属性不清除 float ?

标签 html css css-float

我有一些非常简单的 HTML 和 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;
}

为什么类为 clear 的元素没有显示在 float 图像下方?

最佳答案

clear 仅适用于 block 级元素。

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>

关于html - 为什么 CSS 清除属性不清除 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51458568/

相关文章:

html - 如何水平旋转表格标题并保持自动列宽?

html - 使用 css 重新定位 Html 页面的 div

javascript - 在弹出窗口而不是 DIV 中打开一个新页面

CSS Float 位置问题

javascript - 使用Javascript获取标签值的内容

css - 中心加载动画

html - 具有多个跨度的 div 上的文本溢出省略号

javascript - 使 hover resize 元素只影响元素

html - 调整页面大小时需要页脚保持在内容下方

html - CSS 菜单 - 如何