实现HTML两栏布局
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
是一个弹性单位,代表“分数”,它会根据剩余空间的比例来分配宽度。
原文地址:https://blog.csdn.net/weixin_45596561/article/details/142464975
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!