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: 0;
    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

时间: 2024-08-09 14:16:17

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

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

作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收 集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀,要正确的选择也是一件不容易的事情.我会将这几种方法一一介绍给大家,以供大家参考.或许对 于我这样的初学者有一定的帮助. 用CSS来实现元素的垂直居中效果是件苦差事,虽然说实现方法有多种,但有很多方式在某些浏览器下可能无法正常的工作.接下来我们就一起来看看这些不同方法实现垂直居中的各自优点和其不足

css实现水平 垂直居中

css实现水平居中 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>水平居中</title> <style> .box1{ border: 1px solid #000; text-align: center; } .box2{ display: inline-block; backgroun

《转》CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳)

转自大地Dudy的CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳) 本文主要是对主流居中方法进行了归纳,有些地方甚至就是把别人的代码直接复制过来的,没有什么自己的东西,除了大漠以及张鑫旭的方法外,还有来自司徒正美.怿飞博客的几个方法 以下方法,由于测试环境的原因,IE系列只测试了IE9和IE6,以下所说的IE的支持性只是相对于IE9和IE6来说的: 一.元素的水平垂直居中: 第一种方法: <!doctype html> <html lang="en&

CSS制作水平垂直居中对齐

作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀,要正确的选择也是一件不容易的事情.我会将这几种方法一一介绍给大家,以供大家参考.或许对于我这样的初学者有一定的帮助. 用CSS来实现元素的垂直居中效果是件苦差事,虽然说实现方法有多种,但有很多方式在某些浏览器下可能无法正常的工作.接下来我们就一起来看看这些不同方法实现垂直居中的各自优点和其不足之处

转 CSS制作水平垂直居中对齐各种方法总结

作为前端攻城师,在制作Web页面时都有碰到CSS制作水平垂直居中,我想大家都有研究过或者写过,特别的其中的垂直居中,更是让人烦恼.这段时间,我收集了几种不同的方式制作垂直居中方法,但每种方法各有千秋呀,要正确的选择也是一件不容易的事情.我会将这几种方法一一介绍给大家,以供大家参考.或许对于我这样的初学者有一定的帮助. 用CSS来实现元素的垂直居中效果是件苦差事,虽然说实现方法有多种,但有很多方式在某些浏览器下可能无法正常的工作.接下来我们就一起来看看这些不同方法实现垂直居中的各自优点和其不足之处

CSS实现水平垂直居中小结

版权声明:本文为博主原创文章,未经博主允许不得转载. 水平居中 水平居中实现只要设置margin:0 auto;就可以实现 <code class="hljs xml has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined

CSS实现水平垂直居中方式

1.定位 核心代码实现请看示例代码中的注释: <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8" /> <title>CSS水平垂直居中实现方式--定位实现</title> <style type="text/css"> *{ margin: 0; padding: 0; } .p{ /*父元

CSS元素水平垂直居中方法总结(方法主要来自大漠以及张鑫旭博客)

以下方法,由于测试环境的原因,IE系列只测试了IE9和IE6,以下所说IE的支持性只是相对于IE9和IE6来说的: 一.元素的水平垂直居中: 第一种方法:<!doctype html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <style type="text/css"&g

css布局--水平垂直居中

1. 使用text-align 和 vertical-align 和 inline-block实现水平垂直居中 html <div class="parent"> <div class="child">使用vertical-align,text-align,inline-block实现水平垂直居中</div> </div> css .parent{ vertical-align: middle; text-align: