float瀏覽器
body,
.container{
margin: 0;
padding: 0;
}
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
margin-left: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
position: absolute佈局
body,
.container{
margin: 0;
padding: 0;
}
.left{
position: absolute;
left: 0;
top: 0;
width: 100px;
height: 100px;
background: red;
}
.right{
margin-left: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
BFCflex
.container{
margin: 0;
padding: 0;
}
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
overflow: hidden;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
flexspa
.container{
margin: 0;
padding: 0;
display: flex;
}
.left{
width: 100px;
height: 100px;
background: red;
}
.right{
flex-grow: 1;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
float-overflow:hiddencode
.container{
zoom: 1;
}
.left{
float: left;
background: red;
}
.right{
overflow: hidden;
height: 100px;
background: blue;
zoom: 1;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
複製代碼
flex 佈局it
.container{
display: flex;
}
.left{
background: red;
}
.right{
flex-grow: 1;
background: blue;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
複製代碼
grid佈局io
.container{
display: grid;
grid-template-columns:auto 1fr;
}
.left{
background: red;
}
.right{
background: blue;
}
<div class="container">
<div class="left">
<p>11111111111111111111</p>
</div>
<div class="right"></div>
</div>
複製代碼
流體佈局table
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
float: right;
width: 100px;
height: 100px;
background: blue;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
複製代碼
BFC 三欄佈局class
.left{
float: left;
width: 100px;
height: 100px;
background: red;
}
.right{
float: right;
width: 100px;
height: 100px;
background: blue;
}
.center{
overflow: hidden;
height: 100px;
background: orange;
}
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="center"></div>
</div>
複製代碼
雙飛翼佈局容器
.container{
float: left;
width: 100%;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
.left{
float: left;
margin-left: -100%;
width: 100px;
height: 100px;
background: red;
}
.right{
float: left;
margin-left: -100px;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
</div>
<div class="left"></div>
<div class="right"></div>
複製代碼
聖盃佈局
和與雙飛翼佈局的區別:與雙飛翼佈局很像,有一些細節上的區別,相對於雙飛翼佈局來講,HTML 結構相對簡單,可是樣式定義就稍微複雜,
優勢:也是優先加載內容主體。
代碼實現
.container{
margin-left: 100px;
margin-right: 100px;
}
.center{
float: left;
width: 100%;
height: 100px;
background: orange;
}
.left{
float: left;
margin-left: -100%;
position: relative;
left: -100px;
width: 100px;
height: 100px;
background: red;
}
.right{
float: left;
margin-left: -100px;
position: relative;
right: -100px;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
Flex佈局
.container{
display: flex;
}
.center{
flex-grow: 1;
height: 100px;
background: orange;
}
.left{
order: -1; /* order屬性定義項目的排列順序 數值越小 排列越靠前 默認爲0 */
flex: 0 1 100px; /* flex-grow flex-shrink flex-basis */
height: 100px;
background: red;
}
.right{
flex: 0 1 100px;
height: 100px;
background: blue;
}
/*
flex-grow屬性定義項目的放大比例 默認爲0 即若是存在剩餘空間 也不放大
flex-shrink屬性定義了項目的縮小比例 默認爲1 即若是空間不足 該項目將縮小
flex-basis屬性定義了在分配多餘空間以前 項目佔據的主軸空間(main size)
瀏覽器根據這個屬性 計算主軸是否有多餘空間 它的默認值爲auto 即項目的原本大小
*/
<div class="container">
<div class="center"></div>
<div class="left"></div>
<div class="right"></div>
</div>
複製代碼
Table佈局
.container{
display: table; /* 此元素會做爲塊級表格來顯示(相似 <table>)表格先後帶有換行符 */
width: 100%;
}
.left,
.center,
.right{
display: table-cell; /* 此元素會做爲一個表格單元格顯示(相似 <td> 和 <th>) */
}
.left{
width: 100px;
height: 100px;
background: red;
}
.center{
background: orange;
}
.right{
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
複製代碼
絕對定位佈局
.container{
position: relative;
}
.center{
margin-left: 100px;
margin-right: 100px;
height: 100px;
background: orange;
}
.left{
position: absolute;
left: 0;
right: 0;
width: 100px;
height: 100px;
background: red;
}
.right{
position: absolute;
right: 0;
top: 0;
width: 100px;
height: 100px;
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
複製代碼
網格佈局(Grid佈局)
.container{
display: grid;
grid-template-columns: 100px auto 100px;
/*
用於設置網格容器的列屬性 其實就至關於列的寬度 當咱們須要幾列展現時
就設置幾個值 這個屬性能夠接收具體數值好比100px 也能夠接收百分比值
表示佔據容器的寬度
須要注意的是: 當給容器設定了寬度時
grid-template-columns設定的百分比值是以容器的寬度值爲基礎計算的
若是未設置寬度時 會一直向上追溯到設置了寬度的父容器 直到body元素。
*/
grid-template-rows: 100px;
/*
用於設置網格容器的行屬性 其實就至關於行的高度
其特性與grid-template-columns屬性相似
*/
}
.left{
background: red;
}
.center{
background :orange;
}
.right{
background: blue;
}
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
複製代碼