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

line-height垂直居中

缺点,仅限于单行文字

  1. .item{
  2. text-align: center;
  3. height: 100px;
  4. line-height: 100px;
  5. }

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

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

  1. .item{
  2. padding: 40px 0;
  3. }

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

margin垂直居中

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

  1. .wrap{
  2. height: 100px;
  3. width: 220px;
  4. }
  5. .item{
  6. width: 100px;
  7. height: 30px;
  8. margin-top: 35px;
  9. margin-left: 60px;
  10. }

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

position垂直居中

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

  1. .wrap{
  2. position: relative;
  3. width:220px;
  4. height: 200px;
  5. }
  6. .item{
  7. position: absolute;
  8. width: 100px;
  9. height: 50px;
  10. left: 50%;
  11. top: 50%;
  12. margin-left: -50px;
  13. margin-top: -25px;
  14. }

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

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

  1. .wrap{
  2. position: relative;
  3. width:220px;
  4. height: 200px;
  5. }
  6. .item{
  7. width: 100px;height: 50px;
  8. margin:auto;
  9. position: absolute;
  10. left: 0px;
  11. top: 0px;
  12. right:0px;
  13. bottom: 0px;
  14. }

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

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

  1. .wrap{
  2. width:220px;
  3. height: 150px;
  4. overflow: hidden;
  5. }
  6. .item{
  7. width: 100px;
  8. height: 40px;
  9. margin:0 auto;
  10. margin-top: calc(150px/2 - 40px/2);
  11. }

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

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

  1. .wrap{
  2. width:220px;
  3. height: 150px;
  4. overflow: hidden;
  5. }
  6. .item{
  7. width: 100px;
  8. height: 40px;
  9. margin:0 auto;
  10. position: relative;
  11. top: 50%;
  12. transform:translate3d(0px,-50%,0px);
  13. }

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

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

  1. .wrap{
  2. height: 150px;
  3. width:220px;
  4. }
  5. .placeholder{
  6. overflow: hidden;
  7. width:;
  8. min-height: inherit;
  9. height: inherit;
  10. vertical-align: middle;
  11. display: inline-block;
  12. }
  13.  
  14. .item{
  15. width: 100px;
  16. height: 50px;
  17. vertical-align: middle;
  18. display: inline-block;
  19. }

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

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

  1. .wrap{
  2. line-height: 200px;
  3. }
  4. .item{
  5. line-height: normal;
  6. vertical-align: middle;
  7. display: inline-block;
  8. }

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

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

  1. .wrap{
  2. display: -webkit-box;
  3. display: -moz-box;
  4. display: -ms-flexbox;
  5. display: -webkit-flex;
  6. display: flex;
  7. height: 150px;
  8. }
  9. .item{
  10. margin:auto; //这句话是关键
  11. width: 100px;
  12. height: 50px;
  13. }

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

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

  1. .wrap{
  2. display: -webkit-box;
  3. display: -moz-box;
  4. display: -ms-flexbox;
  5. display: -webkit-flex;
  6. display: flex;
  7. height: 150px;
  8. align-items: center ;
  9. justify-content: center;
  10. }
  11. .item{
  12. width: 100px;
  13. height: 50px;
  14. background: #555;
  15. line-height: 50px;
  16. }

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

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

  1. .wrap{
  2. display: table-cell;
  3. vertical-align: middle;
  4. text-align: center;
  5. height: 150px;
  6. }
  7. .item{
  8. width: 100px;
  9. line-height: 50px;
  10. display: inline-block; //转换为内联元素
  11. }

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

使用button标签

  1. .wrap{
  2. height: 150px;
  3. background: #222;
  4. border-radius: 0px;
  5. border:none;
  6. display:block;
  7. margin:20px auto;
  8. width: 220px;
  9. }
  10. .item{
  11. width: 100px;
  12. line-height: 50px;
  13. display: inline-block;
  14. }

效果: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. BZOJ 1202 [HNOI2005]狡猾的商人:并查集(维护距离)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 题意: 有一个账本,记录了n个月的盈亏. 每个月的数值:正为盈,负为亏. 你知道m个 ...

  2. css(4)

    类选择器和id选择器都有父子选择器. 在css文件中国,有时候为了简化样式,可以把相同的样式拿出来放在一起. display:inline display:block 行内元素里只能放行内元素,而块内 ...

  3. c语言字符串 数字转换函数大全

    最近学数据结构老是做实验 常用到字符串和数字的转换 想找却发现网上的资料太散 所以搜集整理一下 方便以后再用 atof(将字符串转换成浮点型数) atoi(将字符串转换成整型数) atol(将字符串转 ...

  4. idea创建maven-archetype-webapp项目无java目录

    使用idea创建一个maven-archetype-webapp项目 查看项目的目录结构,在main的目录下没有java目录 在main目录下创建java目录 使用快捷键 ctrl+alt+shift ...

  5. storm源码剖析(2):storm的配置项

    storm的配置项,可以从backtype/storm/Config.java中找到所有配置项及其描述

  6. linux命令学习笔记(24):Linux文件类型与扩展名

    Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序 而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux ...

  7. mac laravel 环境变量设置bash_profile

    mac laravel 环境变量设置bash_profile >>>vim ~/.bash_profile '''text export PATH=$PATH:~/.composer ...

  8. zero to one (2)

    kali虚拟机整理 关于kali的虚拟机,我搞崩过很多次,重新安装了很多次,也遇到了很多问题,有一些解决不了的就重新安装,费时费力,要善于用虚拟机. 关于网络配置的问题 关于网络配置,在这里我想主要记 ...

  9. Seal Report结合MySQL数据库 报表展现_20161011

    昨天留了草稿 未来得及发 今日补发一篇 seal report 操作类似excel数据透视表也包含行标签和列标签及值标签,行列代表的是数据指标的维度,值标签代表的是度量. seal report连接M ...

  10. Python手记

    字符串的拼接 1.“+”,如果是字符和数字相连,要使用str()函数对于数字进行字符转化: 2.join() 3.",",链接的两个字符串之间用空格做关联 4.占位符 tmp += ...