css常见布局解决方案

2018-09-09  本文已影响0人  Imrobin

水平居中布局

margin+定宽

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .child {

  6.    width:100px;

  7.    margin:0auto;

  8.  }

  9. </style>

table+margin

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .child {

  6.    display: table;

  7.    margin:0auto;

  8.  }

  9. </style>

inline-block+text-align

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .child {

  6.    display:inline-block;

  7.  }

  8.  .parent {

  9.    text-align: center;

  10.  }

  11. </style>

兼容性佳(甚至可以兼容IE6和IE7)

absolute+margin-left

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5. .parent {

  6.    position: relative;

  7.  }

  8.  .child {

  9.    position: absolute;

  10.    left:50%;

  11.    width:100px;

  12.    margin-left:-50px;  /* width/2 */

  13.  }

  14. </style>

absolute+transform

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    position: relative;

  7.  }

  8.  .child {

  9.    position: absolute;

  10.    left:50%;

  11.    transform: translateX(-50%);

  12.  }

  13. </style>

flex+justify-content

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    display: flex;

  7.    justify-content: center;

  8.  }

  9. </style>

垂直居中

table-cell+vertical-align

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    display: table-cell;

  7.    vertical-align: middle;

  8.  }

  9. </style>

absolute+transform

强大的absolute对于这种小问题当然是很简单的

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    position: relative;

  7.  }

  8.  .child {

  9.    position: absolute;

  10.    top:50%;

  11.    transform: translateY(-50%);

  12.  }

  13. </style>

同水平居中,这也可以使用margin-top实现,原理水平居中

flex+align-items

如果说 absolute强大,那 flex只是笑笑,因为他才是最强的,但有兼容性问题

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    display: flex;

  7.    align-items: center;

  8.  }

  9. </style>

水平垂直居中

absolute+transform

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    position: relative;

  7.  }

  8.  .child {

  9.    position: absolute;

  10.    left:50%;

  11.    top:50%;

  12.    transform: translate(-50%,-50%);

  13.  }

  14. </style>

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

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    text-align: center;

  7.    display: table-cell;

  8.    vertical-align: middle;

  9.  }

  10.  .child {

  11.    display:inline-block;

  12.  }

  13. </style>

flex+justify-content+align-items

  1. <divclass="parent">

  2.  <divclass="child">Demo</div>

  3. </div>

  4. <style>

  5.  .parent {

  6.    display: flex;

  7.    justify-content: center;/* 水平居中 */

  8.    align-items: center;/*垂直居中*/

  9.  }

  10. </style>

一列定宽,一列自适应

这种布局最常见的就是中后台类型的项目,如下图:

float+margin

  1. <divclass="parent">

  2.  <divclass="left">

  3.    <p>left</p>

  4.  </div>

  5.  <divclass="right">

  6.    <p>right</p>

  7.    <p>right</p>

  8.  </div>

  9. </div>

  10. <style>

  11.  .left {

  12.    float: left;

  13.    width:100px;

  14.  }

  15.  .right {

  16.    margin-left:100px

  17.    /*间距可再加入 margin-left */

  18.  }

  19. </style>

IE6中会有3px的BUG,解决方法可以在 .left加入 margin-left:-3px当然下面的方案也可以解决这个bug:

  1. <divclass="parent">

  2.  <divclass="left">

  3.    <p>left</p>

  4.  </div>

  5.  <divclass="right-fix">

  6.    <divclass="right">

  7.      <p>right</p>

  8.      <p>right</p>

  9.    </div>

  10.  </div>

  11. </div>

  12. <style>

  13.  .left {

  14.    float: left;

  15.    width:100px;

  16.  }

  17.  .right-fix {

  18.    float: right;

  19.    width:100%;

  20.    margin-left:-100px;

  21.  }

  22.  .right {

  23.    margin-left:100px

  24.    /*间距可再加入 margin-left */

  25.  }

  26. </style>

float+overflow

  1. <divclass="parent">

  2.  <divclass="left">

  3.    <p>left</p>

  4.  </div>

  5.  <divclass="right">

  6.    <p>right</p>

  7.    <p>right</p>

  8.  </div>

  9. </div>

  10. <style>

  11.  .left {

  12.    float: left;

  13.    width:100px;

  14.  }

  15.  .right {

  16.    overflow: hidden;

  17.  }

  18. </style>

设置overflow:hidden会出发BFC模式(block formatting context)块级格式上下文。BFC是什么呢?用通俗的江就是,随便你在BFC里面干什么,外面都不会手段哦影响。此方法样式简单但不支持 IE 6

table

  1. <divclass="parent">

  2.  <divclass="left">

  3.    <p>left</p>

  4.  </div>

  5.  <divclass="right">

  6.    <p>right</p>

  7.    <p>right</p>

  8.  </div>

  9. </div>

  10. <style>

  11.  .parent {

  12.    display: table;

  13.    width:100%;

  14.    table-layout:fixed;

  15.  }

  16.  .left {

  17.    display: table-cell;

  18.    width:100px;

  19.  }

  20.  .right {

  21.    display: table-cell;

  22.    /*宽度为剩余宽度*/

  23.  }

  24. </style>

table 的显示特性为每列的单元格宽度和一定等与表格宽度。 table-layout:fixed 可加速渲染,也是设定布局优先。 table-cell 中不可以设置 margin但是可以通过 padding 来设置间距

flex

  1. <divclass="parent">

  2.  <divclass="left">

  3.    <p>left</p>

  4.  </div>

  5.  <divclass="right">

  6.    <p>right</p>

  7.    <p>right</p>

  8.  </div>

  9. </div>

  10. <style>

  11.  .parent {

  12.    display: flex;

  13.  }

  14.  .left {

  15.    width:100px;

  16.    margin-left:20px;

  17.  }

  18.  .right {

  19.    flex:1;

  20.  }

  21. </style>

以上就是常见的几种布局。

上一篇 下一篇

猜你喜欢

热点阅读