div水平垂直居中
我们平时常用的定高,top:50%;left:50%和margin-left负一半宽度margin-top负一半高度的居中方式暂不考虑,因为这种方式大家都会。
第一种绝对定位(absolute centering)居中:
<div class="abs-center"></div>
.abs-center{ height: 50%; width: 50%; background: red; margin: auto; position: absolute; left: 0; top: 0; right: 0; bottom: 0; }
原理:
在普通内容流(normal content flow)中,margin:auto的效果等同于margin-top:0;margin-bottom:0。
position:absolute使绝对定位块跳出了内容流,内容流中的其余部分渲染时绝对定位部分不进行渲染。
为块区域设置top: 0; left: 0; bottom: 0; right: 0;将给浏览器重新分配一个边界框,此时该块block将填充其父元素的所有可用空间,父元素一般为body或者声明为position:relative;的容器。
由于内容块被绝对定位,脱离了正常的内容流,浏览器会给margin-top,margin-bottom相同的值,使元素块在先前定义的边界内居中。
这么看来, margin:auto似乎生来就是为绝对居中(Absolute Centering)设计的,所以绝对居中(Absolute Centering)应该都兼容符合标准的现代浏览器。
优点:
1、支持跨浏览器,包括IE8以上,Chrome,Firefox, Safari, Mobile Safari;
		2、无需其他特殊标记,CSS代码量少;
		3、支持百分比%属性值和min-/max-属性
		4、不论是否设置padding都可居中(在不使用box-sizing属性的前提下)
		5、完美支持图片居中
缺点:
1、必须声明高度
		2、在Windows Phone设备上不起作用
第二种方法table-cell居中
<div class="is-table">
<div class="table-cell">
<div class="center-block">
<!-- content -->
</div>
</div>
</div>
 .is-table{ display: table; background: grey; position: absolute; width: 100%; height: 100%; }
         .is-table .table-cell{ display: table-cell; vertical-align: middle; background: blue; }
         .is-table .center-block{ width: 50%; margin: 0 auto; background: red; }
优点:
1、不定高度
		2、内容溢出会将父元素撑开
		3、跨浏览器兼容性好
缺点:
1、需要大量额外的标记
		2、ie7以下不支持
第三种方法transform
<div class="box"></div>
 .box{width: 50%;
             height: 400px;
             background: red;
               margin: auto;
               position: absolute;
               top: 50%; left: 50%;
             -webkit-transform: translate(-50%,-50%);
                 -ms-transform: translate(-50%,-50%);
                     transform: translate(-50%,-50%);
   }
优点:
1、内容可变高度
		2、代码量少
缺点:
1、IE8不支持
		2、属性需要写浏览器厂商前缀
		3、可能干扰其他transform效果
		4、某些情形下会出现文本或元素边界渲染模糊的现象
第四种方法inline-block
<div class="inline-block">
<div class="center-block">
<p>inline-block这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容这是内容</p>
</div>
</div>
*{margin: 0; padding: 0; }
        .inline-block{
            text-align: center;
              overflow: auto;
              height: 100%;
            width: 100%;
            position: absolute;
            left: 0;
            top: 0;
        }
        .inline-block:after{
            content: '';
            height: 100%;
            margin-left: -0.25em;
        }
        .inline-block:after, .center-block{
            display: inline-block;
              vertical-align: middle;
        }
        .center-block{
             max-width: 99%;
             width: 500px;  /*自定宽度*/
            /* Prevents issues with long content causes the content block to be pushed to the top */
              /* max-width: calc(100% - 0.25em)  Only for IE9+ */
        }
优点:
1、内容可变高度
		2、内容溢出会将父元素撑开。
缺点:
1、IE7以下不支持
		2、水平居中依赖于margin-left: -0.25em;该尺寸对于不同的字体/字号需要调整
		3、内容块宽度不能超过容器的100% - 0.25em。
第五种方法display: flex:
<div class="box">
<!-- content -->
</div>
 html{height: 100%;}
         body{
            display: -webkit-box;   /* OLD: Safari,  iOS, Android browser, older WebKit browsers.  */
            display: -moz-box;      /* OLD: Firefox (buggy) */
            display: -ms-flexbox;   /* MID: IE 10 */
            display: -webkit-flex;  /* NEW, Chrome 21–28, Safari 6.1+ */
            display: flex;          /* NEW: IE11, Chrome 29+, Opera 12.1+, Firefox 22+ */
            -webkit-box-align: center; -moz-box-align: center; /* OLD… */
            -ms-flex-align: center; /* You know the drill now… */
            -webkit-align-items: center;
            align-items: center;
            -webkit-box-pack: center; -moz-box-pack: center;
            -ms-flex-pack: center;
            -webkit-justify-content: center;
            justify-content: center;
            margin:;
            height: 100%;
            width: 100%; /*needed for Firefox */
         }
         .box{
            display: -webkit-box; display: -moz-box;
            display: -ms-flexbox;
            display: -webkit-flex;
            display: flex;
            -webkit-box-align: center; -moz-box-align: center;
            -ms-flex-align: center;
            -webkit-align-items: center;
            align-items: center;
            height: 10rem;
         }
优点:
1、内容块的宽高任意,优雅的溢出。
		2、可用于更复杂高级的布局技术中。
缺点:
1、IE9以下不支持。
		2、Body需要特定的CSS样式。
		3、运行于现代浏览器上的代码需要浏览器厂商前缀。
		4、表现上可能会有一些问题
div水平垂直居中的更多相关文章
- 如何让div水平垂直居中
		引子 我们经常遇到需要把div中的内容进行水平和垂直居中.所以,这里介绍一种方法,可以使div水平居中和垂直居中. 代码: <!DOCTYPE html> <html lang=&q ... 
- 文字以及div水平垂直居中
		文字以及div水平垂直居中.md <div class=”content”> <div class=”mydiv”> huangyingnin! </div>< ... 
- 【笔记】让DIV水平垂直居中的两种方法
		今天写的了百度前端学院春季班的任务:定位和居中问题 由于距离上次学习CSS有点久远了,加上以前木有记笔记的习惯,方法忘得只剩下一种,今天通过网上查阅资料总结了以下两种简单的方法让DIV水平垂直居中. ... 
- DIV水平垂直居中的CSS兼容写法
		DIV水平垂直居中,非IE浏览器可以用CSS3来处理,IE浏览器中分别处理IE6和/IE7.IE8.IE9. 在IE低版本中,虽然大致上没有问题,但还是有一些细微的显示问题. 示例如下: <!D ... 
- Div水平垂直居中的三种方法
		百度了很多种方法,很多是不起作用的.下面这些方法是我亲自试过的.希望对大家有帮助! 下面是一波代码水军. <!DOCTYPE html> <html lang="en&qu ... 
- scss : div水平垂直居中
		scss 是一个很好用的css预处理语言,有很多很好的特性. 比如 mixin. 我们可以像使用函数那样使用mixin. 比如写一个div水平垂直居中. 上代码. @mixin absolute_ce ... 
- 【最全】CSS盒子(div)水平垂直居中居然还有这种方式
		最全的CSS盒子(div)水平垂直居中布局,对CSS 布局掌握程度决定你在 Web 开发中的开发页面速度. 相对于屏幕 方法一:利用定位 <div class="box"&g ... 
- 【html】【10】div布局[div水平垂直居中]
		必看参考: http://www.jb51.net/css/28259.html 让div居中对齐缩写形式为: .style{margin:0 auto;} 数字0 表示上下边距是0.可以按照需要设置 ... 
- 未知宽高div水平垂直居中3种方法
		<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ... 
- 如何将一个div水平垂直居中?4种方法做推荐
		方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ... 
随机推荐
- Java获取本机ip和服务器ip
			一.获取服务器IP InetAddress addr = InetAddress.getLocalHost().getHostAddress();//获得本机IP 二.获取客户端本机IP String ... 
- CentOS6上无法启动NFS服务
			CentOS6上无法启动NFS服务 1.系统环境 物理机:Windows 7(32位)旗舰版 虚拟机:CentOS 6.5 2.问题描述 安装好nfs应用程序之后,打算启动nfs服务,却出现以下的报错 ... 
- Openstack Basic
			html,body { } .CodeMirror { height: auto } .CodeMirror-scroll { } .CodeMirror-lines { padding: 4px 0 ... 
- Idea CheckStyle 安装
			Idea CheckStyle 安装 1. 安装CheckStyle a) 下载idea checksytle 插件 b) 打开Idea的 Settings –>Plugi ... 
- 高级php面试题(转)
			一.mysql相关知识 1. mysql优化方式 MYSQL 优化常用方法 mysql 性能优化方案 2.如何分库分表 ... 
- IT在线笔试总结(二)
			1. 循环队列的长度计算:对于非循环队列,尾指针与头指针的差值便是队列长度,而对于循环队列,差值可能为负数,因此需要将差值加上MAXQSIZE再与MAXQSIZE求余. 2. 算法的时间复杂度取决于: ... 
- SpringMVC学习笔记(三)
			一.SpringMVC使用注解完成 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于SpringMVC的配置 <!--configure the setti ... 
- information_schema系列八(事物,锁)
			今天我们主要看一下MySQL information_schema里面的关于innodb的锁和事物的两三个系统表: 看一下锁对应的sql: select * from innodb_lock_wait ... 
- C++STL内存管理方法(g++版)
			STL作为C++的经典作品,一直备受人们关注.本文主要介绍STL的内存管理策略. 早期的STL内存管理 第一次接触STL源码是看侯捷先生的<STL源码剖析>,此书通俗易懂,剖析透彻,是极佳 ... 
- Linux下的tar压缩解压缩命令详解
			转载自http://www.cnblogs.com/qq78292959/archive/2011/07/06/2099427.html tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压 ... 
