<div class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
<div class="content">content</div>
</div>
複製代碼
.wrapper {
text-align: center;
color: #fff;
overflow: hidden;/* 這裏清除浮動,由於觸發了BFC */
line-height: 200px;
}
.left {
float: left;
width: 200px;
height: 200px;
background-color: red;
}
.right {
float: right;
width: 200px;
height: 200px;
background-color: blue;
}
.content {
height: 200px;
margin: 0 200px;
background-color: lime;
}
複製代碼
<div class="wrapper">
<div class="content">content</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
複製代碼
.wrapper {
width: 100%;
line-height: 200px;
color: #fffdef;
text-align: center;
position: relative;
}
.content {
margin: 0 200px;
background-color: lime;
height: 200px;
}
.left {
position: absolute;
top: 0;
left: 0;
width: 200px;
height: 200px;
background-color: red;
}
.right {
position: absolute;
top: 0;
right: 0;
width: 200px;
height: 200px;
background-color: blue;
}
複製代碼
<div class="wrapper">
<div class="left">left</div>
<div class="right">right</div>
<div class="content">content</div>
</div>
複製代碼
.wrapper {
text-align: center;
color: #fffdef;
width: 100%;
line-height: 200px;
font-size: 40px;
}
.left {
float: left;
width: 200px;
height: 200px;
background-color: red;
}
.right {
float: right;
width: 200px;
height: 200px;
background-color: blue;
}
.content {
height: 200px;
background-color: lime;
/* 這會造成BFC區域,不會與浮動的元素重疊 */
overflow: hidden;
}
複製代碼
效果圖以下:
overflow: hidden
,沒有了 margin: 0 200px
了呢。在這裏是這樣的,overflow: hidden
是會觸發BFC的,那麼此時content區域就是一個BFC區域了,那麼他不會被浮動的元素蓋住,因此,它的content的寬度就是從不被left蓋住的位置開始到被right蓋住的區域以前這段寬度,不用再像方法一那樣把margin給左右的值空出來這個區域,因此也造成了寬度自適應,搞定!不過它的缺點也顯而易見,和方法一的同樣:不能優先渲染content區域。<div class="main clearfix">
<div class="content">content</div>
<aside class="sidebar-left">left</aside>
<aside class="sidebar-right">right</aside>
</div>
複製代碼
.main {
padding: 0 200px 0 150px;
}
body {
color: #fff;
font-size: 40px;
background-color: #666;
font-family: Arial;
text-align: center;
}
.content,
.sidebar-left,
.sidebar-right {
float: left;
position: relative;
height: 400px;
line-height: 400px;
}
.content {
width: 100%;
background-color: #f5c531;
}
.sidebar-left {
width: 150px;
background-color: #a0c263;
margin-left: -100%;
left: -150px;
}
.sidebar-right {
background-color: #a0c263;
width: 200px;
margin-right: -200px;
/*margin-left: -200px;*/
/*right: -200px;*/
}
複製代碼
看下效果圖:
margin
負值。 咱們如今看代碼:
margin
的負值,結合浮動使用,那麼咱們知道浮動起來的幾個元素會拍成一行內,而寬度不夠纔會掉下去,這裏就是這種狀況,可是若是我給left塊一個margin-left
負值到一個界限,他就會回到上一行,由於它會向左走,左邊沒地方了,並且給的是margin
值,那麼他就會貼到上一行,由於他們是一塊兒浮動的,上一行一樣可以接納他(這裏我這樣想:好比說寬度足夠的時候,那麼我給它margin-left
負值,它不是同樣要往左走嗎?同樣的道理,它在下一行往左走到沒地方走的時候天然就回到了上一行了,由於他們是一塊兒浮動的),這裏我給了它margin-left: -100%
,因此它就到了這個位置
margin
的百分比的值是父級盒子寬度的百分比,給了-100%,天然向左走一個父級的寬度,就到了這,那麼這個位置不是咱們想要的,咱們要他在左邊的空出來的地方待着,那很簡單:left: -150px
搞定:
margin-right: -200px
搞定:
margin-right
一個負值(這個值大小要超過自身的寬度)他就會上去,繼續被接納,(這個值若是給的沒有自身寬度大,那麼就至關於不被接納,還要被排斥下來)因此正好排在了空缺的地方。 注意看我給right的代碼裏面最後兩句註釋上的代碼,若是用這兩句,能夠達到和上面一樣的效果,原理也相同:先給一個margin-left: -200px
(正好給200px的話就是正好貼在在父級盒子的最右邊,由於正好剛剛能上來,若是再多給點值還會再往左走,可是這裏不須要的,我只是在儘可能去把這個原理闡述明白),被上面接納,來到了它的父級盒子的貼着最右邊:
right: -200px
讓它向右走200px,搞定!這個其實就和我給left盒子的方法原理同樣。最終效果圖:
<div class="main clearfix">
<div class="content-wrapper">
<div class="content">content</div>
</div>
<aside class="sidebar-left">left</aside>
<aside class="sidebar-right">right</aside>
</div>
複製代碼
body {
color: #fff;
font-size: 40px;
font-family: Arial;
background-color: #666;
text-align: center;
}
.sidebar-left,
.sidebar-right {
float: left;
height: 400px;
line-height: 400px;
}
.content-wrapper {
width: 100%;
float: left;
}
.content {
margin: 0 200px 0 150px;
background-color: #f5c531;
height: 400px;
line-height: 400px;
}
.sidebar-left {
width: 150px;
background-color: #a0c263;
margin-left: -100%;
}
.sidebar-right {
background-color: #a0c263;
width: 200px;
margin-left: -200px;
}
複製代碼
老規矩,用圖說話:
margin-left: -100%
就能到想要的位置,爲何呢?由於要注意一點,在這裏,left父級盒子是寬度100%的,再也不是聖盃佈局裏面的留出來左右padding值的父級自適應寬度的盒子,這裏面使用content盒子的左右margin
值留出來的定寬,因此直接就能把left盒子定到想要的位置margin-left: -200px
正好貼到父級盒子最右邊,就能到想要的位置了。<div class="flex-box">
<div class="flex-content flex-item">我是內容</div>
<div class="flex-left flex-item">我是左邊欄</div>
<div class="flex-right flex-item">我是右邊欄</div>
</div>
複製代碼
body {
color: #fff;
font-size: 30px;
text-align: center;
}
.flex-box {
display: flex;
line-height: 600px;
}
.flex-left {
width: 200px;
height: 600px;
background-color: lime;
order: 0;
}
.flex-content {
order: 1;
width: 100%;
background-color: purple;
}
.flex-right {
width: 200px;
height: 600px;
background-color: blue;
order: 2;
}
複製代碼
效果圖就不貼了。。。太累了。這裏主要利用flex佈局中,給子元素添加的屬性order,這個order屬性意爲在主軸方向的排列中顯示的優先級,值越小,優先級越高,因此能夠達到HTML結構中content最早渲染,卻又能讓其在中間部分顯示的效果。css
就寫這6種三欄佈局的方式吧,都是很簡單的。默默地喝掉最後一口咖啡,繼續奮戰。html