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 ...
随机推荐
- python二叉树的遍历,递归和非递归及相关其它
# encoding=utf-8class node(object): def __init__(self,data,left=None,right=None): self.data = data s ...
- Contiki Ctimer模块
Ctimer 提供和Etimer类似的功能,只是Ctimer是在一段时间后调用回调函数,没有和特定进程相关联. 而Etimer是在一段时间后发送PROCESS_EVENT_TIMER事件给特定的进程. ...
- java面试题05
1.写一个冒泡排序的算法 升序排列: public static void main(String[] args) { int score[] = { 67, 20, 75, 87, 89, 90, ...
- 递归/非递归----python深度遍历二叉树(前序遍历,中序遍历,后序遍历)
递归代码:递归实现很简单 '二叉树结点类' class TreeNode: def __init__(self, x): self.val = x self.left = None self.righ ...
- BZOJ_3159_决战
题目链接 分析: 我使用树剖+splay维护这个东西. 对每条重链维护一棵splay,链加和查询正常做,剩下的链反转如下. 由于一定是深度递增的一条链,我们树剖将它分成从左到右log个区间,提取出对应 ...
- 51nod_1236_序列求和 V3 _组合数学
51nod_1236_序列求和 V3 _组合数学 Fib(n)表示斐波那契数列的第n项,Fib(n) = Fib(n-1) + Fib(n-2).Fib(0) = 0, Fib(1) = 1. (1, ...
- 变废为宝,用旧电脑自己DIY组建 NAS 服务器
i17986 出品,必属佳作! 前言: 老外不喜欢升级硬件和软件,大家应该都知道.我昨天无意看到 FreeNAS 自述文件,这个系统可以让你使用旧的计算机硬件,于是我决定这么做.垃圾电脑你怎么能没有, ...
- AtCoder Regular Contest 072 E:Alice in linear land
题目传送门:https://arc072.contest.atcoder.jp/tasks/arc072_c 题目翻译 给你一个数组\(D\),然后给你一个操作序列\(d\),每次操作可以将\(D\) ...
- 如何使用代码美化器Uncrustify (How to use code beautifier Uncrustify)
1.下载NodePad++, 2.选择菜单Plugins/Plugin Manager/Show Plugin Manager 3.勾选UniversalIndentGUI,点击Install 4.下 ...
- WPF学习系列之五(WPF控件)
控件: 1.内容控件------这些控件能够包含嵌套的元素,为它们提供几乎无限的显示能力.内容控件包括Lable,Button 以及ToolTip类. 内容控件是更特殊的控件类型,它们可以包含( ...