div水平垂直居中方法及优缺点

 

代码:

<div class="father">
<div class="son">
</div>
</div>

 

方案一:

div绝对定位水平垂直居中【margin:auto实现绝对定位元素的居中】,

兼容性:,IE7及之前版本不支持

 
    .father{
        width:400px;
        height:400px;
              background: red;
        position:relative;  /* 或者position:absolute;*/
    }
    .son{
            width: 200px;
            height: 200px;
            background: green;
            position:absolute;
            left:0;
            top: 0;
            bottom: 0;
            right: 0;
            margin: auto;
        }

 

优点:

  • 简单

缺点:

  • IE(IE8 beta)中无效
  • 无足够空间时,.son 被截断,但是不会有滚动条出现

 

方案二:

div绝对定位水平垂直居中【margin 负间距】     这或许是当前最流行的使用方法

    .father{
        width:400px;
        height:400px;
              background: red;
        position:relative;  /* 或者position:absolute;*/
    }
         .son{
            width:200px;
            height: 200px;
            background:green;
            position: absolute;
            left:50%;
            top:50%;
            margin-left:-100px;
            margin-top:-100px;
        }        

 

优点:

  • 适用于所有浏览器
  • 不需要嵌套标签

缺点:

  • 没有足够空间时,.son会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)

 

方案三:

div绝对定位水平垂直居中【Transforms 变形】

兼容性:IE8不支持;

    .father{
        width:400px;
        height:400px;
              background: red;
        position:relative;  /* 或者position:absolute;*/
    }

.son{ width: 200px; height: 200px; background: green; position:absolute; left:50%; /* 定位父级的50% */ top:50%; transform: translate(-50%,-50%); /*自己的50% */ }

 

 不定宽高的的水平垂直居中

方案四:

css不定宽高水平垂直居中,CSS3属性

     .father{
        width:?px;
        height:?px;
              background: red;
       display:flex;
            justify-content:center;
            align-items:center;
               /* aa只要三句话就可以实现不定宽高水平垂直居中。 */
        }
        .son{
            background: green;
            width: ?px;
            height: ?px;
        }

 

 

 

方案五:

将父盒子设置为table-cell元素,可以使用text-align:center和vertical-align:middle实现水平、垂直居中。比较完美的解决方案是利用三层结构模拟父子结构

     .father{
        width:?px;
        height:?px;
              background: red;

        display: table-cell;
        vertical-align: middle;

        }
        .son{
            background: green;
            width: ?px;
            height: ?px;
       margin: auto; }

或者
     .father{
        width:?px;
        height:?px;
              background: red;

        display: table-cell;
        vertical-align: middle;

        text-align:center;

        }
        .son{
            background: green;
            width: ?px;
            height: ?px;
       display:inline-block; }

 

优点:

.son 可以动态改变高度(不需在 CSS 中定义),.son 不会被截断

缺点:

Internet Explorer(甚至 IE8 beta)中无效,许多嵌套标签

 

方案六:

对子盒子实现绝对定位,利用calc计算位置

  .box {
    position: relative;
   }

  .div {
    width: 200px;
    height: 200px;
    background: green;
    position: absolute;
    left: -webkit-calc((400px - 200px)/2);
    top: -webkit-calc((400px - 200px)/2);
    left: -moz-calc((400px - 200px)/2);
    top: -moz-calc((400px - 200px)/2);
    left: calc((400px - 200px)/2);
    top: calc((400px - 200px)/2);
  }

 

posted @ 2017-12-25 16:12  大城小梦  阅读(857)  评论(0编辑  收藏  举报