zoukankan      html  css  js  c++  java
  • 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);
      }

  • 相关阅读:
    ASP.NET MVC 重点教程一周年版 第二回 UrlRouting
    ASP.NET MVC 重点教程一周年版 第三回 Controller与View
    DynamicData for Asp.net Mvc留言本实例 下篇 更新
    Asp.net MVC视频教程 18 单选与复选框
    使用ASP.NET MVC Futures 中的异步Action
    ASP.NET MVC RC 升级要注意的几点
    ATL、MFC、WTL CString 的今生前世
    msvcprt.lib(MSVCP90.dll) : error LNK2005:已经在libcpmtd.lib(xmutex.obj) 中定义
    关于Windows内存的一些参考文章
    Windows访问令牌相关使用方法
  • 原文地址:https://www.cnblogs.com/mslalan/p/8109901.html
Copyright © 2011-2022 走看看