css居中总结

前言

一直有个想法要把各类居中的方法总结一下,但仍是一直没有时间去整理。最近恰好在作样式重构的项目,顺便把一下本身有用过的或积累的居中方法给总结一下。

水平居中

  • 行内元素水平居中
  1. 行内元素的居中比较简单,直接使用text-align就能够达到居中效果
/* 行内元素 */
.parent4 {
    text-align: center;
}
  • 块级元素水平居中(块级水平居中方法列举如下几种)
  1. margin autocss

    这是最经常使用到的块级水平居中,利用margin:auto自动达到居中的效果,不过前提是子元素必须知道宽度css3

    • 缺点: 必须提早知道元素的尺寸
/* 必须设置子元素宽度 */
.child1 {
    width: 100px;
    height: 100px;
    margin: 0 auto; 
    background: aqua;
}
  1. 利用inline-block实现水平居中web

    • 缺点: 必须有个父元素对其设置text-align
.parent2 {
  
    text-align: center;

}

/* 必须经过父元素 */
.child2 {
    display: inline-block;
    /*width: 100px;
    height: 100px;*/
    background: aqua;
}
  1. 利用css3新增的width属性fit-content实现

    不少状况下咱们并不清楚一个元素的具体尺寸是多少,可是又要实现水平居中。这个时候咱们想起float,自动撑开宽高,可是惋惜的是float的脱离了文档流并不能用margin:auto去实现元素的水平居中。inline-block又必须有个父级对其进行设置居中。css3新增长了width里的属性实现了元素相似于float,inline-block的包裹效果,而且可使用margin: auto进行居中。fit-content会根据你的内容去包裹你的元素。在此处不细说明,该兴趣的小伙伴能够看看张鑫旭老师对这几个新增的属性的讲解wordpress

/* width的其余属性 */
 .parent3 {
    width: fit-content;
    margin: 10px auto;
    background: aquamarine;

}

垂直居中

  • 行内元素垂直居中
  1. line-height实现当行文字垂直居中
/* 行内元素,当行文字垂直居中 */
    .parent1 {
        height: 100px;
        line-height: 100px;
        background: wheat;
    }
  • 块级元素垂直居中(块级元素居中的方法比较多,总结以下)
  1. margin负边距实现性能

    该方法使用绝对定位利用margin负值将其居中,前提是须要 提早知道尺寸flex

    • 优势:兼容性不错
    • 缺点: 须要提早知道尺寸
.parent2 {
        position: relative;
        background: rosybrown;
        height: 100px;
    }
    .child2 {  
        background: blue;
        position: absolute;
        width: 80px;
        height: 40px;
        left: 50%;
        top: 50%;
        margin-top: -20px;
        margin-left: -40px;
    }
  1. 如何在不知道尺寸的状况下垂直居中呢,CSS3——translate属性的出现为咱们提供了可能。该方法利用translate以自身的宽高为基准来达到居中效果,至关于margin负值的做用,不过咱们不须要知道尺寸,translate帮咱们解决了。transform中translate偏移的百分比值是相对于自身大小的,code

    • 优势: 不须要提早知道尺寸
    • 缺点: 兼容性很差(在移动端上基本支持)
/* 块级元素: 绝对定位 + transform  优势: 不须要提早知道尺寸
缺点: 兼容性很差*/
.parent3 {
    position: relative;
    background: antiquewhite;
    height: 200px;
}
.child3 {
    background: salmon;
    position: absolute;
    width: 80px;
    height: 40px;
    left: 50%;
    top: 50%;
    transform: translate(-50%,-50%);
}
  1. 块级元素:绝对定位 + margin: auto;

    结合以上两种,在介绍一个利用绝对定位的一个很好用的方法
    这是从张鑫旭老师的博客搬运过来的详情戳这里orm

    优势:不须要根据宽高去作相应的位移,自动帮你居中好了,兼容性好文档

/* 块级元素:绝对定位 + margin: auto; 优势:不须要根据宽高去作相应的位移,兼容性好 */
.parent4 {
    position: relative;
    background: wheat;
    height: 200px;
}
.child4 {
    width: 80px;
    height: 40px;
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    background: blue;
}
  1. 利用display: table-cell实现垂直居中

    display的table和table-cell通常状况下用的很少,因此不多有人去关注它。这个实现的原理就是把其变成表格样式,再利用表格的样式来进行居中,在某些状况下仍是很方便的。get

/* 块级元素:display: table-cell */
.parent5 {
    width: 600px;
    height: 200px;
    border: 1px solid red;
    display: table;
}
.child5 {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
 
/* 水平垂直居中 */
.parent7 {
    width: 400px;
    height: 300px;
    display: table-cell;
    vertical-align: middle;
    border: 1px solid red;
}
.child7 {
    display: inline-block;
    vertical-align: middle;
    background: blue;
}
  1. 利用calc()计算属性

    缺点: 兼容性差,须要计算,消耗性能,须要提早知道尺寸

.parent8 {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    position: relative;
}
.child8 {
    top:-webkit-calc(50%-50px);
    top:-moz-calc(50%-50px);
    top:calc(50%-50px);
    left:-webkit-calc(50%-50px);
    left:-moz-calc(50%-50px);
    left:calc(50%-50px);
    width: 100px;
    height: 100px;
    background: blue;
    
}
  1. 利用伪元素实现居中(这个原理我还没搞懂,可是实践过真的ok)
.parent9 {
    width: 300px;
    height: 300px;
    border: 1px solid red;
    text-align: center;
}
.child9 {
    background: blue;
    width: 100px;
    height: 40px;
    display: inline-block;
    vertical-align: middle;
}
.parent9::before {
    content: '';
    height: 100%;
    display: inline-block;
    vertical-align: middle;
}
  1. 块级元素:display: flex

    缺点:在pc上兼容性很差

.parent10 {
    width: 600px;
    height: 200px;
    border: 1px solid red;
    display: flex;
    align-items: center;  /*垂直居中*/
    justify-content: center;  /*水平居中*/
}
.child10 {
    background: blue;
}

总结

以上是分别总结了水平居中和垂直居中经常使用的方法,要想实现水平垂直居中能够本身组合搭配一下。方法目前总结了这几种,以后有新的方法也会持续更新,未完待续连载中....
相关文章
相关标签/搜索