<div class="box">
<div class="left"></div>
<div class="right"></div>
</div>
- flex布局
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
display: flex;
}
.left {
width: 200px;
height: 100%;
background-color: red;
}
.right {
width: 100%;
height: 100%;
background-color:green;
flex: 1;
}
- BFC模式
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
}
.left {
width: 200px;
height: 100%;
background-color: red;
float: left;
}
.right {
overflow: hidden; /* 触发bfc */
height: 100%;
background-color:green;
}
- padding-left
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
}
.left {
width: 200px;
height: 100%;
background-color: red;
float: left;
}
.right {
width: 100%;
height: 100%;
background-color:green;
padding-left: 200px;
box-sizing: border-box;
}
- margin-left
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
}
.left{
float:left;
width:200px;
height:100%;
background:red;
}
.right{
height:100%;
background:green;
margin-left:200px;
}
- grid
.main{
height: 200px;
display: grid;
/*分成两列*/
grid-template-columns: minmax(200px, 25%) 1fr;
/* 分成三列*/
/* grid-template-columns: minmax(200px, 200px) 1fr minmax(200px, 400px);*/
}
grid-template-columns指定页面分成两列。第一列的宽度是minmax(200px, 25%),即最小宽度为200px,最大宽度为总宽度的25%;第二列为1fr,即所有剩余宽度。
- calc()
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
}
.left{
width:200px;
height:100%;
background:red;
float: left;
}
.right{
height:100%;
background:green;
float: right;
width: calc(100% - 200px);
}
- table布局
.box {
width: 100%;
height: 300px;
border: 1px solid #ccc;
display: table;
}
.left{
width:200px;
height:100%;
background:red;
display: table-cell;
}
.right{
height:100%;
background:green;
display: table-cell;
}
左右定宽中间自适应
1. center在right之后
<div class="box">
<div class="left">left</div>
<div class="right">rigth</div>
<div class="center">center</div>
</div>
.box {
width: 100%;
height: 100%;
}
.left {
float: left;
width: 200px;
height: 100%;
background-color: red;
}
.right {
float: right;
width: 200px;
height: 100%;
background-color: pink;
}
.center {
height: 100%;
background-color: green;
}
2. div顺序正常排列使用flex布局
<div class="box">
<div class="left">left</div>
<div class="center">center</div>
<div class="right">rigth</div>
</div>
.box {
display: flex;
}
.left,
right {
width: 200px
}
.center {
flex: 1;
}
3. grid布局
.main {
height: 200px;
display: grid;
grid-template-columns: minmax(200px, 200px) 1fr minmax(200px, 400px);
}