在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ HTML/ 布局解決方案
書單
JavaScript 動畫
HTML
CSS Reset
屬性操作
DOM 事件
閉包
Photoshop
Atom 文本編輯器
表單操作
布局解決方案
類型系統(tǒng)
開發(fā)實踐
數(shù)據(jù)通信
變量作用域
BOM
JavaScript 程序設計
前端工程師概述
CSS
響應式布局
表達式與運算符
基本語法
JavaScript 介紹
版本控制
布局
調(diào)試器
背景
圖片保存
多媒體
文檔樹
列表操作
Sublime 編輯器
盒模型
常見布局樣例
類型識別
變形
數(shù)據(jù)存儲
選擇器
頁面架構
開發(fā)及調(diào)試工具
頁面模塊化
節(jié)點操作
測量及取色
瀏覽器兼容
HTML 簡介
內(nèi)置對象
實體字符
產(chǎn)品前端架構
協(xié)作流程
切圖
工具, 面板, 視圖
正則表達式
動畫
語句
面向對象
HTML 語法
HTML 標簽
技術選擇
樣式操作
圖片優(yōu)化與合并
語法
DOM 編程藝術
Canvas
接口設計
頁面優(yōu)化
文本

布局解決方案

了解 CSS 中屬性的值及其特性, 透徹分析問題和需求才可以選擇和設計最適合的布局解決方案。

居中布局

水平居中

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-center-horizontal.png" alt="" />

子元素于父元素水平居中且其(子元素與父元素)寬度均可變。

inline-block + text-align

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .child {
    display: inline-block;
  }
  .parent {
    text-align: center;
  }
</style>

優(yōu)點

  • 兼容性佳(甚至可以兼容 IE 6 和 IE 7)

table + margin

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .child {
    display: table;
    margin: 0 auto;
  }
</style>

NOTE: display: table 在表現(xiàn)上類似 block 元素,但是寬度為內(nèi)容寬。

優(yōu)點

  • 無需設置父元素樣式 (支持 IE 8 及其以上版本)

NOTE:兼容 IE 8 一下版本需要調(diào)整為 <table> 的結果

absolute + transform

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    position: relative;
  }
  .child {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }
</style>

優(yōu)點

  • 絕對定位脫離文檔流,不會對后續(xù)元素的布局造成影響。

缺點

  • transform 為 CSS3 屬性,有兼容性問題

flex + justify-content

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    display: flex;
    justify-content: center;
  }

  /* 或者下面的方法,可以達到一樣的效果 */

  .parent {
    display: flex;
  }
  .child {
    margin: 0 auto;
  }
</style>

優(yōu)點

  • 只需設置父節(jié)點屬性,無需設置子元素

缺點

  • 有兼容性問題

垂直居中

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-center-vertical.png" alt="" />

子元素于父元素垂直居中且其(子元素與父元素)高度均可變。

table-cell + vertical-align

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    display: table-cell;
    vertical-align: middle;
  }
</style>

優(yōu)點

  • 兼容性好(支持 IE 8,以下版本需要調(diào)整頁面結構至 table

absolute + transform

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    position: relative;
  }
  .child {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
  }
</style>

優(yōu)點

  • 絕對定位脫離文檔流,不會對后續(xù)元素的布局造成影響。但如果絕對定位元素是唯一的元素則父元素也會失去高度。

缺點

  • transform 為 CSS3 屬性,有兼容性問題

flex + align-items

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    display: flex;
    align-items: center;
  }
</style>

優(yōu)點

  • 只需設置父節(jié)點屬性,無需設置子元素

缺點

  • 有兼容性問題

水平與垂直居中

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-center-center.png" alt="" />

子元素于父元素垂直及水平居中且其(子元素與父元素)高度寬度均可變。

inline-block + text-align + table-cell + vertical-align

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    text-align: center;
    display: table-cell;
    vertical-align: middle;
  }
  .child {
    display: inline-block;
  }
</style>

優(yōu)點

  • 兼容性好

absolute + transform

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    position: relative;
  }
  .child {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
  }
</style>

優(yōu)點

  • 絕對定位脫離文檔流,不會對后續(xù)元素的布局造成影響。

缺點

  • transform 為 CSS3 屬性,有兼容性問題

flex + justify-content + align-items

<div class="parent">
  <div class="child">Demo</div>
</div>

<style>
  .parent {
    display: flex;
    justify-content: center;
    align-items: center;
  }
</style>

優(yōu)點

  • 只需設置父節(jié)點屬性,無需設置子元素

缺點

  • 有兼容性問題

多列布局

多列布局在網(wǎng)頁中非常常見(例如兩列布局),多列布局可以是兩列定寬,一列自適應, 或者多列不定寬一列自適應還有等分布局等。

一列定寬,一列自適應

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-0.jpg" alt="" />

float + margin

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .left {
    float: left;
    width: 100px;
  }
  .right {
    margin-left: 100px
    /*間距可再加入 margin-left */
  }
</style>

NOTE:IE 6 中會有3像素的 BUG,解決方法可以在 .left 加入 margin-left:-3px

float + margin + (fix) 改造版

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right-fix">
    <div class="right">
      <p>right</p>
      <p>right</p>
    </div>
  </div>
</div>

<style>
  .left {
    float: left;
    width: 100px;
  }
  .right-fix {
    float: right;
    width: 100%;
    margin-left: -100px;
  }
  .right {
    margin-left: 100px
    /*間距可再加入 margin-left */
  }
</style>

NOTE:此方法不會存在 IE 6 中3像素的 BUG,但 .left 不可選擇, 需要設置 .left {position: relative} 來提高層級。 此方法可以適用于多版本瀏覽器(包括 IE6)。缺點是多余的 HTML 文本結構。

float + overflow

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .left {
    float: left;
    width: 100px;
  }
  .right {
    overflow: hidden;
  }
</style>

設置 overflow: hidden 會觸發(fā) BFC 模式(Block Formatting Context)塊級格式化文本。 BFC 中的內(nèi)容與外界的元素是隔離的。

優(yōu)點

  • 樣式簡單

缺點

  • 不支持 IE 6

table

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: table;
    width: 100%;
    table-layout: fixed;
  }
  .left {
    display: table-cell;
    width: 100px;
  }
  .right {
    display: table-cell;
    /*寬度為剩余寬度*/
  }
</style>

table 的顯示特性為每列的單元格寬度合一定等與表格寬度。 table-layout: fixed; 可加速渲染,也是設定布局優(yōu)先。

NOTE:table-cell 中不可以設置 margin 但是可以通過 padding 來設置間距。

flex

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: flex;
  }
  .left {
    width: 100px;
    margin-left: 20px;
  }
  .right {
    flex: 1;
    /*等價于*/
    /*flex: 1 1 0;*/
  }
</style>

NOTE:flex-item 默認為內(nèi)容寬度。

缺點

  • 低版本瀏覽器兼容問題
  • 性能問題,只適合小范圍布局。

兩列定寬,一列自適應

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-1.png" alt="" />

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="center">
    <p>center<p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .left, .center {
    float: left;
    width: 100px;
    margin-right: 20px;
  }
  .right {
    overflow: hidden;
    /*等價于*/
    /*flex: 1 1 0;*/
  }
</style>

多列定寬的實現(xiàn)可以更具單列定寬的例子進行修改與實現(xiàn)。

一列不定寬加一列自適應

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-2.png" alt="" />

不定寬的寬度為內(nèi)容決定,下面為可以實現(xiàn)此效果的方法:

  • float + overflow,此方法在 IE6 中有兼容性問題
  • table,此方法在 IE6 中有兼容性問題
  • flex,此方法在 IE9及其以下版本中有兼容性問題

多列不定寬加一列自適應

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-1.png" alt="" />

其解決方案同一列不定寬加一列自適應相仿。

多列等分布局

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-4.png" alt="" />

每一列的寬度和間距均相等,下面為多列等分布局的布局特定。

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-5.png" alt="" />

父容器寬度為 C,C = W * N + G * N - G => C + G = (W + G) * N。

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-6.png" alt="" />

float

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-7.png" alt="" />

<div class="parent">
  <div class="column">
    <p>1</p>
  </div>
  <div class="column">
    <p>2</p>
  </div>
  <div class="column">
    <p>3</p>
  </div>
  <div class="column">
    <p>4</p>
  </div>
</div>
<style media="screen">
  .parent {
    margin-left: -20px;
  }
  .column {
    float: left;
    width: 25%;
    padding-left: 20px;
    box-sizing: border-box;
  }
</style>

NOTE:此方法可以完美兼容 IE8 以上版本。 NOTE+:此方法結構和樣式具有耦合性。

table

<div class='parent-fix'>
  <div class="parent">
    <div class="column">
      <p>1</p>
    </div>
    <div class="column">
      <p>2</p>
    </div>
    <div class="column">
      <p>3</p>
    </div>
    <div class="column">
      <p>4</p>
    </div>
  </div>
</div>

<style media="screen">
  .parent-fix {
    margin-left: -20px;
  }
  .parent {
    display: table;
    width: 100%;
    /*可以布局優(yōu)先,也可以單元格寬度平分在沒有設置的情況下*/
    table-layout: fixed;
  }
  .column {
    display: table-cell;
    padding-left: 20px;
  }
</style>

NOTE:缺點是多了文本結果

flex

<div class="parent">
  <div class="column">
    <p>1</p>
  </div>
  <div class="column">
    <p>2</p>
  </div>
  <div class="column">
    <p>3</p>
  </div>
  <div class="column">
    <p>4</p>
  </div>
</div>

<style media="screen">
  .parent {
    display: flex;
  }
  .column {
    /*等價于 flex: 1 1 0;*/
    flex: 1;
  }
  .column+.column {
    margin-left: 20px;
  }
</style>

NOTE:flex 的特性為分配剩余空間。 NOTE+:兼容性有問題。

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-multicolumn-8.png" alt="" />

table

table 的特性為每列等寬,每行等高可以用于解決此需求。

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: table;
    width: 100%;
    table-layout: fixed;
  }
  .left {
    display: table-cell;
    width: 100px;
  }
  .right {
    display: table-cell;
    /*寬度為剩余寬度*/
  }
</style>

flex

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    display: flex;
  }
  .left {
    width: 100px;
    margin-left: 20px;
  }
  .right {
    flex: 1;
    /*等價于*/
    /*flex: 1 1 0;*/
  }
</style>

NOTE:flex 默認的 align-items 的值為 stretch。

float

<div class="parent">
  <div class="left">
    <p>left</p>
  </div>
  <div class="right">
    <p>right</p>
    <p>right</p>
  </div>
</div>

<style>
  .parent {
    overflow: hidden;
  }
  .left,
  .right {
    padding-bottom: 9999px;
    margin-bottom: -9999px;
  }
  .left {
    float: left;
    width: 100px;
    margin-right: 20px;
  }
  .right {
    overflow: hidden;
  }
</style>

NOTE:此方法為偽等高(只有背景顯示高度相等),左右真實的高度其實不相等。 NOTE+:此方法兼容性較好。

全屏布局

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-full-screen.png" alt="" />

例如管理系統(tǒng),監(jiān)控與統(tǒng)計平臺均廣泛的使用全屏布局。

定寬需求

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-full-0.jpg" alt="" />

實現(xiàn)方案

  • Position 常規(guī)方案
  • Flex CSS3 新實現(xiàn)

Position

<div class="parent">
  <div class="top"></div>
  <div class="left"></div>
  <div class="right">
    /*輔助結構用于滾動*/
    <div class="inner"></div>
  </div>
  <div class="bottom"></div>
</div>
<style>
  html,
  body,
  .parent {
    height: 100%;
    /*用于隱藏滾動條*/
    overflo: hidden;
  }
  .top {
    /*相對于 body 定位*/
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 100px;
  }
  .left {
    position: absolute;
    left: 0;
    top: 100px;
    bottom: 50px;
    width: 200px;
  }
  .right {
    position: absolute;
    left: 200px;
    right: 0;
    top: 100px;
    bottom: 50px;
    overflow: auto;
  }
  .right .inner {
    /*此樣式為演示所有*/
    min-height: 1000px;
  }
  .bottom {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    height: 50px;
  }
</style>
Position 兼容

此方法不支持 IE6 可以使用下面的方法解決兼容問題。

<div class="g-hd"></div>
<div class="g-sd"></div>
<div class="g-mn"></div>
<div class="g-ft"></div>
<style>
  html,
  body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    margin: 0;
  }

  html {
    _height: auto;
    _padding: 100px 0 50px;
  }

  .g-hd,
  .g-sd,
  .g-mn,
  .g-ft {
    position: absolute;
    left: 0;
  }

  .g-hd,
  .g-ft {
    width: 100%;
  }

  .g-sd,
  .g-mn {
    top: 100px;
    bottom: 50px;
    _height: 100%;
    overflow: auto;
  }

  .g-hd {
    top: 0;
    height: 100px;
  }

  .g-sd {
    width: 300px;
  }

  .g-mn {
    _position: relative;
    left: 300px;
    right: 0;
    _top: 0;
    _left: 0;
    _margin-left: 300px;
  }

  .g-ft {
    bottom: 0;
    height: 50px;
  }
</style>

Flex

<div class="parent">
  <div class="top"></div>
  <div class="middle">
    <div class="left"></div>
    <div class="right">
      <div class="inner"></div>
    </div>
  </div>
  <div class="bottom"></div>
</div>
<style media="screen">
  html,
  body,
  parent {
    height: 100%;
    overflow: hidden;
  }

  .parent {
    display: flex;
    flex-direction: column;
  }

  .top {
    height: 100px;
  }

  .bottom {
    height: 50px;
  }

  .middle {
    // 居中自適應
    flex: 1;
    display: flex;
    /*flex-direction: row 為默認值*/
  }

  .left {
    width: 200px;
  }

  .right {
    flex: 1;
    overflow: auto;
  }
  .right .inner {
    min-height: 1000px;
  }
</style>
Flex 兼容性

CSS3 中的新概念所有 IE9 及其也行版本都不兼容。

百分比寬度需求

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-full-percentage.jpg" alt="" />

只需把定寬高(px 為單位的值)的實現(xiàn)改成百分比(%)既可。

內(nèi)容自適應

http://wiki.jikexueyuan.com/project/fend_note/images/L/layout-content-response.jpg" alt="" />

只有右側欄占據(jù)剩余位置,其余空間均需根據(jù)內(nèi)容改變。 所以 Postion 的定位方法不適合實現(xiàn)此方案。下面列出了兩種布局方案:

  • Flex
  • Grid,W3C 草案并不穩(wěn)定,瀏覽器支持也并不理想

Flex

只有不為寬高做出限制,既可對其中的內(nèi)容做出自適應的布局。

<div class="parent">
  <div class="top"></div>
  <div class="middle">
    <div class="left"></div>
    <div class="right">
      <div class="inner"></div>
    </div>
  </div>
  <div class="bottom"></div>
</div>

<style media="screen">
  html,
  body,
  parent {
    height: 100%;
    overflow: hidden;
  }

  .parent {
    display: flex;
    flex-direction: column;
  }

  .middle {
    // 居中自適應
    flex: 1;
    display: flex;
    /*flex-direction: row 為默認值*/
  }

  .right {
    flex: 1;
    overflow: auto;
  }
  .right .inner {
    min-height: 1000px;
  }
</style>

方案比較

方案 兼容性 性能 自適應
Position 部分自適應
Flex 較差 可自適應
Grid 較好 可自適應
上一篇:數(shù)據(jù)通信下一篇:圖片保存