一、前言:


垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式。

主要方式:

  1. line-height
  2. 绝对定位
  3. 表格 display:table-cell

主要需求:

  1. 固定宽高
  2. 不固定宽高

主要兼容:

  1. ie8+  主流浏览器
  2. ie6,7

二、行高


1. 利用行高与高度相同,实现单行文本居中

缺点:只能是单行文本

 <!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.d1{width: 200px;height: 200px;background-color: blue;line-height: 200px;}
</style>
</head>
<body>
<div class="d1">
fdsfdsfdsfdfsfds
</div>
</body>
</html>

效果预览

2.利用inline-block改进(推荐)

改变display属性,就可以是块元素了,vertical-align: middle;设置垂直属性

优点:自适应内容

兼容:>=ie8 + 主流

 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
text-align: center;
line-height: 500px;
}
.div2{
display: inline-block;
vertical-align: middle;
width: 200px;
height: 200px;
background-color: red;
/*通过 line-hight 来垂直居中 此法>= ie8*/
}
</style>
<body>
<div class="div1">
<div class="div2"> </div>
</div>
</body>
</html>

效果预览

三、绝对定位


1.负margin

top,left 50%;margin -50%;

缺点:需固定宽高

 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
position: absolute;
width: 200px;
height: 200px;
background-color: red;
top: 50%;
left: 50%;
margin-left: -100px; /*此处为width的一半,所以应用此法,元素必须固定宽、高*/
margin-top: -100px; }
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>

效果预览

2.css3 translate

我们希望实现不固定宽高,在上法上改进。可以用js,动态添加宽高,但不推荐。其实可以用css3 translate属性,因为translate是唯一一个相对于自身宽度计算的属性

兼容:>=ie9 + 主流

优点:自适应内容

 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
position: absolute;
background-color: red;
top: 50%;
left: 50%;
transform:translate(-50%,-50%);
/*应用css3 translate属性是相对于自身的,此法>=ie9,且宽高自适应*/
}
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>

效果预览

3.绝对定位 + 相对定位(推荐)

思想与上面的方式是一样,只是这是基于ie6,7的bug,相对定位位移父元素的50%

缺点:多添加一个容器标签

优点:自适应内容

兼容:ie6,7

 <!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.middle-demo-4{
background-color: blue;
height: 300px;
width: 300px;
position: relative;
}
.middle-demo-4 div{
position: absolute;
top: 50%;
left: 50%;
}
.middle-demo-4 div div{
height: 200px;
background-color: red;
position: relative;
top: -50%;
left: -50%;
}/*ie6 ie7*/
</style>
</head>
<body>
<div class="middle-demo-4">
<div>
<div>dsfdsfdsfdsfdsfdsfdsf</div>
</div>
</div> </body>
</html>

4.margin:auto

绝对定位下,固定宽高,top:0,bottom:0,会自适应内容,多余部分会用margin填充

缺点:固定宽高

兼容:>=ie8

 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
margin: auto;
position: absolute;
background-color: red;
width: 200px;
height: 200px;
left: 0;
right: 0;
top: 0;
bottom: 0; }
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>

结果预览

四、表格


 1.table-cell(推荐)

单元格可以设置垂直属性 vertical-align: middle;

优点:自适应内容

兼容:>=ie8 +主流

缺点:子元素为浮动、绝对定位无效(注意)

 <!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.div2{
/*float: left;position: absolute; 子元素为浮动、绝对定位无效
此法>=ie8
*/}
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>

结果预览

五、总结


根据兼容性和自适应内容来考虑         表格/行高法 + 相对定位法

如果固定宽高                                 负margin法

css 垂直水平居中总结的更多相关文章

  1. CSS垂直水平居中方法总结

    在布局的时候经常能用到居中,在此总结一下 html结构: <div class="wrap"> <div class="content"> ...

  2. 关于css垂直水平居中的几种方式

    css中元素的垂直水平居中是比较常见及较常使用的,在这里向大家介绍一下几种方式. 1.水平居中 margin: 0 auto; 效果图: 而文字的垂直水平居中也比较简单,加上line-height: ...

  3. css 垂直+水平居中

    垂直+水平居中是一个老生常谈的问题了,现在就固定高度和不固定高度两种情况去讨论 1.父盒子固定高度[定位] 实现1: father-box: position:relative child-box:p ...

  4. CSS垂直水平居中方法整理

    CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...

  5. css垂直水平居中方案

    1. 水平居中 如果是inline元素:在父元素上面设置text-align:center; 如果是block元素:设置宽度和margin:0 auto; 如果是多块级元素:在父元素上面设置text- ...

  6. (转载)css垂直水平居中的整理

    方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...

  7. CSS垂直水平居中

    小小的总结一下:行内元素水平居中用text-align: center;块级元素水平居中用margin-left: auto; margin-right: auto; 首先讨论一下单行时的情况. 毫无 ...

  8. 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)

    实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...

  9. div+css实现未知宽高元素垂直水平居中

    div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框(或者图片)垂直水平居中??如果用JS的话就好办了,但是JS的使用 ...

随机推荐

  1. CorelDRAW X8 如何破解激活(附国际版安装包+激活工具) 2016-12-15

    之前有位搞平面的好友“小瘦”说CDR X8无法破解,只能用X7.呃……呃……呃……好像是的 其实CDR8难激活主要在于一个点“没有离线激活了,只可以在线激活”,逆天不是专供逆向的,当然没能力去破解,这 ...

  2. 01.LoT.UI 前后台通用框架分解系列之——小图片背景全屏显示(可自动切换背景)

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  3. 基于DFA敏感词查询的算法简析

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 项目中需要对敏感词做一个过滤,首先有几个方案可以选择: a.直 ...

  4. CentOS7使用firewalld打开关闭防火墙与端口(转载)

    1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disabl ...

  5. C#——传值参数(2)

    //我的C#是跟着猛哥(刘铁猛)(算是我的正式老师)<C#语言入门详解>学习的,微信上猛哥也给我讲解了一些不懂得地方,对于我来说简直是一笔巨额财富,难得良师! 这次与大家共同学习C#中的 ...

  6. 开发者最爱的Firebug停止更新和维护

        近日,Firebug团队在其官网上宣布,Firebug将不再继续开发和维护,并邀请大家使用Firefox的内置开发工具.     Firebug最初是2006年1月由Joe Hewitt编写, ...

  7. java面向对象中的关键字

    1,super关键字 super:父类的意思 1. super.属性名 (调用父类的属性) 2. super.方法名 (调用父类的方法) 3. super([参数列表])(调用父类的构造方法) 注意: ...

  8. Spring配置文件标签报错:The prefix "XXX" for element "XXX:XXX" is not bound. .

    例如:The prefix "context" for element "context:annotation-config" is not bound. 这种 ...

  9. Java企业实训 - 01 - Java前奏

    前言: 虽然个人专攻.NET方向,不过由于个人是干教育行业的,方方面面的东西,不能说都必须精通,但肯定多少都会涉及到. 一个菜鸟学员,从啥都不会,经过一步步学习,最后到企业上手掌管一个模块甚至一个项目 ...

  10. windows 7(32/64位)GHO安装指南(序篇)~

    大家好,本人是高三刚毕业,即将踏入校园的程序猿~我写这篇文章呢,主要是想巩固一下之前对于电脑的基础知识理论,也希望能帮助没有电脑基础的同学能维护一下自己的电脑,要是能帮助女生修电脑那就是更好啦~~哈哈 ...