总结:前端开发中让元素居中的方法

注:前端开发中,让元素居中是必不可少的步骤,下面就来总结下让元素居中的方法。掌握了这些,可让咱们在开发中事半功倍。css

1、text-align:cener;

前提:元素没有浮动,并且是内联元素(如:span、img、b)。若是是块级元素,要把块级元素设置成display:inline-block;或display:inline;前端

2、margin:auto;

前提:元素必须是块级元素,若是是内联元素,必须设置为display:block;jquery

3、position:absolute;+定位(left:50%; top:50%;)+偏移(margin-left:-(w/2)px;margin-top:-(-h/2)px);

缺点:已知元素的宽、高,且需计算偏移值。css3

注意:偏移的方向是负方向。web

div {
    position: relative;
    width: 250px;
    height: 250px;
}

div img {
    width: 200px;
    height: 140px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -100px;
    margin-top: -70px;
}
复制代码

4、position:absolute;+定位(left:50%; top:50%;)+transform:translate(-50%,-50%);

说明:该方法的优势是不须要知道元素的宽度和高度,在移动端用的比较多,由于移动端对css3新属性的兼容性比较好。算法

div {
    position: relative;
    width: 250px;
    height: 250px;
}

div img {
    position: absolute;
    left: 50%;
    top: 50%;
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    transform: translate(-50%, -50%);
}
复制代码

5、position:absolute;+定位(四个方向)+margin:auo;

优势:不须要知道元素的宽、高,并且浏览器的兼容性好。浏览器

div {
    position: relative;
    width: 250px;
    height: 250px;
}

div img {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
}
复制代码

6、display:flex;+justify-content:center;+align-items:center;(弹性布局)

  • Flexbox布局最适合应用程序的组件和小规模布局,而 Gird 布局则适用于较大规模的布局。bash

  • 设为Flex布局之后,子元素的float、clear和vertical-align属性将失效函数

  • 使用flex居中不须要知道元素自己宽高以及元素的属性布局

div {
    width: 250px;
    height: 250px;
    display: flex;
    justify-content: center;/*水平居中*/
    align-items: center;/*垂直居中*/
}
复制代码

7、jquery方法实现(经常使用于弹框居中)

jquery实现水平和垂直居中的原理是经过jquery设置div的css,获取div的左,上的边距偏移量。 边距偏移量的算法就是用页面窗口的宽度减去该div的宽度,获得的值再除以2即左偏移量,右偏移量算法相同。 注意div的css设置要在resize()方法中完成,就是每次改变窗口大小是,都要执行设置div的css,代码以下:

$(function(){
          $(window).resize(function(){
            $('.mydiv').css({
              position:'absolute',
              left:($(window).width()-$('.mydiv').outerWidth())/2,
              top:($(window).height()-$('.mydiv').outerHeight())/2
            });
          });
      })
复制代码

此方法的好处就是不须要知道div 的具体宽度和高度,直接用jquery就能够实现水平和垂直居中,而且兼容各类浏览器。这个方法在不少的弹出层效果中应用。

8、css3新属性calc()和定位配合使用(须要知道元素的宽高)

  • 须要注意的是,运算符先后都须要保留一个空格,例如:width: calc(100% - 10px);

  • 任何长度值均可以使用calc()函数进行计算;

  • calc()函数支持 "+", "-", "*", "/" 运算;

  • calc()函数使用标准的数学运算优先级规则;

div {
    position: relative;
    width: 250px;
    height: 250px;
}

div img {
    width: 200px;
    height: 140px;
    position: absolute;
    left: calc(50% - 100px);
    top: calc(50% - 70px);
}
复制代码

9、display:table-cell;实现居中

配合使用display:table-cell和vertical-align、text-align,使父元素内的全部行内元素水平垂直居中(内部div设置display:inline-block便可)。这在子元素不肯定宽高和数量时,特别实用!

div {
    display: table-cell;
    width: 250px;
    height: 250px;
    text-align: center;
    vertical-align: middle;
    float: none;
}

div img {
    display: inline-block;
}
复制代码
  • table-cell不感知margin,在父元素上设置table-row等属性,也会使其不感知height。

  • 设置float或position会对默认布局形成破坏,能够考虑为之增长一个父div定义float等属性。


注:如您有更好的方法,欢迎留言评论,谢谢!

相关文章
相关标签/搜索