浮动属性是 CSS 中用于布局的重要属性之一,它可以让元素向左或向右浮动,从而实现文本环绕效果和创建多栏布局。虽然现代 CSS 有更强大的布局方式,但理解浮动仍然很重要。
float 属性用于设置元素的浮动方向。
/* 向左浮动 */
.left {
float: left;
}
/* 向右浮动 */
.right {
float: right;
}
/* 不浮动(默认) */
.none {
float: none;
}
/* 图片浮动实现文字环绕 */
.float-image {
float: left;
width: 200px;
height: 150px;
margin: 0 20px 20px 0;
}
/* 文字会环绕浮动的图片 */
.text {
/* 文字会自动环绕浮动的图片 */
}
clear 属性用于清除浮动,防止元素受到影响。
/* 清除左侧浮动 */
.clear-left {
clear: left;
}
/* 清除右侧浮动 */
.clear-right {
clear: right;
}
/* 清除两侧浮动 */
.clear-both {
clear: both;
}
/* 不清除浮动(默认) */
.clear-none {
clear: none;
}
/* 清除浮动后的元素 */
.clear {
clear: both;
}
/* 在浮动元素后清除浮动 */
.float-container::after {
content: "";
display: table;
clear: both;
}
/* 两栏布局 */
.container {
width: 100%;
overflow: hidden; /* 清除浮动 */
}
.left-column {
float: left;
width: 70%;
background: #3498db;
}
.right-column {
float: right;
width: 30%;
background: #e74c3c;
}
/* 三栏布局 */
.container {
width: 100%;
overflow: hidden;
}
.left-column {
float: left;
width: 25%;
background: #2ecc71;
}
.center-column {
float: left;
width: 50%;
background: #3498db;
}
.right-column {
float: right;
width: 25%;
background: #e74c3c;
}
/* 在浮动元素后添加清除元素 */
.clearfix {
clear: both;
}
/* 父元素设置 overflow */
.container {
overflow: hidden;
}
/* 使用 ::after 伪元素 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 使用 ::before 和 ::after 伪元素 */
.clearfix::before,
.clearfix::after {
content: "";
display: table;
}
.clearfix::after {
clear: both;
}
<div class="container">
<div class="float-left">浮动元素</div>
<div class="float-right">浮动元素</div>
<div class="clear"></div>
</div>
<style>
.clear {
clear: both;
}
</style>
/* 图片向左浮动,文字环绕 */
.article-image {
float: left;
width: 200px;
height: 150px;
margin: 0 20px 20px 0;
border-radius: 8px;
}
.article-content {
/* 文字会自动环绕浮动的图片 */
line-height: 1.6;
}
/* 浮动导航菜单 */
.nav {
overflow: hidden;
background: #333;
}
.nav-item {
float: left;
}
.nav-link {
display: block;
padding: 15px 20px;
color: white;
text-decoration: none;
}
.nav-link:hover {
background: #555;
}
/* 浮动产品卡片 */
.product-list {
overflow: hidden;
}
.product-card {
float: left;
width: calc(33.333% - 20px);
margin: 10px;
background: white;
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
/* 侧边栏布局 */
.main-container {
overflow: hidden;
width: 100%;
}
.sidebar {
float: left;
width: 250px;
background: #f5f5f5;
padding: 20px;
}
.content {
float: right;
width: calc(100% - 250px);
padding: 20px;
}
/* 父元素高度塌陷问题 */
.container {
background: #f5f5f5;
/* 高度为 0,因为子元素浮动 */
}
.float-element {
float: left;
width: 100px;
height: 100px;
background: #3498db;
}
/* 解决方法:清除浮动 */
.container::after {
content: "";
display: table;
clear: both;
}
/* 浮动元素影响后续元素 */
.float-left {
float: left;
width: 100px;
height: 100px;
background: #3498db;
}
.normal-element {
/* 会受到浮动元素的影响 */
background: #e74c3c;
}
/* 解决方法:清除浮动 */
.normal-element {
clear: left;
}
/* 推荐 - 使用伪元素清除浮动 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 不推荐 - 忘记清除浮动 */
.container {
/* 没有清除浮动,高度会塌陷 */
}
/* 推荐 - 使用现代布局方式 */
.container {
display: flex;
}
/* 不推荐 - 使用浮动进行复杂布局 */
.container {
/* 浮动布局不够灵活 */
}
/* 注意浮动对布局的影响 */
.float-container {
overflow: hidden;
}
.float-element {
float: left;
}
/* 浮动布局 */
.container {
overflow: hidden;
}
.left {
float: left;
width: 50%;
}
.right {
float: right;
width: 50%;
}
/* Flexbox 布局 */
.container {
display: flex;
}
.left {
flex: 1;
}
.right {
flex: 1;
}
浮动属性是 CSS 中用于布局的重要属性:
记住以下几点: