在牛客上看面经时,发现有很多关于CSS布局的问题,尤其是两栏、三栏布局。
b站css两栏布局
b站css三栏布局
<div class="container">
<h2 style="text-align:center">利用浮动+margin实现两栏布局h2>
<div class="left">div>
<div class="right">div>
div>
* {
margin: 0;
padding: 0;
}
.left {
height:100%;
width:300px;
background-color: orange;
float: left;
}
.right {
height:100%;
background-color: blue;
margin-left: 300px;
}
.container {
height:800px;
}
了解BFC
.left {
height:100%;
width:300px;
background-color: orange;
float: left;
}
.right {
height:100%;
background-color: blue;
/* 触发BFC */
overflow: hidden;
}
.container {
height:800px;
}
.container {
position: relative;
height:800px;
}
.left {
height:100%;
position: absolute;
left:0;
width:300px;
background-color: orange;
}
/* .right {
height:100%;
background-color: blue;
margin-left: 300px;
} */
.right {
height:100%;
position: absolute;
background-color: blue;
left: 300px;
right:0;
}
.container {
display: flex;
height:800px;
}
.left {
height:100%;
width:300px;
background-color: orange;
}
.right {
height:100%;
flex:1;
background-color: blue;
}
.container {
height:800px;
}
.left {
height:100%;
width:300px;
float: left;
margin-right: -100%;
background-color: orange;
}
.right {
height:100%;
width: 100%;
float: left;
/* background-color: red; */
}
.content{
height: 100%;
margin-left: 300px;
background-color: blue;
}
.container {
width:100%;
display: table;
height:800px;
}
.left {
height:100%;
width:300px;
display: table-cell;
background-color: orange;
}
.right {
height:100%;
display: table-cell;
background-color: blue;
}
在左列定宽,右列自适应的情况下,去掉左列的宽度即可。
左列的宽度:
width:20%;
左列的宽度:
width:20%;
<h2 style="text-align:center">利用浮动+margin实现三栏布局h2>
<div class="container">
<div class="left">div>
<div class="right">div>
<div class="main">div>
div>
.container{
height: 800px;
}
.left{
height: 100%;
float: left;
width: 200px;
background-color: red;
}
.right{
height: 100%;
float: right;
width: 200px;
background-color: blue;
}
.main{
height: 100%;
margin-left: 200px;
margin-right: 200px;
background-color: green;
}
触发中间列的BFC
.main{
height: 100%;
overflow: hidden;
background-color: green;
}
.container{
height: 800px;
display: flex;
}
.left{
height: 100%;
width: 200px;
background-color: red;
}
.right{
height: 100%;
width: 200px;
background-color: blue;
}
.main{
height: 100%;
flex: 1;
background-color: green;
}
.container{
height: 800px;
display: table;
width: 100%;
}
.left{
height: 100%;
display: table-cell;
width: 200px;
background-color: red;
}
.right{
height: 100%;
display: table-cell;
width: 200px;
background-color: blue;
}
.main{
height: 100%;
width: 60%;
display: table-cell;
background-color: green;
}
注意容器的排列顺序:
<div class="left">div>
<div class="main">div>
<div class="right">div>
.container{
height: 800px;
position: relative;
}
.left{
height: 100%;
position: absolute;
left: 0;
width: 200px;
background-color: red;
}
.right{
height: 100%;
position: absolute;
right: 0;
width: 200px;
background-color: blue;
}
.main{
height: 100%;
margin: 0 200px;
background-color: green;
}
圣杯布局以class="main"
这个div为主:
<div class="container">
<div class="main">div>
<div class="left">div>
<div class="right">div>
div>
.container{
height: 800px;
margin-left: 200px;
margin-right: 200px;
}
.main{
width: 100%;
height: 100%;
float: left;
background-color: red;
}
.left{
height: 100%;
float: left;
width: 200px;
margin-left: -100%;
position: relative;
left: -200px;
background-color: blue;
}
.right{
height: 100%;
float: left;
width: 200px;
margin-left: -200px;
position: relative;
right: -200px;
background-color: green;
}
<div class="content">
<div class="main">div>
div>
<div class="left">div>
<div class="right">div>
.content{
height: 600px;
float: left;
width: 100%;
}
.main{
margin-left: 200px;
margin-right: 200px;
height: 600px;
background-color: red;
}
.left{
height: 600px;
float: left;
width: 200px;
margin-left: -100%;
background-color: blue;
}
.right{
height: 600px;
float: left;
width: 200px;
margin-left: -200px;
background-color: green;
}
视频中的例子都没加高度,所以写的时候要自己加高度。
圣杯布局和双飞翼布局好难理解