CSS布局問題

1.居中布局:

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

inline-block+text-align
<div class="parent">
     <div class="child">Demo</div>
 </div>
<style>
 .child { 
  display: inline-block;
  *display: inline;
  *zoom: 1;/**IE7中居中**/
 } 
.parent { text-align: center; }
</style>
兼容性佳(甚至可以兼容 IE 6 和 IE 7)

table+margin
<div class="parent"> 
  <div class="child">Demo</div>
</div>
<style> 
  .child { display: table; margin: 0 auto; }
</style>
display: table在表現(xiàn)上類似 block 元素,但是寬度為內(nèi)容寬。支持 IE 8 及其以上版本。

absolute + transform
<div class="parent"> 
  <div class="child">Demo</div>
</div>
<style>
   .parent { position: relative; }
   .child { position: absolute; left: 50%; transform: translateX(-50%); }
</style>
transform為 CSS3 屬性,有兼容性問題

flex + justify-content
<div class="parent">
 <div class="child">Demo</div>
</div>
<style> 
  .parent { display: flex; justify-content: center; }
   /* 或者下面的方法,可以達(dá)到一樣的效果 */
  .parent { display: flex; } 
  .child { margin: 0 auto; }
</style>
有兼容性問題

2.垂直居中

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

table-cell + vertical-align
<div class="parent"> 
  <div class="child">Demo</div>
</div>
<style> 
  .parent { display: table-cell; vertical-align: middle; }
</style>
兼容性好(支持 IE 8,以下版本需要調(diào)整頁面結(jié)構(gòu)至 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>
flex + align-items
<div class="parent"> 
  <div class="child">Demo</div>
</div>
<style> 
  .parent { display: flex; align-items: center; }
</style>
有兼容性問題

3.水平垂直居中

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

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>
兼容性好

absolute + transform
<div class="parent"> 
  <div class="child">Demo</div>
</div>
<style> 
  .parent { display: flex; justify-content: center; align-items: center; }
</style>

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>
有兼容性問題

4.多列布局

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

一列定寬,一列自適應(yīng)
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 中會(huì)有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:此方法不會(huì)存在 IE 6 中3像素的 BUG,但 .left不可選擇, 需要設(shè)置 .left {position: relative}來提高層級(jí)。 此方法可以適用于多版本瀏覽器(包括 IE6)。缺點(diǎn)是多余的 HTML 文本結(jié)構(gòu)。

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>
設(shè)置 overflow: hidden會(huì)觸發(fā) BFC 模式(Block Formatting Context)塊級(jí)格式化文本。 BFC 中的內(nèi)容與外界的元素是隔離的。
  • 不支持 IE 6

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 問答題47 /72 常見瀏覽器兼容性問題與解決方案? 參考答案 (1)瀏覽器兼容問題一:不同瀏覽器的標(biāo)簽?zāi)J(rèn)的外補(bǔ)...
    _Yfling閱讀 14,204評論 1 92
  • 前言 溫馨提示:本文較長,圖片較多,本來是想寫一篇 CSS 布局方式的,但是奈何 CSS 布局方式種類太多并且實(shí)現(xiàn)...
    sunshine小小倩閱讀 3,272評論 0 59
  • 水平居中布局 首先我們來看看水平居中 1.margin + 定寬 Demo.child{width:100px;m...
    xiaotao123閱讀 372評論 0 1
  • 至少我變成了更好的人了 不是嗎?
    SweetCC閱讀 207評論 0 0
  • 因工作需要年后再次去亳州出差,巧合的是今天情人節(jié),下午兩點(diǎn)二十五的火車。昨天去蚌埠陪父親做手術(shù),晚上匆匆忙忙...
    nike簡書閱讀 288評論 0 0

友情鏈接更多精彩內(nèi)容