两栏式布局
1. 左列定宽,右列自适应
<div class="container">
<div class="left">定宽左部分</div>
<div class="right">自适应右部分</div>
</div>
方法一:浮动 + margin
.container{
width: 70%;
border: 2px solid red;
}
.left{
width: 300px;
background-color: rgba(217, 255, 0, 0.815);
height: 300px;
float: left;
}
.right{
background-color: rgba(137, 43, 226, 0.952);
height: 500px;
margin-left: 300px;
}
方法二:浮动 + BFC
.container{
width: 70%;
border: 2px solid red;
}
.left{
width: 300px;
background-color: rgba(217, 255, 0, 0.815);
height: 300px;
float: left;
}
.right{
background-color: rgba(137, 43, 226, 0.952);
height: 500px;
overflow: hidden;
}
方法三:定位
.container{
width: 70%;
border: 2px solid red;
position: relative;
}
.left{
width: 300px;
background-color: rgba(217, 255, 0, 0.815);
height: 300px;
position: absolute;
left: 0px;
}
.right{
background-color: rgba(137, 43, 226, 0.952);
height: 500px;
position: absolute;
left: 300px;
right: 0;
}
方法四:flex 布局
.container{
width: 70%;
border: 2px solid red;
display: flex;
}
.left{
width: 300px;
background-color: rgba(217, 255, 0, 0.815);
height: 300px;
}
.right{
background-color: rgba(137, 43, 226, 0.952);
height: 500px;
flex: 1;
}
方法五:浮动 + 父外边距
.container{
width: 70%;
border: 2px solid red;
}
.container::after{
display: block;
content: '';
clear: both;
}
.left{
width: 300px;
height: 300px;
background-color: yellow;
float: left;
margin-right: -100%;
}
.right{
width: 100%;
height: 500px;
float: left;
}
.content{
height: 100%;
background-color: green;
margin-left: 300px;
}
方法六:浮动布局
.container{
width: 70%;
border: 2px solid black;
display: table;
}
.left{
width: 300px;
height: 300px;
display: table-cell;
background-color: yellow;
}
.right{
display: table-cell;
height: 500px;
background-color: blue;
}
2. 左列不定宽,右列自适应
方法一:flex
.container{
width: 70%;
border: 2px solid black;
display: flex;
}
.left{
width: 300px;
height: 300px;
background-color: yellow;
}
.right{
flex: 1;
height: 500px;
background-color: blue;
}
方法二:浮动 + BFC
.container{
width: 70%;
border: 2px solid black;
}
.left{
width: 300px;
height: 300px;
background-color: yellow;
float: left;
}
.right{
height: 500px;
background-color: blue;
overflow: hidden;
}