Flex 布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.container {
display: flex;
}
.left {
width: 200px; /* 固定左边宽度 */
background-color: lightblue;
}
.right {
flex: 1; /* 右边占据剩余空间 */
background-color: lightpink;
}
</style>
<body>
<div class="container">
<div class="left">左边内容</div>
<div class="right">右边内容</div>
</div>
</body>
</html>
浮动 + margin
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.container {
overflow: hidden;
/* 清除浮动 */
}
.left {
float: left;
width: 200px;
background-color: lightblue;
}
.right {
margin-left: 200px;
/* 与左边元素的宽度相同 */
background-color: lightpink;
}
</style>
<body>
<div class="container">
<div class="left">左边内容</div>
<div class="right">右边内容</div>
</div>
</body>
</html>
Grid 布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
.container {
display: grid;
grid-template-columns: 200px 1fr;
}
.left {
background-color: lightblue;
}
.right {
background-color: lightpink;
}
</style>
<body>
<div class="container">
<div class="left">左边内容</div>
<div class="right">右边内容</div>
</div>
</body>
</html>
.container {
display: grid;
grid-template-columns: 200px 1fr;
}
1fr
:表示第二列会占据剩余的所有空间。fr
是一个弹性单位,代表"分数",它会根据剩余空间的比例来分配宽度。