浮动布局是 CSS 中常用的布局方法之一,它使用 float 属性让元素向左或向右浮动,从而实现文本环绕效果和多栏布局。虽然现代 CSS 有更强大的布局方式,但理解浮动布局仍然很重要。
浮动布局是使用 float 属性让元素向左或向右浮动,脱离正常的文档流,从而实现特殊的布局效果。
/* 向左浮动 */
.float-left {
float: left;
width: 200px;
height: 100px;
background: #3498db;
}
/* 向右浮动 */
.float-right {
float: right;
width: 200px;
height: 100px;
background: #e74c3c;
}
/* 不浮动(默认) */
.no-float {
float: none;
}
/* 两栏布局 */
.container {
width: 100%;
overflow: hidden; /* 清除浮动 */
}
.left-column {
float: left;
width: 70%;
background: #3498db;
min-height: 300px;
}
.right-column {
float: right;
width: 30%;
background: #e74c3c;
min-height: 300px;
}
/* 左侧固定,右侧自适应 */
.container {
width: 100%;
overflow: hidden;
}
.left-column {
float: left;
width: 250px;
background: #3498db;
min-height: 300px;
}
.right-column {
margin-left: 250px;
background: #e74c3c;
min-height: 300px;
}
/* 右侧固定,左侧自适应 */
.container {
width: 100%;
overflow: hidden;
}
.left-column {
margin-right: 250px;
background: #3498db;
min-height: 300px;
}
.right-column {
float: right;
width: 250px;
background: #e74c3c;
min-height: 300px;
}
/* 三栏布局 */
.container {
width: 100%;
overflow: hidden;
}
.left-column {
float: left;
width: 25%;
background: #2ecc71;
min-height: 300px;
}
.center-column {
float: left;
width: 50%;
background: #3498db;
min-height: 300px;
}
.right-column {
float: right;
width: 25%;
background: #e74c3c;
min-height: 300px;
}
/* 中间自适应,两侧固定 */
.container {
width: 100%;
overflow: hidden;
}
.left-column {
float: left;
width: 200px;
background: #2ecc71;
min-height: 300px;
}
.center-column {
margin: 0 200px;
background: #3498db;
min-height: 300px;
}
.right-column {
float: right;
width: 200px;
background: #e74c3c;
min-height: 300px;
}
浮动元素会脱离文档流,导致父元素高度塌陷,影响后续元素的布局。
/* 使用 clear 属性清除浮动 */
.clear {
clear: both;
}
.clear-left {
clear: left;
}
.clear-right {
clear: right;
}
/* 使用 overflow 属性清除浮动 */
.container {
overflow: hidden;
}
.container {
overflow: auto;
}
/* 使用 ::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);
}
/* 页面布局 */
.header {
width: 100%;
height: 60px;
background: #333;
color: white;
}
.nav {
width: 100%;
height: 40px;
background: #555;
color: white;
}
.main-container {
width: 100%;
overflow: hidden;
min-height: 400px;
}
.sidebar {
float: left;
width: 250px;
background: #f5f5f5;
padding: 20px;
}
.content {
float: right;
width: calc(100% - 250px);
padding: 20px;
}
.footer {
width: 100%;
height: 40px;
background: #2c3e50;
color: white;
clear: both;
}
/* 问题 - 父元素高度塌陷 */
.container {
background: #f5f5f5;
}
.float-element {
float: left;
width: 100px;
height: 100px;
}
/* 解决方法 - 清除浮动 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 问题 - 浮动元素影响后续元素 */
.float-element {
float: left;
width: 100px;
height: 100px;
}
.normal-element {
/* 会受到浮动元素的影响 */
}
/* 解决方法 - 清除浮动 */
.normal-element {
clear: left;
}
/* 问题 - 浮动元素重叠 */
.float-element1 {
float: left;
width: 100%;
}
.float-element2 {
float: left;
width: 100%;
}
/* 解决方法 - 调整宽度 */
.float-element1 {
float: left;
width: 50%;
}
.float-element2 {
float: left;
width: 50%;
}
/* 推荐 - 使用伪元素清除浮动 */
.clearfix::after {
content: "";
display: table;
clear: both;
}
/* 不推荐 - 忘记清除浮动 */
.container {
/* 没有清除浮动,高度会塌陷 */
}
/* 推荐 - 使用 box-sizing */
* {
box-sizing: border-box;
}
.column {
float: left;
width: 33.333%;
padding: 10px;
}
/* 不推荐 - 不使用 box-sizing */
.column {
float: left;
width: 33.333%;
padding: 10px;
/* 边框和内边距会影响宽度 */
}
/* 推荐 - 使用 calc 计算宽度 */
.column {
float: left;
width: calc(33.333% - 20px);
margin: 10px;
}
/* 不推荐 - 手动计算宽度 */
.column {
float: left;
width: 32%;
margin: 0.666%;
/* 计算复杂,容易出错 */
}
浮动布局是 CSS 中常用的布局方法:
记住以下几点: