专栏名称: SegmentFault思否
SegmentFault (www.sf.gg)开发者社区,是中国年轻开发者喜爱的极客社区,我们为开发者提供最纯粹的技术交流和分享平台。
目录
相关文章推荐
程序员小灰  ·  部署DeepSeek ... ·  12 小时前  
程序员小灰  ·  小灰筹划6年的“仓鼠币系统”,终于上线了! ·  2 天前  
程序员的那些事  ·  北京大学出的第二份 DeepSeek ... ·  昨天  
OSC开源社区  ·  RAG市场的2024:随需而变,从狂热到理性 ·  2 天前  
码农翻身  ·  漫画 | 为什么大家都愿意进入外企? ·  2 天前  
51好读  ›  专栏  ›  SegmentFault思否

【整理】CSS 布局方案

SegmentFault思否  · 公众号  · 程序员  · 2017-09-08 08:00

正文

我们在日常开发中经常遇到布局问题,下面罗列几种常用的css布局方案。话不多说,上代码!

以下所有demo的源码:

github: https://github.com/zwwill/css-layout/tree/master/demo-1

链接: http://pan.baidu.com/s/1cHBH3g

密码:obkb

居中布局

以下居中布局均以不定宽为前提,定宽情况包含其中

1、水平居中

a) inline-block + text-align

  1. .parent{

  2.    text-align: center;

  3. }

  4. .child{

  5.    display: inline-block;

  6. }

tips:此方案兼容性较好,可兼容至IE8,对于IE567并不支持inline-block,需要使用css hack进行兼容

b) table + margin

  1. .child{

  2.    display: table;

  3.    margin: 0 auto;

  4. }

tips:此方案兼容至IE8,可以使用 代替css写法,兼容性良好

c) absolute + transform

  1. .parent{

  2.    position: relative;

  3.    height:1.5em;

  4. }

  5. .child{

  6.    position: absolute;

  7.    left: 50%;

  8.    transform: translateX(-50%);

  9. }

tips:此方案兼容至IE9,因为transform兼容性限制,如果 .child 为定宽元素,可以使用以下写法,兼容性极佳

  1. .parent{

  2.    position: relative;

  3.    height:1.5em;

  4. }

  5. .child{

  6.    position: absolute;

  7.    width:100px;

  8.    left: 50%;

  9.    margin-left:-50px;

  10. }

d) flex + justify-content

  1. .parent{

  2.    display: flex;

  3.    justify-content: center;

  4. }

  5. .child{

  6.    margin: 0 auto;

  7. }

tips:flex是一个强大的css,生而为布局,它可以轻松的满足各种居中、对其、平分的布局要求,但由于现浏览器兼容性问题,此方案很少被使用,但是值得期待浏览器兼容性良好但那一天!

2、垂直

a) table-cell + vertial-align

  1. .parent{

  2.    display: table-cell;

  3.    vertical-align: middle;

  4. }

tips:可替换成

布局,兼容性良好

b) absolute + transform

  1. .parent{

  2.    position: relative;

  3. }

  4. .child{

  5.    position: absolute;

  6.    top: 50%;

  7.    transform: translateY(-50%);

  8. }

tips:存在css3兼容问题,定宽兼容性良好

c) flex + align-items

  1. .parent{

  2.    display: flex;

  3.    align-items: center;

  4. }

tips:高版本浏览器兼容,低版本不适用

3、水平垂直

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

  1. .parent{

  2.    text-align: center;

  3.    display: table-cell;

  4.    vertical-align: middle;

  5. }

  6. .child{

  7.    display: inline-block;

  8. }

tips:兼容至IE8

b) absolute + transform

  1. .parent{

  2.    position: relative;

  3. }

  4. .child{

  5.    position: absolute;

  6.    left: 50%;

  7.    top: 50%;

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

  9. }

tips:兼容性稍差,兼容IE10以上

c) flex

  1. .parent{

  2.    display: flex;

  3.    justify-content: center;

  4.    align-items: center;

  5. }

tips:兼容差

多列布局

1、一列定宽,一列自适应

a) float + margin

  1. .left{

  2.    float: left;

  3.    width: 100px;

  4. }

  5. .right{

  6.    margin-left: 120px;

  7. }

tips:此方案对于定宽布局比较好,不定宽布局推荐方法b

b) float + overflow

  1. .left{

  2.    float: left;

  3.    width: 100px;

  4.    margin-right: 20px;

  5. }

  6. .right{

  7.    overflow: hidden;

  8. }

tips:个人常用写法,此方案不管是多列定宽或是不定宽,都可以完美实现,同时可以实现登高布局

c) table

  1. .parent{

  2.    display: table; width: 100%;

  3.    table-layout: fixed;

  4. }

  5. .left,.right{

  6.    display: table-cell;

  7. }

  8. .left{

  9.    width: 100px;

  10.    padding-right: 20px;

  11. }

d) flex

  1. .parent{

  2.    display: flex;

  3. }

  4. .left{

  5.    width: 100px;

  6.    padding-right: 20px;

  7. }

  8. .right{

  9.    flex: 1;

  10. }

2、多列定宽,一列自适应

a) float + overflow

  1. .left,.center{

  2.    float: left;

  3.    width: 100px;

  4.    margin-right: 20px;

  5. }

  6. .right{

  7.    overflow: hidden;

  8. }

b) table

  1. .parent{

  2.    display: table; width: 100%;

  3.    table-layout: fixed;

  4. }

  5. .left,.center,.right{

  6.    display: table-cell;

  7. }

  8. .right{

  9.    width: 100px;

  10.    padding-right: 20px;

  11. }

c) flex

  1. .parent{

  2.    display: flex;

  3. }

  4. .left,.center{

  5.    width: 100px;

  6.    padding-right: 20px;

  7. }

  8. .right{

  9.    flex: 1;

  10. }

3、一列不定宽,一列自适应

a) float + overflow

  1. .left{

  2.     float: left;

  3.    margin-right: 20px;

  4. }

  5. .right{

  6.    overflow: hidden;

  7. }

  8. .left p{width: 200px;}

b) table

  1. .parent{

  2.    display: table; width: 100%;

  3. }

  4. .left,.right{

  5.    display: table-cell;

  6. }

  7. .left{

  8.    width: 0.1%;

  9.    padding-right: 20px;

  10. }

  11. .left p{width:200px;}

c) flex

  1. .parent{

  2.    display: flex;

  3. }

  4. .left{

  5.    margin-right: 20px;

  6. }

  7. .right{

  8.    flex: 1;

  9. }

  10. .left p{width: 200px;}

4、多列不定宽,一列自适应

a) float + overflow

  1. .left,.center{

  2.    float: left;

  3.    margin-right: 20px;

  4. }

  5. .right{

  6.    overflow: hidden;

  7. }

  8. .left p,.center p{

  9.    width: 100px;

  10. }

5、等分

a) float + margin

  1. .parent{

  2.    margin-left: -20px;

  3. }

  4. .column{

  5.    float: left;

  6.    width: 25%;

  7.    padding-left: 20px;

  8.    box-sizing: border-box;

  9. }

b) table + margin

  1. .parent-fix{

  2.    margin-left: -







请到「今天看啥」查看全文