• 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{
                 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{
                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{ 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;
                 ?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;
                 ?px;
                height: ?px;
           margin: auto; }

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

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

            text-align:center;

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

    优点:

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

    缺点:

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

    方案六:

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

      .box {
        position: relative;
       }

      .div {
         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);
      }

  • 相关阅读:
    STM32 + RT Thread OS 学习笔记[三]
    全代码实现ios-1
    HTML5 Web Speech API 结合Ext实现浏览器语音识别以及输入
    全代码实现ios-2
    从零开始学C++之虚函数与多态(一):虚函数表指针、虚析构函数、object slicing与虚函数
    二进制程序分析工具Pin在Windows系统中的安装和使用方法
    使用U盘安装Ubuntu系统的实践小结
    HDU 1874 畅通工程续
    JSP页面上用横线代替文本框
    Mysql设置编码
  • 原文地址:https://www.cnblogs.com/mslalan/p/8109901.html
Copyright © 2020-2023  润新知