我们平时常用的定高,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水平垂直居中的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. scss : div水平垂直居中

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

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

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

  8. 【html】【10】div布局[div水平垂直居中]

    必看参考: http://www.jb51.net/css/28259.html 让div居中对齐缩写形式为: .style{margin:0 auto;} 数字0 表示上下边距是0.可以按照需要设置 ...

  9. 未知宽高div水平垂直居中3种方法

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> </head&g ...

  10. 如何将一个div水平垂直居中?4种方法做推荐

    方案一: div绝对定位水平垂直居中[margin:auto实现绝对定位元素的居中], 兼容性:,IE7及之前版本不支持 div{ width: 200px; height: 200px; backg ...

随机推荐

  1. 转:中间人攻击利用框架bettercap测试

    0x00前言 上篇提到内网渗透很有趣,这次就从一款新工具说起: bettercap 0x01简介 bettercap可用来实现各种中间人攻击,模块化,便携.易扩展 0x02特点 提到中间人攻击,最知名 ...

  2. 简单的java socket 示例

    一.搭建服务器端 a).创建ServerSocket对象绑定监听端口. b).通过accept()方法监听客户端的请求. c).建立连接后,通过输入输出流读取客户端发送的请求信息. d).通过输出流向 ...

  3. Java线程:概念与原理

    Java线程:概念与原理 一.操作系统中线程和进程的概念 现在的操作系统是多任务操作系统.多线程是实现多任务的一种方式. 进程是指一个内存中运行的应用程序,每个进程都有自己独立的一块内存空间,一个进程 ...

  4. 解决WAMP搭建PHP环境后后局域网其他机器无法访问的问题

    刚安装wamp以后本地访问localhost或者127.0.0.1可以访问,但是如果局域网内其他电脑访问则出现403错误.从网上找了很多,各种说法都有了,却没几个好用的.解决问题方法如下: 1,首先确 ...

  5. temp_web

    使用vs2010创建.发布.部署.调用 WebService http://blog.sina.com.cn/s/blog_45eaa01a0102vp8z.html c#简易Http服务器 http ...

  6. Ninject之旅之九:Ninject上下文绑定(附程序下载)

    摘要 既然在插件模型里,每一个服务类型可以被映射到多个实现,绑定方法不用决定要返回哪个实现.因为kernel应该返回所有的实现.然而,上下文绑定是多个绑定场景,在这个场景里,kernel需要根据给定的 ...

  7. Leetcode2:Add Two Numbers@Python

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  8. Welcome to China

    subway, railway, highway ,way way to dieofficer,announcer, professor, sir sir to lieWelcome to China

  9. PHPCMS V9 分页类的修改教程

    首先,打开 phpcms\libs\functions\global.func.php 这个文件,找到文件第622行的分页函数,复制一下,粘贴到默认分页函数的下面,重新命名后保存.(笔者在此命名为:p ...

  10. 解决低版本chrome浏览器不支持es6 Array.find()

     if (!Array.prototype.find) {  Array.prototype.find = function(predicate) {    'use strict';    if ( ...