一、前言:


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

主要方式:

  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. C语言 · 阶乘计算 · 基础练习

    问题描述 输入一个正整数n,输出n!的值. 其中n!=1*2*3*-*n. 算法描述 n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个大整数a,A[0]表 ...

  2. Android总结之链式调用(方法链)

    前言: 最近在学习总结Android属性动画的时候,发现Android的属性动画设计采用了链式调用的方式,然后又回顾了一下了以前接触的开源框架Glide也是采用链式调用的方式,还有最近火的一塌糊涂的R ...

  3. vue.js学习笔记

    有了孩子之后,元旦就哪也去不了了(孩子太小),刚好利用一些时间,来公司充充电补补课,学习学习新技术,在这里做一个整理和总结.(选择的东西,既然热爱就把他做好吧!). 下来进入咱们的学习环节: 一.从H ...

  4. 用FSM一键制作逐帧动画雪碧图 Vue2 + webpack

    因为工作需要要将五六十张逐帧图拼成雪碧图,网上想找到一件制作工具半天没有找到,就自己用canvas写了一个. 写成之后就再没有什么机会使用了,因此希望有人使用的时候如果遇到bug了能及时反馈给我. 最 ...

  5. mongodb

    修改所有的记录: > db.t_express_apply.update({},{$set:{"isStatus" : 0}},{multi:true})WriteResul ...

  6. Java实现Excel中的NORMSDIST函数和NORMSINV函数

    由于工作中需要将Excel中的此两种函数转换成java函数,从而计算内部评级的资本占用率和资本占用金额.经过多方查阅资料和整理,总结出如下两个转换方法 标准正态分布累计函数NORMSDIST: pub ...

  7. 浏览器的兼容模式下的button中文字垂直方向不居中显示

    <button style="cursor:pointer;vertical-align: middle;" >删除</button> 这时候垂直不居中. ...

  8. iOS - 模态Model视图跳转和Push视图跳转的混合需求实现原理

    在研发中总会遇到一些莫名的需求,本着存在即合理的态度跟大家分享一下"模态Model视图跳转和Push视图跳转的需求实现",本文仅仅传授研发技术不传授产品以及UE的思想,请大家合理对 ...

  9. 编写一个通用的Makefile文件

    1.1在这之前,我们需要了解程序的编译过程 a.预处理:检查语法错误,展开宏,包含头文件等 b.编译:*.c-->*.S c.汇编:*.S-->*.o d.链接:.o +库文件=*.exe ...

  10. Mono 3.2.7发布,JIT和GC进一步改进

    Mono 3.2.7已经发布,带来了很多新特性,如改进的JIT.新的面向LINQ的解释器以及使用了64位原生指令等等. 这是一次主要特性发布,累积了大约5个月的开发工作.看上去大部分改进都是底层的性能 ...