CSS多種布局方式自我實現(xiàn)-三欄布局(一)

一律胀、float實現(xiàn)三欄布局

1、HTML代碼:

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="第一個三欄布局" />
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="container">
    <div id="topbar">This is topbar</div>
    <div id="navbar">This is nav bar</div>
    <div id="main">
      <div id="column_left">This is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_left</div>
      <div id="column_right">This is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_right</div>
      <div id="column_right_adsense">This is column right adsense This is column right adsense This is column right adsense</div>
      <div id="clear">this is clear</div>
    </div>
    <div id="footer">This is footer</div>
  </div>
</body>
</html>

2貌矿、CSS代碼:

*{
  font-size:12px;
  padding: 0;
  margin:0;
}
body{
  background-color: #eee;
}
#container{

  background-color: #83A82B;

  width:500px;
  margin:50px auto;
}
#topbar{
  height:25px;
  background-color: #31DB20;
}
#column_left{
  float:left;
  width:250px;
  background-color: red;
}
#column_right{

  width:150px;
  background-color: #0f0;
  float:left;
}
#column_right_adsense{
  background-color: #00f;
  float:left;
  width:100px;
}
#clear{
  clear:both;
  background-color: pink;
}
#footer{
  background-color: piple;
}

3炭菌、結果展示:

三欄布局

4、注意事項

container逛漫、main不直接設置height,由內(nèi)容區(qū)撐起高度黑低,float后加clear元素清除浮動,撐起#main區(qū)高度。

5克握,以上實現(xiàn)方式的缺點

由于main寬度的固定蕾管,一旦某一欄添加了padding或margin,則會導致浮動欄下滑(浮動滑移),添加大圖片或是沒有空格的長字符串也都會打亂布局菩暗。

滑動漂移

6掰曾,滑動漂移的三種解決辦法

6.1 從設定的元素寬度中減去padding或margin的影響。

如:column_left增加padding:1px,則將其寬度減去2px停团,效果如下:

元素寬度減去影響值

?但是此方法操作起來比較麻煩旷坦,每次添加需要計算影響并修改代碼。

6.2 在元素內(nèi)部添加新div包裹元素內(nèi)容

設置padding和margin時應用于這個元素佑稠。部分代碼如下:
?HTML:
<div id="column_left"><div id="column_left_inner">This is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_left</div></div>
?CSS:
#column_left_inner{ padding:10px; margin:10px; }

元素內(nèi)部再嵌套div包裹內(nèi)容

?此方法較上一種方法更為簡單秒梅。

6.3 box-sizing:border-box

為容器內(nèi)各元素設置box-sizing:border-box,將盒模型改為IE盒模型,即盒子寬度=content area+padding+border;注意此方法無法解決margin造成的布局打亂舌胶。
?部分CSS代碼

#column_left{
  box-sizing:border-box;
  float:left;
  width:250px;
  background-color: red;
  padding:10px;
  border:3px solid black;
}
#column_right{
  box-sizing:border-box;
  width:150px;
  background-color: #0f0;
  float:left;
}
#column_right_adsense{
  box-sizing:border-box;
  background-color: #00f;
  float:left;
  width:100px;
}

效果圖:

box-sizing:border-box

?此方法最為簡單捆蜀,但是不能消除margin的設置引起的布局混亂。

二辆琅、flex三欄布局

flex布局適用于線型布局漱办,使用flex布局能直接有效的避免因padding或border等造成的布局混亂。使用方便婉烟,推薦使用此方法娩井。
HTML代碼:

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="flex實現(xiàn)三欄布局" />
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="container">
    <div id="topbar">This is topbar</div>
    <div id="navbar">This is nav bar</div>
    <div id="main">
      <div id="column_left">This is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis isdafsssssssssssssssssssssssssssssssssssssssssssssssssssss column_leftThis is column_leftThis is column_left</div>
      <div id="column_right">This is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_right</div>
      <div id="column_right_adsense">This is column right adsense This is column right adsense This is column right adsense</div>

    </div>
    <div id="footer">This is footer</div>
  </div>
</body>
</html>

CSS代碼:

*{
  font-size:12px;
  padding: 0;
  margin:0;
}
body{
  background-color: #eee;
}
#container{

  background-color: #83A82B;

  width:500px;
  margin:50px auto;
}
#topbar{
  height:25px;
  background-color: #31DB20;
}

#main{
  display:flex;
  flex-flow:row nowrap;
  
}
#column_left{
  background-color: #f00;
  flex-grow:1;
  width:150px;
  padding:10px;
}
#column_right{
  background-color: #0f0;
  flex-grow:1;
  width:150px;
}
#column_right_adsense{
  flex-grow:1;
  background-color: #00f;
  width:150px;
}


#footer{
  background-color: piple;
}

結果展示:

flex實現(xiàn)三欄布局

三、絕對定位實現(xiàn)三欄布局

1. HTML代碼

<!DOCTYPE html>
<html>
<head>
<meta name="description" content="flex實現(xiàn)三欄布局" />
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="container">
    <div id="topbar">This is topbar</div>
    <div id="navbar">This is nav bar</div>
    <div id="main">
      <div id="column_left">This is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_leftThis is column_left</div>
      <div id="column_center">This is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_rightThis is column_right</div>
      <div id="column_right">This is column right adsense This is column right adsense This is column right adsense</div>

    </div>    <!--main完-->
    <div id="footer">This is footer</div>
  </div>
</body>
</html>

CSS代碼:

*{
  font-size:12px;
  padding: 0;
  margin:0;
}
body{
  background-color: #eee;
}
#container{
  background-color: #83A82B;
  max-width:500px;
  margin:50px auto;
}
#topbar{
  height:25px;
  background-color: #31DB20;
}

#main{
  border:1px solid black;
  position:relative;
}
#column_left{
  background-color: #f00;
  position:absolute;
  top:0;
  left:0;
  width:150px;
height:100%;
}
#column_center{
  background-color: #0f0;
 margin:0 150px;

}
#column_right{
  background-color: #00f;
  position:absolute;
  top:0;
  right:0;
  width:150px;
  height:100%;
}

#footer{
  background-color: red;
  /* border:5px solid red; */
}

效果展示:

絕對定位實現(xiàn)的三欄布局
  1. CSS學習筆記(八) 頁面布局之三欄-固定寬度布局
  1. CSS 布局:40個教程似袁、技巧洞辣、例子和最佳實踐
  2. 我熟知的三種三欄網(wǎng)頁寬度自適應布局方法
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市昙衅,隨后出現(xiàn)的幾起案子扬霜,更是在濱河造成了極大的恐慌,老刑警劉巖而涉,帶你破解...
    沈念sama閱讀 222,252評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件著瓶,死亡現(xiàn)場離奇詭異,居然都是意外死亡啼县,警方通過查閱死者的電腦和手機材原,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,886評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來季眷,“玉大人余蟹,你說我怎么就攤上這事∽庸危” “怎么了威酒?”我有些...
    開封第一講書人閱讀 168,814評論 0 361
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我葵孤,道長担钮,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,869評論 1 299
  • 正文 為了忘掉前任佛呻,我火速辦了婚禮裳朋,結果婚禮上病线,老公的妹妹穿的比我還像新娘吓著。我一直安慰自己,他們只是感情好送挑,可當我...
    茶點故事閱讀 68,888評論 6 398
  • 文/花漫 我一把揭開白布绑莺。 她就那樣靜靜地躺著,像睡著了一般惕耕。 火紅的嫁衣襯著肌膚如雪纺裁。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,475評論 1 312
  • 那天司澎,我揣著相機與錄音欺缘,去河邊找鬼。 笑死挤安,一個胖子當著我的面吹牛谚殊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播蛤铜,決...
    沈念sama閱讀 41,010評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼嫩絮,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了围肥?” 一聲冷哼從身側響起剿干,我...
    開封第一講書人閱讀 39,924評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎穆刻,沒想到半個月后置尔,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,469評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡氢伟,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,552評論 3 342
  • 正文 我和宋清朗相戀三年榜轿,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片腐芍。...
    茶點故事閱讀 40,680評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡差导,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出猪勇,到底是詐尸還是另有隱情设褐,我是刑警寧澤,帶...
    沈念sama閱讀 36,362評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站助析,受9級特大地震影響犀被,放射性物質發(fā)生泄漏。R本人自食惡果不足惜外冀,卻給世界環(huán)境...
    茶點故事閱讀 42,037評論 3 335
  • 文/蒙蒙 一寡键、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧雪隧,春花似錦西轩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,519評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至庄拇,卻和暖如春注服,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背措近。 一陣腳步聲響...
    開封第一講書人閱讀 33,621評論 1 274
  • 我被黑心中介騙來泰國打工溶弟, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人瞭郑。 一個月前我還...
    沈念sama閱讀 49,099評論 3 378
  • 正文 我出身青樓辜御,卻偏偏與公主長得像,于是被迫代替她去往敵國和親凰浮。 傳聞我的和親對象是個殘疾皇子我抠,可洞房花燭夜當晚...
    茶點故事閱讀 45,691評論 2 361

推薦閱讀更多精彩內(nèi)容