- CSS 菜鸟教程
- CSS - 教程
- CSS - 简介
- CSS - 语法
- CSS - 选择器
- CSS - 包含
- CSS - 度量单位
- CSS - 颜色
- CSS - 背景
- CSS - 字体
- CSS - 文本
- CSS - 图像
- CSS - 链接
- CSS - 表格
- CSS - 边框
- CSS - border-block 属性
- CSS - 边框内联
- CSS - 边距
- CSS - 列表
- CSS - Padding 属性
- CSS - 光标
- CSS - 轮廓
- CSS - 维度
- CSS - 滚动条
- CSS - 内联块
- CSS - 下拉列表
- CSS - visibility 属性
- CSS - Overflow 属性
- CSS - 清除修复
- CSS - float(浮点)
- CSS - 箭头
- CSS - resize 属性
- CSS - quotes 属性
- CSS - order 属性
- CSS - Position 属性
- CSS - hypens 属性
- CSS - :hover(悬停)
- CSS - display(显示)
- CSS - focus 属性
- CSS - zoom(缩放)
- CSS - translate 属性
- CSS - Height 属性
- CSS - hyphenate-character 属性
- CSS - Width 属性
- CSS - opacity 属性
- CSS - z-index 属性
- CSS - bottom 属性
- CSS - 导航栏
- CSS - 覆盖
- CSS - 表单
- CSS - 对齐
- CSS - 图标
- CSS - 图片库
- CSS - 注释
- CSS - 加载器
- CSS - Atrribute 选择器属性
- CSS - 运算器
- CSS - root
- CSS - 盒子模型
- CSS - 计数器
- CSS - Clip (Obsolete) 属性
- CSS - writing-mode 属性
- CSS - Unicode-bidi 属性
- CSS - min-content 属性
- CSS - 全部
- CSS - inset 属性
- CSS - isolation 属性
- CSS - overscroll-behavior 属性
- CSS - justify-items 属性
- CSS - justify-self 属性
- CSS - tab-size 属性
- CSS - pointer-event 属性
- CSS - place-content 属性
- CSS - place-items 属性
- CSS - place-self 属性
- CSS - max-block-size 属性
- CSS - min-block-size 属性
- CSS - mix-blend-mode 属性
- CSS - max-inline-size 属性
- CSS - min-inline-size 属性
- CSS - offset 属性
- CSS - accent-color 属性
- CSS - user-select 属性
- CSS 高级
- CSS - grid 属性
- CSS - Grid 布局
- CSS - flexbox
- CSS - vertical-align 属性
- css - positioning
- css - layers
- css - pseudo_classes
- CSS - 伪元素
- CSS - @ 规则
- CSS 滤镜 - text-effect 属性
- CSS 分页媒体
- CSS 打印
- CSS - 布局
- CSS - 验证
- CSS - 图像精灵
- CSS - !important
- CSS - 数据类型
- CSS3 教程
- CSS - 圆角
- CSS - 边框图像
- CSS - 多种背景
- CSS - 渐变
- CSS - box-shadow 属性
- CSS - box-decoration-break 属性
- CSS - caret-color 属性
- CSS - text-shadow 属性
- CSS - 2D 转换
- CSS - 3D 变换
- CSS - transition 属性
- CSS - 动画
- CSS - 多列布局
- CSS - 盒子大小调整
- CSS - 工具提示
- CSS - buttons
- CSS - 分页
- CSS - 变量
- CSS - 媒体查询
- CSS - 值函数
- CSS - 数学函数
- CSS - Mask 属性
- CSS - shape-outside 属性
- CSS - 样式图像
- CSS - 特异性
- CSS - 自定义属性
- CSS 响应式
- CSS - 响应式网页设计 (RWD)
- CSS - 响应式设计视口
- CSS - 响应式网格视图
- CSS - 响应式媒体查询
- CSS - 响应式图像
- CSS - 响应式视频
- CSS - 响应式框架
- CSS 引用
- CSS - 所有属性列表
- CSS - 颜色引用
- CSS - 浏览器支持参考
- CSS - 网页字体
- CSS 工具
- CSS - PX 到 EM 的转换
CSS - 清除修复
Clearfix 是一种技术,用于确保容器正确包围并在其中包含浮动元素。它通过向容器添加一个空元素来防止布局问题,该元素会清除左右浮动,从而允许容器扩展并保持其预期的布局。
Clearfix 有助于防止容器倒塌、高度不平坦、内容物重叠、对齐不一致等问题。
本章将探讨 clearfix 技术如何确保容器元素正确包含其浮动子元素。
如上所述,CSS clearfix 修复了所需元素的溢出元素。为此,可以处理以下三个属性:
下图演示了 clearfix 布局以供参考:
CSS Clearfix - 带有溢出和浮动属性
让我们看一个示例,其中图像大于其容器的高度,导致图像超出其容器的边界,并可能破坏布局。
<html>
<head>
<style>
div {
border: 2px solid #f0610e;
padding: 5px;
background-color: #86f00e;
}
.tutimg {
float: right;
border: 3px solid #f0610e;
}
</style>
</head>
<body>
<h2>Without clearfix</h2>
<div>
<img class="tutimg" src="images/tutimg.png" width="200" height="200">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet.
</div>
</body>
</html>
CSS Clearfix - 带溢出属性
为了解决这个溢出问题,我们可以将 overflow: auto; 属性设置为相应的元素,确保图像完全包含在容器中。
<html>
<head>
<style>
div {
border: 2px solid #f0610e;
padding: 5px;
background-color: #86f00e;
}
.tutimg {
float: right;
border: 3px solid #f0610e;
}
.custom-clearfix {
overflow: auto;
}
</style>
</head>
<body>
<h2>With clearfix</h2>
<div class="custom-clearfix">
<img class="tutimg" src="images/tutimg.png" width="200" height="200">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet.
</div>
</body>
</html>
CSS Clearfix - 带 height 属性
您还可以通过设置类似于浮动图像高度的 <div> 元素的高度来实现 clearfix。
<html>
<head>
<style>
div {
border: 2px solid #f0610e;
padding: 10px;
height: 120px;
background-color: #86f00e;
}
.tutimg {
float: right;
border: 3px solid #f0610e;
}
</style>
</head>
<body>
<div>
<img class="tutimg" src="images/tutimg.png" width="120" height="120">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet.
</div>
</body>
</html>
CCS Clear 属性
clear CSS 属性通过将元素移动到浮点概念上来影响浮点概念,而不是让它们向上移动以占用可用空间。clear 属性适用于浮动元素和非浮动元素。
可能的值为:
- none: 是一个关键字,指示元素不会向下移动以清除过去的浮动元素。
- left: 是一个关键字,指示元素向下移动以清除过去的左侧浮点数。
- right: 是一个关键字,指示元素向下移动以清除过去的右浮点数。
- both: 是一个关键字,指示元素向下移动以清除左侧和右侧浮点数。
- inline-start:: 是一个关键字,指示元素向下移动以清除其包含块的起始侧的浮点数,即在 ltr 脚本上左浮点数,在 rtl 脚本上右浮点数。
- inline-end: 是一个关键字,指示元素向下移动以清除其包含块的末端侧的浮点数,即在 ltr 脚本上右边浮点,在 rtl 脚本上左浮点。
CSS clear - left 值
以下示例演示了使用 clear:left 属性的 clearfix:
<html>
<head>
<style>
.mainbody{
border: 1px solid black;
padding: 10px;
}
.left {
border: 1px solid black;
clear: left;
}
.aqua{
float: left;
margin: 0;
background-color: aqua;
color: #000;
width: 20%;
}
.pink{
float: left;
margin: 0;
background-color: pink;
width: 20%;
}
p {
width: 50%;
}
</style>
</head>
<body>
<div class="mainbody">
<p class="aqua">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in ton the Internet.
</p>
<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
<p class="left">This paragraph clears left.</p>
</div>
</body>
</html>
CSS clear - right 值
以下示例演示了使用 clear:right 属性的 clearfix:
<html>
<head>
<style>
.mainbody{
border: 1px solid black;
padding: 10px;
}
.right {
border: 1px solid black;
clear: right;
}
.aqua{
float: right;
margin: 0;
background-color: black;
color: #fff;
width: 20%;
}
.pink{
float: right;
margin: 0;
background-color: pink;
width: 20%;
}
p {
width: 50%;
}
</style>
</head>
<body>
<div class="mainbody">
<p class="aqua">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in ton the Internet.
</p>
<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
<p class="right">This paragraph clears right.</p>
</div>
</body>
</html>
CSS clear - both 值
以下示例演示了使用 clear:both 属性的 clearfix:
<html>
<head>
<style>
.mainbody{
border: 1px solid black;
padding: 10px;
}
.both {
border: 1px solid black;
clear: both;
}
.aqua{
float: left;
margin: 0;
background-color: black;
color: #fff;
width: 20%;
}
.pink {
float: right;
margin: 0;
background-color: pink;
width: 20%;
}
p {
width: 45%;
}
</style>
</head>
<body>
<div class="mainbody">
<p class="aqua">
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in ton the Internet.
</p>
<p class="pink">Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</p>
<p class="both">This paragraph clears both.</p>
</div>
</body>
</html>