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. html5--2.1新的布局元素(1)-header/footer

    html5--2.1新的布局元素(1)-header/footer 学习要点 了解header/footer的语义和用法 使用header/footer进行一个简单的布局 header元素(标签) 用 ...

  2. 存储过程IF --ELSE IF -- END IF 使用

    CREATE OR REPLACE PROCEDURE BJPJYXK_HF_SD( sqid_p IN VARCHAR2,--申请单ID xkbh_p IN VARCHAR2,--新生成的许可证编号 ...

  3. RAC环境下控制文件损坏重建过程

    处理过程参考了: https://blogs.oracle.com/Database4CN/entry/%E5%A6%82%E4%BD%95%E9%87%8D%E5%BB%BArac%E7%9A%84 ...

  4. About getByClass

    不能获取class为多个的情况 function getByClass(parent,cls){ var res=[]; var ele=parent.getElementsByTagName(&qu ...

  5. 「CF779B」「LOJ#10201.」「一本通 6.2 练习 4」Sherlock and His Girlfriend(埃氏筛

    题目描述 原题来自:Codeforces Round #400 B. Sherlock 有了一个新女友(这太不像他了!).情人节到了,他想送给女友一些珠宝当做礼物. 他买了 nnn 件珠宝.第 iii ...

  6. [转载]Windows网络编程系列教程之四:Select模型

    原文:http://www.51see.com/asp/bbs/public/bp_show.asp?t_id=200308131152297103 讲一下套接字模式和套接字I/O模型的区别.先说明一 ...

  7. scala新人佑门

    scala语言 1. val和var val和var当前区别在于前者只能被赋值一次,就像java中的final,但是后者则可以随意覆盖: 2. Unit Unit返回代表空返回,类似于vo 3. 类型 ...

  8. 洛谷 P4512 [模板] 多项式除法

    题目:https://www.luogu.org/problemnew/show/P4512 看博客:https://www.cnblogs.com/owenyu/p/6724611.html htt ...

  9. 【opencv学习笔记一】opencv下载安装与VS2017开发环境配置

    本文章摘录自浅墨博客,原文链接http://blog.csdn.net/poem_qianmo/article/details/19809337 目录 1.opencv下载与安装 2.计算机环境变量配 ...

  10. java线程基本原理

    1.线程池简介:     多线程技术主要解决处理器单元内多个线程执行的问题,它可以显著减少处理器单元的闲置时间,增加处理器单元的吞吐能力.        假设一个服务器完成一项任务所需时间为:T1 创 ...