如何使用float
CSS中的属性并带有清除
过去,浮动是一个非常重要的话题。
它被用于许多技巧和创意用途,因为它是表格和我们可以真正实现某些布局的少数几种方法之一。过去,我们曾经将边栏浮动到左侧,例如,将其显示在屏幕的左侧,并在主要内容上添加了一些边距。
幸运的是,时代已经发生了变化,如今,我们有了Flexbox和Grid来帮助我们进行布局,而float已回到其原始范围:将内容放置在容器元素的一侧,并使它的兄弟姐妹出现在容器元素的周围。
这float
属性支持3个值:
left
right
none
(默认)
假设我们有一个框,其中包含带有一些文本的段落,并且该段落还包含图像。
这是一些代码:
<div class="parent">
<div class="child">
<div class="box">
<p>This is some random paragraph and an image. <img src="https://via.placeholder.com/100x100" /> The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text.
</p>
</div>
</div>
</div>
.parent {
background-color: #af47ff;
padding: 30px;
width: 500px;
}
.child {
background-color: #ff4797;
padding: 30px;
}
.box {
background-color: #f3ff47;
padding: 30px;
border: 2px solid #333;
border-style: dotted;
font-family: courier;
text-align: justify;
font-size: 1rem;
}
和视觉外观:
如您所见,默认情况下,正常流程会考虑图像内联,并在行本身中为其留出空间。
如果我们添加float: left
到图像,以及一些填充:
img {
float: left;
padding: 20px 20px 0px 0px;
}
结果是:
这就是我们通过应用float来得到的:正确,相应地调整填充:
img {
float: right;
padding: 20px 0px 20px 20px;
}
从页面的正常流程中删除一个浮动元素,其他内容在页面周围流动。
您也不仅限于浮动图像。在这里,我们使用span
元素:
<div class="parent">
<div class="child">
<div class="box">
<p>This is some random paragraph and an image. <span>Some text to float</span> The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text. The image is in the middle of the text.
</p>
</div>
</div>
</div>
span {
float: right;
margin: 20px 0px 20px 20px;
padding: 10px;
border: 1px solid black
}
结果如下:
清算
当您浮动一个以上元素时会发生什么?
如果在浮动时找到另一个浮动图像,则默认情况下它们会水平堆叠在一起。直到没有空间,它们都将开始堆叠在新的行上。
假设我们在一个p
标签:
如果我们添加float: left
这些图像:
img {
float: left;
padding: 20px 20px 0px 0px;
}
这就是我们将要拥有的:
如果您添加clear: left
对于图像,这些图像将垂直而不是水平堆叠:
我用了left
的价值clear
。它允许
left
清除左浮球right
清除右浮动both
清除左右浮动none
(默认)禁用清除
免费下载我的CSS手册
更多CSS教程:
- Flexbox指南
- CSS网格教程
- CSS变量(自定义属性)
- PostCSS简介
- CSS保证金属性
- 如何使用CSS将元素居中
- CSS系统字体
- 如何使用样式打印HTML
- CSS过渡入门指南
- CSS动画教程
- CSS简介
- CSS指南
- 如何使用PurgeCSS和PostCSS设置Tailwind
- 尾风备忘单
- 如何使用CSS连续旋转图像
- 使用CSS使表响应
- 如何通过二等分来调试CSS
- CSS选择器
- CSS级联
- CSS特异性
- CSS属性选择器
- CSS颜色
- CSS单位
- CSS url()
- CSS排版
- CSS Box模型
- CSS位置属性
- CSS媒体查询和响应式设计
- CSS功能查询
- CSS转换
- 如何使用CSS设置列表样式
- CSS供应商前缀
- CSS继承
- CSS伪类
- CSS伪元素
- 用CSS样式化HTML表
- CSS Display属性
- CSS calc()函数
- CSS边框
- 使用@import导入CSS文件
- CSS错误处理
- CSS过滤器
- CSS Box大小
- CSS背景
- CSS注释
- CSS字体
- CSS填充
- CSS float属性和清除
- CSS规范化
- CSS z-index属性
- 如何使用CSS禁用文本选择
- 如何使用CSS将项目放在其容器的底部
- 如何使用CSS反转颜色
- CSS中的响应式前置标签
- 自适应YouTube视频嵌入
- 响应式设计的良好CSS断点值是多少?
- 如何在Flexbox中对齐中心