文章目录
css实现两列/三列布局
两列布局
第一种方式:浮动实现
(1)清除浮动
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>calc实现</title>
</head>
<body>
<!-- 两列布局 -->
<div class="left">左边定宽</div>
<div class="right">右边自适应</div>
</body>
</html>
<style>
.left{
width: 100px;
height: 1000px;
background-color: red;
float: left;
}
.right{
height:1000px;
background-color: yellow;
overflow:auto;
}
</style>
结果:
(2)父元素清除浮动
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
</div>
body,
html {
margin: 0px;
padding: 0px;
}
.container {
width: 100%;
height: 100vh;
overflow:auto;
}
.left,
.right {
float: left;
height: inherit;
}
.left {
width: 20%;
background-color: gray;
}
.right {
width: 80%;
background-color: rgb(198, 154, 154);
}
第二种方式:通过calc()实现自适应
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
<title>calc实现</title>
</head>
<body>
<div class="left">左边定宽</div><div class="right">右边自适应</div>
<!--将两个div元素连着一起写不换行是避免换行到时导致显示有间隙
这是由display:inline-block的特性决定的,设置该属性后,两个元素之间的换行符或者空格就会生效-->
</body>
</html>
<style>
.left{
width:300px;
height:1000px;
background-color: red;
display: inline-block;
}
.right{
width:calc(100% - 300px);
height:1000px;
background-color: orange;
display: inline-block;
}
</style>
分析:
(1)左边定宽,是因为设置了宽度,右边自适应是通过calc(100% - xxxpx)实现的,表示的是浏览器整个页面的宽度减去定宽的宽度,剩下的都来做自适应,需要注意calc中的减号两边有空格
(2)无论怎么缩放页面的大小,定宽区域的宽度其实是相对没有改变的,改变的是自适应区域。
第三种方式:定位实现
.container{
width: 100%;
height: 100vh;
position: relative;
background-color: green;
}
.left{
position:absolute;
left:0;
height: 100vh;
width: 200px;
background-color:pink;
}
.right{
position: absolute;
right:0;
height: 100vh;
background-color: red;
width: calc(100% - 200px);
}
<div class="container">
<div class="left">left</div><div class="right">right</div>
</div>
第四种方式:flex布局实现
<div class="container">
<div class="left">left</div><div class="right">right</div>
</div>
(1)使用flex:xxx属性实现
.container{
display: flex;
width: 100%;
height: 100vh;
}
.left{
width: 200px;
height: 100vh;
flex:1; /* 占据容器宽度1/8*/
background-color: pink;
}
.right{
height: 100vh;
flex:7; /* 占据容器宽度的7/8*/
background-color:rgb(246, 190, 190);
}
(2)使用flex-grow属性实现
.container{
display: flex;
width: 100%;
height: 100vh;
}
.left{
width: 200px;
height: 100vh;
flex-grow:0; /* 设置为0表示不放大,使用width宽度*/
background-color: pink;
}
.right{
flex-grow: 1; /* 设置为1表示放大并占据剩余容器的宽度*/
height: 100vh;
background-color:rgb(246, 190, 190);
}
第五种方式:网格布局grid实现
<div class="container">
<div class="left">left</div><div class="right">right</div>
</div>
.container{
display:grid;
grid-template-columns: 1fr 8fr;
grid-template-rows: 100vh;
gap:0;
}
.left{
background-color:rgb(133, 240, 94);
}
.right{
background-color: rgb(217, 234, 107);
}
三列布局
方式一:calc()灵活计算自适应宽度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=<device-width>, initial-scale=1.0">
<title>calc实现</title>
</head>
<body>
<div class="left">左边定宽</div><div class="center">中间自适应</div><div class="right">右边定宽</div>
<!--将三个div元素连着一起写不换行是避免换行到时导致显示有间隙-->
</body>
</html>
<style>
.left{
width:300px;
height:1000px;
background-color: red;
display: inline-block;
}
.center{
width:calc(100% - 600px);
height:1000px;
background-color: orange;
display: inline-block;
}
.right{
width: 300px;
height: 1000px;
background-color: yellow;
display: inline-block;
}
</style>
分析:
左右两边定宽,是因为设置了宽度,中间自适应是通过calc(100% - xxxpx)实现的,表示的是浏览器整个页面的宽度减去定宽的宽度,剩下的都来做自适应,需要注意calc中的减号两边有空格
方式二:浮动
<!-- TODO:浮动实现三列布局 -->
<div class="container">
<div class="left">left</div>
<div class="main">main</div>
<div class="right">right</div>
</div>
body,
html {
margin: 0;
padding: 0;
}
.container {
width: 100%;
overflow: hidden;
height: 100vh;
}
.left,
.main,
.right {
float: left;
}
.left {
width: 20%;
height: inherit;
background-color: #f2f2f2;
}
.main {
width: 60%;
height: inherit;
background-color: #d9d9d9;
}
.right {
width: 20%;
height: inherit;
background-color: #f2f2f2;
}
方式三:position定位
主要代码
<div class="container">
<div class="left">left</div>
<div class="middle">middle</div>
<div class="right">right</div>
</div>
body,
html {
margin: 0;
padding: 0;
}
.container {
position: relative;
width: 100%;
height: 100vh;
}
.left {
position: absolute;
left: 0;
height: 100vh;
width: 200px;
background-color: rgb(234, 160, 160);
}
.middle {
width: calc(100% - 400px);
position: absolute;
left:200px;
height: 100vh;
background-color: #f7cef6;
}
.right {
position: absolute;
right: 0;
width: 200px;
height: 100vh;
background-color: #bce352;
}
方式四:grid网格布局实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
.outer{
display: grid;
grid-template-columns: 1fr 2fr 1fr; /* 三列,比例1:2:1*/
grid-template-rows:1000px;
/* gap:10px */
}
.right{
background-color: red;
}
.middle{
background-color: #6ab690;
}
.left{
background-color: pink;
}
</style>
</head>
<body>
<div class="outer">
<div class="right">right</div>
<div class="middle">middle</div>
<div class="left">left</div>
</div>
<script></script>
</body>
</html>
方式五:弹性布局flex实现,参考两列布局的实现
。。。