一、两列布局
一般两栏布局指的是左边一栏宽度固定,右边一栏宽度自适应
1、利用浮动。
(1)左边设置固定宽度,设置向左浮动。右边设置margin-left: width ; 宽度设置为auto(不设置也行,默认是auto,撑满整个父元素)
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
float: left;
}
.right {
background-color: yellowgreen;
height: 100%;
margin-left: 200px;
width: auto; /* 当这个没有设置时,也是可以的 */
}
</style>
<body>
<div class="content">
<div class="left">左边一栏宽度固定</div>
<div class="right">右边一栏宽度自适应</div>
</div>
</body>
(2)左边设置固定大小,并设置向左浮动,右边设置overflow:hidden;触发BFC(BFC的区域不会与浮动元素发生重叠)
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
float: left;
}
.right {
background-color: yellowgreen;
height: 100%;
overflow: hidden;
}
</style>
<body>
<div class="content">
<div class="left">左边一栏宽度固定</div>
<div class="right">右边一栏宽度自适应</div>
</div>
</body>
2、利用绝对定位
(1)父元素设置 position: relative;左边设置position:absolute;固定宽度;右边设置margin-left:width;
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
position: relative;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
position: absolute;
}
.right {
background-color: yellowgreen;
height: 100%;
margin-left: 200px;
}
</style>
<body>
<div class="content">
<div class="left">左边一栏宽度固定</div>
<div class="right">右边一栏宽度自适应</div>
</div>
</body>
(2)父元素设置position: relative;左边设置固定宽度;右边设置position: absolute;left: width;其他方位定义为0
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
position: relative;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
}
.right {
background-color: yellowgreen;
height: 100%;
position: absolute;
left: 200px;
top: 0;
right: 0;
bottom: 0;
}
</style>
<body>
<div class="content">
<div class="left">左边一栏宽度固定</div>
<div class="right">右边一栏宽度自适应</div>
</div>
</body>
3、利用flex布局:父元素设置 display: flex; 左边设置固定宽度; 右边设置flex:1
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
display: flex;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
}
.right {
background-color: yellowgreen;
height: 100%;
flex: 1;
}
</style>
<body>
<div class="content">
<div class="left">左边一栏宽度固定</div>
<div class="right">右边一栏宽度自适应</div>
</div>
</body>
二、三栏布局
一般指页面中一共有三栏,左右两栏宽度固定,中间自适应的布局
1、利用浮动。左右固定宽度,并设置向左向右的浮动。中间设置左右两个方向的margin值。(中间一栏必须放到最后)
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
float: left;
}
.right {
background-color: yellowgreen;
height: 100%;
width: 200px;
float: right;
}
.center {
background-color: blue;
height: 100%;
margin-left: 200px;
margin-right: 200px;
}
</style>
<body>
<div class="content">
<div class="left">宽度固定</div>
<div class="right">宽度固定</div>
<div class="center">中间自适应</div>
</div>
</body>
2、利用绝对定位。左右两栏设置为绝对定位,中间设置对应方向大小的margin的值。
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
position: relative;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
position: absolute;
}
.right {
background-color: yellowgreen;
height: 100%;
width: 200px;
position: absolute;
right: 0;
top: 0;
}
.center {
background-color: blue;
height: 100%;
margin-left: 200px;
margin-right: 200px;
}
</style>
<body>
<div class="content">
<div class="left">宽度固定</div>
<div class="center">中间自适应</div>
<div class="right">宽度固定</div>
</div>
</body>
3、利用flex布局。左右两栏设置固定大小,中间一栏设置为flex:1。
<style>
* {
padding: 0;
margin: 0;
}
.content {
height: 200px;
display: flex;
}
.left {
background-color: aqua;
height: 100%;
width: 200px;
}
.right {
background-color: yellowgreen;
height: 100%;
width: 200px;
}
.center {
background-color: blue;
height: 100%;
flex: 1;
}
</style>
<body>
<div class="content">
<div class="left">宽度固定</div>
<div class="center">中间自适应</div>
<div class="right">宽度固定</div>
</div>
</body>
4、网格布局。在父元素上设置就行。
<style>
* {
padding: 0;
margin: 0;
}
.content {
display: grid;
width: 100%;
grid-template-columns: 200px auto 200px;
grid-template-rows: 200px;
}
.left {
background-color: aqua;
}
.right {
background-color: yellowgreen;
}
.center {
background-color: blue;
}
</style>
<body>
<div class="content">
<div class="left">宽度固定</div>
<div class="center">中间自适应</div>
<div class="right">宽度固定</div>
</div>
</body>
5、表格布局。父元素设置display: table, width: 100%; 三列都设置display: table-cell;左右设置固定宽度
<style>
* {
padding: 0;
margin: 0;
}
.content {
display: table;
width: 100%;
height: 200px;
}
.content div {
display: table-cell;
}
.left {
background-color: aqua;
width: 200px;
}
.right {
background-color: yellowgreen;
width: 200px;
}
.center {
background-color: blue;
}
</style>
<body>
<div class="content">
<div class="left">宽度固定</div>
<div class="center">中间自适应</div>
<div class="right">宽度固定</div>
</div>
</body>
如果去掉高度已知,考虑纵向,哪种方案还适用,哪些不适用
flex布局和表格布局是通用的,其它方案不适用
三、“品”字布局
1、第一个div设置margin:0 auto;第二个设置向左浮动,margin-left:50%;第三个设置向左浮动,miang-left: 2*width;
2、将第二个和第三个向左浮动改为display: inline-block;其他不变
<style>
* {
padding: 0;
margin: 0;
}
.content div {
width: 100px;
height: 100px;
text-align: center;
line-height: 100px;
}
.d1 {
background-color: aqua;
margin: 0 auto;
}
.d2 {
background-color: yellowgreen;
float: left; /* 可替换成display: inline-block; */
margin-left: 50%;
}
.d3 {
background-color: blue;
float: left; /* 可替换成display: inline-block; */
margin-left: -200px;
}
</style>
<body>
<div class="content">
<div class="d1">1</div>
<div class="d2">2</div>
<div class="d3">3</div>
</div>
</body>
四、九宫格布局
待更新