代码:

<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);
  }

div水平垂直居中方法及优缺点的更多相关文章

  1. 【笔记】让DIV水平垂直居中的两种方法

    今天写的了百度前端学院春季班的任务:定位和居中问题  由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中. ...

  2. Div水平垂直居中的三种方法

    百度了很多种方法,很多是不起作用的.下面这些方法是我亲自试过的.希望对大家有帮助! 下面是一波代码水军. <!DOCTYPE html> <html lang="en&qu ...

  3. div 固定宽高 水平垂直居中方法

    div固定宽高,水平垂直居中,根据所用单位不同,分成两种情况,分别是"px"和"%"情况. 例:将三层div做出三个边框,要求水平垂直居中.效果如图 情况一(单 ...

  4. 如何让div水平垂直居中

    引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang=&q ...

  5. css两种常用的不定宽高的水平垂直居中方法,记住它,不再为样式发愁

    css 几种常用的简单容易记住的水平垂直居中方法 前言 正文 第一种方法 第二种方法 结束语 前言 我们在设计网页时,会大量的运用到水平垂直居中,如果知道元素的宽高,那水平垂直居中是很简单的,无非是用 ...

  6. 【最全】CSS盒子(div)水平垂直居中居然还有这种方式

    最全的CSS盒子(div)水平垂直居中布局,对CSS 布局掌握程度决定你在 Web 开发中的开发页面速度. 相对于屏幕 方法一:利用定位 <div class="box"&g ...

  7. 文字以及div水平垂直居中

    文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div>< ...

  8. DIV水平垂直居中的CSS兼容写法

    DIV水平垂直居中,非IE浏览器可以用CSS3来处理,IE浏览器中分别处理IE6和/IE7.IE8.IE9. 在IE低版本中,虽然大致上没有问题,但还是有一些细微的显示问题. 示例如下: <!D ...

  9. scss : div水平垂直居中

    scss 是一个很好用的css预处理语言,有很多很好的特性. 比如 mixin. 我们可以像使用函数那样使用mixin. 比如写一个div水平垂直居中. 上代码. @mixin absolute_ce ...

随机推荐

  1. Django的admin介绍

    我们看到我们创建一个默认的django的项目,他在project的urls有一个admin的url的路径 我们访问这个路径,他是一个登陆框,需要输入用户名和密码 我们就需要创建这个用户名和密码,如果你 ...

  2. 交叉字符串 · Interleaving String

    [抄题]: 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成.(洗牌) 比如 s1 = "aabcc" s2 = "dbbca" - 当 s3 ...

  3. dede的cfg_keywords和cfg_description无法显示

    问题:在生成html文件时,网页的keywords和description的content为空,但后台显示这两项是有值的.   解决方案: 1.设置 系统->系统基本参数->站点根网址 设 ...

  4. Spring框架中的工厂(了解)

    1. ApplicationContext接口 * 使用ApplicationContext工厂的接口,使用该接口可以获取到具体的Bean对象 * 该接口下有两个具体的实现类 * ClassPathX ...

  5. 轮播图jQuery

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. beego启动找不到conf的原因

    beego配置文件路径如下: app.conf内容 httpaddr = "192.168.199.178" httpport = appname = SecProxy runmo ...

  7. Banner中文字怎么排版才好看?

    今天这命题,相信有很多人提出过疑问,一个好的文字排版会给你的作品增添色彩,我们先看看好的作品是怎样的. 看完这些图大家都能感觉出来这是一个好的作品,大家天生就对美和丑有一定的区分. 其实文字排版也好, ...

  8. Reactor 模型(一)基本并发编程模型

    Reactor 模型(一)基本并发编程模型 Netty 系列目录 (https://www.cnblogs.com/binarylei/p/10117436.html) 在讲解 Reactor 线程模 ...

  9. 设计模式-生成者模式之c#代码

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  10. sys.argv和getopt.getopt()的用法

    1.sys.argv Python中sys.argv是命令行参数从程序外部传值的的一种途径,它是一个列表,列表元素是我们想传进去的的新参数,所以可以用索引sys.argv[]来获得想要的值.因为一个写 ...