css各种水平垂直居中
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;
- }
css各种水平垂直居中的更多相关文章
- CSS实现水平|垂直居中漫谈
利用CSS进行元素的水平居中,比较简单,手到擒来:行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可.而撸起垂直居中, ...
- 纯CSS制作水平垂直居中“十字架”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS制作水平垂直居中对齐
作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀 ...
- CSS实现水平垂直居中的1010种方式
转载自:CSS实现水平垂直居中的1010种方式 划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了 要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下CSS实现水平垂直居 ...
- 你知道CSS实现水平垂直居中的第10种方式吗?
你知道CSS实现水平垂直居中的第10种方式吗? 仅居中元素定宽高适用: absolute + 负 margin absolute + margin auto absolute + calc 居中元素不 ...
- css实现水平 垂直居中
css实现水平居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- CSS制作水平垂直居中对齐 多种方式各有千秋
作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋 ...
- CSS实现水平垂直居中的数种方法整合
CSS实现水平垂直居中可以说是前端老生常谈的问题了,一般面试官问的时候面试者都会回答出来,但是继续追问还有没有其他方法的时候有可能就说不出来了. 本着学习知识的目的,特在此纪录CSS实现水平垂直居中的 ...
- CSS如何水平垂直居中?
CSS如何水平垂直居中? 1.CSS如何实现水平居中? margin: 0 auto 2.CSS如何实现水平垂直居中? 首先设置一个div元素,设置背景颜色以便看出变化.代码如下: <!DOCT ...
随机推荐
- BZOJ 1202 [HNOI2005]狡猾的商人:并查集(维护距离)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 题意: 有一个账本,记录了n个月的盈亏. 每个月的数值:正为盈,负为亏. 你知道m个 ...
- css(4)
类选择器和id选择器都有父子选择器. 在css文件中国,有时候为了简化样式,可以把相同的样式拿出来放在一起. display:inline display:block 行内元素里只能放行内元素,而块内 ...
- c语言字符串 数字转换函数大全
最近学数据结构老是做实验 常用到字符串和数字的转换 想找却发现网上的资料太散 所以搜集整理一下 方便以后再用 atof(将字符串转换成浮点型数) atoi(将字符串转换成整型数) atol(将字符串转 ...
- idea创建maven-archetype-webapp项目无java目录
使用idea创建一个maven-archetype-webapp项目 查看项目的目录结构,在main的目录下没有java目录 在main目录下创建java目录 使用快捷键 ctrl+alt+shift ...
- storm源码剖析(2):storm的配置项
storm的配置项,可以从backtype/storm/Config.java中找到所有配置项及其描述
- linux命令学习笔记(24):Linux文件类型与扩展名
Linux文件类型和Linux文件的文件名所代表的意义是两个不同的概念.我们通过一般应用程序 而创建的比如file.txt.file.tar.gz ,这些文件虽然要用不同的程序来打开,但放在Linux ...
- mac laravel 环境变量设置bash_profile
mac laravel 环境变量设置bash_profile >>>vim ~/.bash_profile '''text export PATH=$PATH:~/.composer ...
- zero to one (2)
kali虚拟机整理 关于kali的虚拟机,我搞崩过很多次,重新安装了很多次,也遇到了很多问题,有一些解决不了的就重新安装,费时费力,要善于用虚拟机. 关于网络配置的问题 关于网络配置,在这里我想主要记 ...
- Seal Report结合MySQL数据库 报表展现_20161011
昨天留了草稿 未来得及发 今日补发一篇 seal report 操作类似excel数据透视表也包含行标签和列标签及值标签,行列代表的是数据指标的维度,值标签代表的是度量. seal report连接M ...
- Python手记
字符串的拼接 1.“+”,如果是字符和数字相连,要使用str()函数对于数字进行字符转化: 2.join() 3.",",链接的两个字符串之间用空格做关联 4.占位符 tmp += ...