CSS布局

課程內(nèi)容

什么是布局
DIV + CSS宰掉?
固定寬度 VS 彈性布局
單列布局
雙列布局
三列布局

什么是布局

現(xiàn)有樣式不能滿足人們的需求

文檔流
浮動
定位

人們需要:

導(dǎo)航欄+內(nèi)容
導(dǎo)航欄+內(nèi)容+廣告欄
從上到下慌烧、從左到右、定寬、自適應(yīng)...
CSS 2 并沒有提供原生支持驶睦,所以需要將一些屬性組合起來晨炕,以實現(xiàn)布局

“DIV + CSS 布局”?

中國特色歇由,國外一般不這么叫 <div> 是一個無語義的標(biāo)簽仅孩,適合用來做與內(nèi)容無關(guān)的事情 只能用 <div> 嗎?

不一定
盡量使用有語義的標(biāo)簽

常見布局(PC)

固定寬度布局:比如京東首頁印蓖,一般pc端都是
優(yōu)點:簡單辽慕,寬度可以寫死,不管頁面怎么變赦肃,整體的樣式不變
缺點:假如瀏覽器的寬度小于內(nèi)容時溅蛉,會出現(xiàn)滾動條
彈性(fluid)布局:內(nèi)容并不是固定寬度,和頁面的整體寬度有關(guān)他宛,需要用百分比做適配船侧,
缺點:在設(shè)計和實現(xiàn)上都要復(fù)雜些,需要考慮屏幕的不同情況厅各,
優(yōu)點:設(shè)計的好的話镜撩,頁面會很好看。
響應(yīng)式布局 —— 多終端(PC队塘、Pad袁梗、Phone)

如何實現(xiàn)
定寬

width: 1000px; 或 max-width: 1000px;

width:1000px:當(dāng)小于1000的時候有滾動條
max-width: 1000px;當(dāng)小于1000的時候沒有滾動條

水平居中

margin-left: auto; margin-right: auto;

范例:單列布局

范例 注意 max-width和width的區(qū)別

<style>
  .layout{
  /*   width: 960px; */
    max-width: 960px;
    margin: 0 auto;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div class="layout">
  <div id="header">頭部</div>
  <div id="content">內(nèi)容</div>
  <div id="footer">尾部</div>
</div>

進化

省標(biāo)簽,便于控制局部 范例

<style>
  .layout{
    width: 960px;
    margin: 0 auto;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div id="header"  class="layout">頭部</div>
<div id="content" class="layout">內(nèi)容</div>
<div id="footer" class="layout">尾部</div>

通欄

給通欄加背景色 范例

<style>
  .layout{
    width: 960px;
    margin: 0 auto;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div id="header">
  <div class="layout">頭部</div>
</div>
<div id="content" class="layout">內(nèi)容</div>
<div id="footer">
  <div class="layout">尾部</div>
</div>

通欄優(yōu)化

給 body 設(shè)置min-width 去掉滾動背景色 bug

<style>
  .layout{
    width: 960px;
    margin: 0 auto;
  }
  body{
    min-width: 960px;
  }
  #header{
    height: 60px;
    background: red;
  }
  #content{
    height: 400px;
    background: blue;
  }
  #footer{
    height: 50px;
    background: yellow;
  }
</style>
<div id="header">
  <div class="layout">頭部</div>
</div>
<div id="content" class="layout">內(nèi)容</div>
<div id="footer">
  <div class="layout">尾部</div>
</div>

內(nèi)部元素水平居中

.parent{text-align:center;}
.child{display: inline-block;}
IE 6 不支持 inline-block (為什么用下面的寫法)

.child{*display: inline; *zoom: 1;}

http://js.jirengu.com/yaluqavufi/1/edit

如何實現(xiàn)

浮動元素 + 普通元素margin 范例

  <style>
    #content:after{
      content: '';
      display: block;
      clear: both;
    }
    .aside{
      width: 200px;
      height: 500px;
      background: yellow;
      float: left;
    }
    .main{
      margin-left: 210px;
      height: 400px;
      background: red;
    }
    
    #footer{
      background: #ccc;
    }
  
  </style>
  <div id="content">
    <div class="aside">aside</div>
    <div class="main">content</div>
  </div>
  <div id="footer">footer</div>

如果側(cè)邊欄在右邊呢憔古?

側(cè)邊欄在右

謹記頁面元素的渲染順序 范例

:瀏覽器先讀content遮怜,樣式,然后再讀aside鸿市,樣式锯梁,再讀content
  <style>
    #content:after{
      content: '';
      display: block;
      clear: both;
    }
    .aside{
      width: 200px;
      height: 500px;
      background: yellow;
      float: right;
    }
    .main{
      margin-right: 210px;
      height: 400px;
      background: red;
    }
    
    #footer{
      background: #ccc;
    }
  
  </style>

  <div id="content">
    <div class="aside">aside</div>
    <div class="main">content</div>
  </div>
  <div id="footer">footer</div>

三列布局

兩側(cè)兩列固定寬度,中間列自適應(yīng)寬度
如何實現(xiàn)
范例

    #content:after{
      content: '';
      display: block;
      clear: both;
    }
    .menu{
      width: 100px;
      height: 500px;
      background: pink;
      float: left;
    }
    .aside{
      width: 200px;
      height: 500px;
      background: yellow;
      float: right;
    }
    .main{
      margin-left: 110px; /*為什么要加margin-left*/
      margin-right: 210px;
      height: 400px;
      background: red;
    }
    
    #footer{
      background: #ccc;
    }

  <div id="content">
    <!-- 為什么不是main在前面 -->:
    <div class="menu">aside</div>
    <div class="aside">aside</div>
    <div class="main">content</div>
  </div>
  <div id="footer">footer</div>

http://js.jirengu.com/jige/edit?html,output

浮動 vs 負margin
水平等距排列
圣杯布局
雙飛翼布局

浮動 vs 負margin

對于相鄰的兩個浮動元素焰情,如果因為空間不夠?qū)е碌诙€浮動元素下移陌凳,可以通過給第二個浮動元素設(shè)置
 margin-left: 負值 來讓第二個元素上移,其中 負值 大于等于元素上移后和第一個元素重合的臨界值

三個浮動元素

最后一個浮動元素使用了負邊距

使用了負邊距

范例演示

范例 想想最后一個元素為什么要設(shè)置為 -20px?

.float{
    overflow:hidden;
    width:280px;
    border:dashed 1px orange;
}

.float .item{
    width:100px;
    height:100px;
    float:left;
}

.float .item:nth-child(1){
    background:red;
}
.float .item:nth-child(2){
    background:grey;
}
.float .item:nth-child(3){
    background:blue;
    margin-left: -20px;  /* 為什么這里是 -20px ??*/
}
</style>

<div class="float">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

水平等距排列

范例

<style>
ul,li{
  margin: 0;
  padding: 0;
  list-style: none;
}
.ct{
    overflow:hidden;
    width: 640px;
    border:dashed 1px orange;
    margin: 0 auto;
}

.ct .item{
    float:left;
    margin-left: 20px;
    margin-top: 20px;
    width:200px;
    height:200px;
    background: red;
}
.ct>ul{
  margin-left: -20px;
}

</style>
<div class="ct">
  <ul>
    <li class="item">1</li>
    <li class="item">2</li>
    <li class="item">3</li>
    <li class="item">4</li>
    <li class="item">5</li>
    <li class="item">6</li>
    <li class="item">7</li>
    <li class="item">8</li>
  </ul>
</div>

http://js.jirengu.com/xute/edit?html,output

圣杯布局

why it?

是三列布局内舟,兩邊固定寬度合敦,中間自適應(yīng)
中間內(nèi)容元素在 dom 元素次序中優(yōu)先位置

實現(xiàn)

按照注釋編號,一行行實現(xiàn)觀察效果 范例

  <style>
    #content:after{
      content: '';        /*8*/
      display: block;     /*8*/
      clear: both;        /*8*/
    }
    #content{
      padding-left: 100px;  /*5*/
      padding-right: 150px; /*5*/
    }
    .aside, .main, .extra{
      float: left;         /*2*/
    }
    
    .aside{
      width: 100px;        /*1*/
      height: 300px;       /*1*/
      background: red;     /*1*/
      
      margin-left: -100%;  /*4*/
      position: relative;  /*6*/
      left: -100px;        /*6*/
    }
    .extra{
      width: 150px;        /*1*/
      height: 300px;       /*1*/
      background: yellow;  /*1*/
      
      margin-left: -150px; /*5*/
      position: relative;  /*7*/
      left: 150px;         /*7*/
      
    }
    .main{
      height: 350px;       /*1*/
      background: blue;    /*1*/
      
      width: 100%;         /*3*/
    }
  
  </style>

  <div id="content">
    <div class="main">main</div>
    <div class="aside">aside</div>
    <div class="extra">extra</div>
  </div>

http://js.jirengu.com/poya/1/edit?html,output

缺點

.mian的最小寬度不能小于.aside的寬度

why?:當(dāng)小于的時候會發(fā)生錯亂谒获,.main被擠下去

雙飛翼布局

按照注釋編號蛤肌,一行行實現(xiàn)觀察效果 范例

解決了什么問題?

  <style>
    
    #content:after{
      content: '';        /*8*/
      display: block;     /*8*/
      clear: both;        /*8*/
    }
    #content{
  
    }
    .aside, .main, .extra{
      float: left;         /*2*/
    }
    
    .aside{
      width: 100px;        /*1*/
      height: 300px;       /*1*/
      background: red;     /*1*/
      
      margin-left: -100%;  /*4*/

    }
    .extra{
      width: 150px;        /*1*/
      height: 300px;       /*1*/
      background: yellow;  /*1*/
      
      margin-left: -150px; /*5*/

      
    }
    .main{
      /* background: blue;  */   /*第1步添加壁却,第7步注釋掉*/
      /* height: 350px;  */      /*第1步添加,第7步注釋掉*/
      
      width: 100%;         /*3*/
    }
    
    .wrap{
      margin-left: 100px;  /*6*/
      margin-right: 150px; /*6*/
      background: blue;    /*7*/
      height: 350px;       /*7*/
 
    }
  
  </style>
  
  <div id="content">
    <div class="main">
      <div class="wrap">main</div>
    </div>
    <div class="aside">aside</div>
    <div class="extra">extra</div>
  </div>

http://js.jirengu.com/casozaraxo/1/edit

一.實現(xiàn)一個單欄布局

代碼一

二.實現(xiàn)一個三欄布局

代碼二

三.實現(xiàn)圣杯布局

代碼三

四.實現(xiàn)雙飛翼布局

代碼四

代碼五

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末裸准,一起剝皮案震驚了整個濱河市展东,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌炒俱,老刑警劉巖盐肃,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異权悟,居然都是意外死亡砸王,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門峦阁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谦铃,“玉大人,你說我怎么就攤上這事榔昔【匀颍” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵撒会,是天一觀的道長嘹朗。 經(jīng)常有香客問我,道長诵肛,這世上最難降的妖魔是什么屹培? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮怔檩,結(jié)果婚禮上褪秀,老公的妹妹穿的比我還像新娘。我一直安慰自己珠洗,他們只是感情好溜歪,可當(dāng)我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布若专。 她就那樣靜靜地躺著许蓖,像睡著了一般。 火紅的嫁衣襯著肌膚如雪调衰。 梳的紋絲不亂的頭發(fā)上膊爪,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天,我揣著相機與錄音嚎莉,去河邊找鬼米酬。 笑死,一個胖子當(dāng)著我的面吹牛趋箩,可吹牛的內(nèi)容都是我干的赃额。 我是一名探鬼主播加派,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼跳芳!你這毒婦竟也來了芍锦?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤飞盆,失蹤者是張志新(化名)和其女友劉穎娄琉,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體吓歇,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡孽水,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了城看。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片女气。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖测柠,靈堂內(nèi)的尸體忽然破棺而出主卫,到底是詐尸還是另有隱情,我是刑警寧澤鹃愤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布簇搅,位于F島的核電站,受9級特大地震影響软吐,放射性物質(zhì)發(fā)生泄漏瘩将。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一凹耙、第九天 我趴在偏房一處隱蔽的房頂上張望姿现。 院中可真熱鬧,春花似錦肖抱、人聲如沸备典。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽提佣。三九已至,卻和暖如春荤崇,著一層夾襖步出監(jiān)牢的瞬間拌屏,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工术荤, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留倚喂,地道東北人。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓瓣戚,卻偏偏與公主長得像端圈,于是被迫代替她去往敵國和親焦读。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,066評論 2 355

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

  • 前言 現(xiàn)在舱权,我們被稱為前端工程師吨灭。然而,早年給我們的稱呼卻是頁面仔刑巧⌒郑或許是職責(zé)越來越大,整體的前端井噴式的發(fā)展啊楚,使...
    Calvin李閱讀 497評論 0 0
  • CSS布局 布局是CSS中一個重要部分吠冤,本文總結(jié)了CSS布局中的常用技巧,包括常用的水平居中恭理、垂直居中方法拯辙,以及單...
    web前端學(xué)習(xí)閱讀 1,597評論 1 38
  • 目錄 常用居中垂直居中水平居中垂直水平居中 單列布局 雙列&三列布局 常用居中 垂直居中 單行文本垂直居中 圖片垂...
    聽城閱讀 435評論 1 2
  • 定寬+水平居中實現(xiàn)單列布局 定寬+水平居中實現(xiàn)單列布局(通欄) 代碼:http://js.jirengu.com/...
    莊海鑫閱讀 532評論 0 3
  • 為什么我們覺得堅持很難? 可能是因為低估了這件事的難度颜价, 高估了自己的能力涯保。 I could't agree mo...
    初心如是閱讀 133評論 0 0