css常见布局解决方案

水平居中布局

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>

  • display:table在表现上类似 block元素,但是宽度为内容宽。

  • 无需设置父元素样式 (支持 IE 8 及其以上版本)兼容 IE 8 一下版本需要调整为 <table>

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>

  • 宽度固定

  • 相比与使用 transform兼容性更好

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>

  • 绝对定位脱离文档流,不会对后续元素的布局造成影响

  • transform为CSS3属性,有兼容性问题

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>

  • 只需设置父节点属性,无需设置子元素

  • flex有兼容性问题

垂直居中

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>

  • 兼容性好(IE 8以下版本需要调整页面结构至 table)

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>

  • 绝对定位脱离文档流,不会对后续元素的布局造成影响,但如果绝对定位元素是唯一的元素,则父元素也会失去高度。

  • transform 为CSS3属性,有兼容性问题

同水平居中,这也可以使用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>

  • 绝对定位脱离文档流,不会对后续元素的布局造成影响

  • transform为CSS3属性,有兼容性问题

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>

  • 低版本浏览器兼容性问题

  • 性能问题,只是适合小范围布局

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


©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 14,718评论 1 92
  • CSS布局解决方案(终结版) 前端布局非常重要的一环就是页面框架的搭建,也是最基础的一环。在页面框架的搭建之中,又...
    杀个程序猿祭天阅读 3,701评论 0 2
  • 前端布局非常重要的一环就是页面框架的搭建,也是最基础的一环。在页面框架的搭建之中,又有居中布局、多列布局以及全局布...
    一个敲代码的前端妹子阅读 4,260评论 0 12
  • 1. 前言 前端圈有个“梗”:在面试时,问个css的position属性能刷掉一半人,其中不乏工作四五年的同学。在...
    YjWorld阅读 10,222评论 5 15
  • 又出来培训一周,每次培训前都是无限的期待,可以换一个环境放松一下心情,可以见识一下不同的城市文化。但每次提着拉杆箱...
    微习惯养成阅读 3,885评论 0 0

友情链接更多精彩内容