css各种水平垂直居中,网上查了一下,总结以下几种

line-height垂直居中

缺点,仅限于单行文字

.item{
text-align: center;
height: 100px;
line-height: 100px;
}

效果:http://runjs.cn/code/rmjiq3a8

padding垂直居中
缺点,内容不确定时,高度不好固定

.item{
padding: 40px 0;
}

效果:http://runjs.cn/code/hg5yrpm8

margin垂直居中

需要知道父层和子层高度,然后marginLeft && marginTop = 父层/2 - 子层/2;
缺点,不灵活

.wrap{
height: 100px;
width: 220px;
}
.item{
width: 100px;
height: 30px;
margin-top: 35px;
margin-left: 60px;
}

效果:http://runjs.cn/code/tvewrftd

position垂直居中

需要知道子层高度,根据子层高度来设置margin;
缺点,不灵活

.wrap{
position: relative;
width:220px;
height: 200px;
}
.item{
position: absolute;
width: 100px;
height: 50px;
left: 50%;
top: 50%;
margin-left: -50px;
margin-top: -25px;
}

效果:http://runjs.cn/code/hkpk8zdr

position垂直居中二
内容div固定宽高,不固定宽高都可以,但是父层貌似必须有宽高。
缺点:父层宽高,比较灵活

.wrap{
position: relative;
width:220px;
height: 200px;
}
.item{
width: 100px;height: 50px;
margin:auto;
position: absolute;
left: 0px;
top: 0px;
right:0px;
bottom: 0px;
}

效果:http://runjs.cn/code/lo7kn0lx

css3的calc垂直居中
也是需要知道父层宽高和自身宽高;
缺点,不灵活,兼容性

.wrap{
width:220px;
height: 150px;
overflow: hidden;
}
.item{
width: 100px;
height: 40px;
margin:0 auto;
margin-top: calc(150px/2 - 40px/2);
}

效果:http://runjs.cn/code/jsuy1smh

css3的translate垂直居中
这个是最方便,尤其在移动端,简直神器!

.wrap{
width:220px;
height: 150px;
overflow: hidden;
}
.item{
width: 100px;
height: 40px;
margin:0 auto;
position: relative;
top: 50%;
transform:translate3d(0px,-50%,0px);
}

效果:http://runjs.cn/code/wnpyt6qq

text-align + vertical-align垂直居中
添加一个占位标签。
没啥大缺点,多加了一个标签

.wrap{
height: 150px;
width:220px;
}
.placeholder{
overflow: hidden;
width:;
min-height: inherit;
height: inherit;
vertical-align: middle;
display: inline-block;
} .item{
width: 100px;
height: 50px;
vertical-align: middle;
display: inline-block;
}

效果:http://runjs.cn/code/pvopbrds

text-align + line-height垂直居中
缺点:父元素必须有个行高。

.wrap{
line-height: 200px;
}
.item{
line-height: normal;
vertical-align: middle;
display: inline-block;
}

效果:http://runjs.cn/code/oldyl2ee

flex垂直居中。
唯一缺点就是兼容性了.

.wrap{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
height: 150px;
}
.item{
margin:auto; //这句话是关键
width: 100px;
height: 50px;
}

效果:http://runjs.cn/code/g8wqi2kx

flex垂直居中二
唯一缺点就是兼容性

.wrap{
display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
height: 150px;
align-items: center ;
justify-content: center;
}
.item{
width: 100px;
height: 50px;
background: #555;
line-height: 50px;
}

效果:http://runjs.cn/code/qsdrl4tk

table垂直居中
利用table表格的属性,来居中元素

.wrap{
display: table-cell;
vertical-align: middle;
text-align: center;
height: 150px;
}
.item{
width: 100px;
line-height: 50px;
display: inline-block; //转换为内联元素
}

效果:http://runjs.cn/code/6flrxvh2

使用button标签

.wrap{
height: 150px;
background: #222;
border-radius: 0px;
border:none;
display:block;
margin:20px auto;
width: 220px;
}
.item{
width: 100px;
line-height: 50px;
display: inline-block;
}

效果:http://runjs.cn/code/1zvstbad

抄袭于:http://itakeo.com/blog/2015/09/17/csscenter/?none=121

css各种水平垂直居中的更多相关文章

  1. CSS实现水平|垂直居中漫谈

    利用CSS进行元素的水平居中,比较简单,手到擒来:行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可.而撸起垂直居中, ...

  2. 纯CSS制作水平垂直居中“十字架”

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

  3. CSS制作水平垂直居中对齐

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀 ...

  4. CSS实现水平垂直居中的1010种方式

    转载自:CSS实现水平垂直居中的1010种方式 划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了 要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下CSS实现水平垂直居 ...

  5. 你知道CSS实现水平垂直居中的第10种方式吗?

    你知道CSS实现水平垂直居中的第10种方式吗? 仅居中元素定宽高适用: absolute + 负 margin absolute + margin auto absolute + calc 居中元素不 ...

  6. css实现水平 垂直居中

    css实现水平居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  7. CSS制作水平垂直居中对齐 多种方式各有千秋

    作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋 ...

  8. CSS实现水平垂直居中的数种方法整合

    CSS实现水平垂直居中可以说是前端老生常谈的问题了,一般面试官问的时候面试者都会回答出来,但是继续追问还有没有其他方法的时候有可能就说不出来了. 本着学习知识的目的,特在此纪录CSS实现水平垂直居中的 ...

  9. CSS如何水平垂直居中?

    CSS如何水平垂直居中? 1.CSS如何实现水平居中? margin: 0 auto 2.CSS如何实现水平垂直居中? 首先设置一个div元素,设置背景颜色以便看出变化.代码如下: <!DOCT ...

随机推荐

  1. jquery 3D分页翻转滑块

    jquery 3D分页翻转滑块,jquery分页,jquery插件,jquery,3D翻转,css3分页,360度旋转,网页特效代码3D分页翻转滑块是一款使用网格样式与滑块效果分页的特效.

  2. Cocos2d-x中定时器的使用

    CCTimer:轻量级的计时器 CCTimer (void) ccTime  getInterval (void) void  setInterval (ccTime fInterval) bool  ...

  3. Linux_学习_01_ 压缩文件夹

    二.参考资料 1.Linux下压缩某个文件夹命令

  4. u3d 多线程 网络

    开启一个线程做网络连接,和接收数据, 用event进行广播 using UnityEngine; using System; using System.Threading; using System. ...

  5. linux 下文件恢复工具extundelete介绍

        下载 http://extundelete.sourceforge.net/ bunzip2 extundelete-0.2.0.tar.bz2 tar xvf extundelete-0.2 ...

  6. kettle及数据库导数_20160920

    一.kettle是什么. Kettle是一款国外开源的ETL( Extract-Transform-Load 的缩写,用来描述将数据从来源端经过抽取(extract).转换(transform).加载 ...

  7. ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)

    Description Three wizards are doing a experiment. To avoid from bothering, a special magic is set ar ...

  8. uC/OS-II源码分析(五)

    每个任务被赋予不同的优先级等级,从0 级到最低优先级OS_LOWEST_PR1O,包括0 和 OS_LOWEST_PR1O 在内.当μC/OS-Ⅱ初始化的时候,最低优先级OS_LOWEST_PR1O  ...

  9. poj2777Count Color——线段树+状压

    题目:http://poj.org/problem?id=2777 状压每个颜色的选择情况,取答案时 | 一番: 注意题目中的区间端点可能大小相反,在读入时换一下位置: 注意pushdown()中要l ...

  10. Oracle获取日期的特定部分

    (1)oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分 ,语法:extract ({ year | month | day ...