CSS 元素垂直居中的 6种方法
利用CSS进行元素的水平居中,比较简单,行级元素设置其父元素的text-align center,块级元素设置其本身的left 和 right margins为auto即可。本文收集了六种利用css进行元素的垂直居中的方法,每一种适用于不同的情况,在实际的使用过程中选择某一种方法即可。
Line-Height Method

试用:单行文本垂直居中
代码:
html
<div id="parent">
<div id="child">Text here</div>
</div>
css
#child {
line-height: 200px;
}
垂直居中一张图片,代码如下
html
<div id="parent">
<img src="data:image.png" alt="" />
</div>
css
#parent {
line-height: 200px;
}
#parent img {
vertical-align: middle;
}
CSS Table Method

适用:通用
代码:
html
<div id="parent">
<div id="child">Content here</div>
</div>
css
#parent {display: table;}
#child {
display: table-cell;
vertical-align: middle;
}
低版本 IE fix bug:
#child {
display: inline-block;
}
Absolute Positioning and Negative Margin

适用:块级元素
代码:
html
<div id="parent">
<div id="child">Content here</div>
</div>
css
#parent {position: relative;}
#child {
position: absolute;
top: %;
left: %;
height: %;
width: %;
margin: -% -%;
}
Absolute Positioning and Stretching

适用:通用,但在IE版本低于7时不能正常工作
代码:
html
<div id="parent">
<div id="child">Content here</div>
</div>
css
#parent {position: relative;}
#child {
position: absolute;
top: ;
bottom: ;
left: ;
right: ;
width: %;
height: %;
margin: auto;
}
Equal Top and Bottom Padding

适用:通用
代码:
html
<div id="parent">
<div id="child">Content here</div>
</div>
css
#parent {
padding: % ;
}
#child {
padding: % ;
}
Floater Div

适用:通用
代码:
html
<div id="parent">
<div id="floater"></div>
<div id="child">Content here</div>
</div>
css
#parent {height: 250px;}
#floater {
float: left;
height: %;
width: %;
margin-bottom: -50px;
}
#child {
clear: both;
height: 100px;
}
以上就是六种方法,可以在实际的使用过程中合理选择,参考文章《vertical-centering》。
CSS 元素垂直居中的 6种方法的更多相关文章
- [转]-CSS 元素垂直居中的6种方法
原文地址:http://blog.zhourunsheng.com/2012/03/css-%E5%85%83%E7%B4%A0%E5%9E%82%E7%9B%B4%E5%B1%85%E4%B8%AD ...
- 顽石系列:CSS实现垂直居中的五种方法
顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...
- CSS实现垂直居中的5种方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些方法在一些浏览器 ...
- 纯CSS实现垂直居中的几种方法
垂直居中是布局中十分常见的效果之一,为实现良好的兼容性,PC端实现垂直居中的方法一般是通过绝对定位,table-cell,负边距等方法.有了css3,针对移动端的垂直居中就更加多样化. 方法1:tab ...
- 纯CSS实现垂直居中的7种方法
今天申请博客通过了,给大家讲讲我所看到过的纯css实现垂直居中的各种方法.为什么要把它作为第一篇文章呢?因为这是我刚开始接触前端学到的对我最有用的知识,希望大家也可以从中获益! 在CSS中实现水平居中 ...
- css元素垂直居中的8中方法
1. 通过vertical-align:middle实现CSS垂直居中 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的 ...
- css 水平居中垂直居中的几种方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css一个元素垂直居中的6种方法
方法一: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...
- CSS水平垂直居中的几种方法2
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
随机推荐
- HTML输出 一 控制列背景颜色
#将需要读取的域名和端口列表保存在名为ports01.txt.ports02的文件中,文件与脚本位于相同目录下$CurrentPath = $MyInvocation.MyCommand.Path.s ...
- Codeforces Beta Round #85 (Div. 1 Only) A. Petya and Inequiations 贪心
A. Petya and Inequiations Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Date Format, 时间戳格式化
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- dl-ssl.google.com
转载:http://jingyan.baidu.com/article/64d05a02752300de55f73b99.html 搭建Android就会用到Android SDK,而安装SDK有个恶 ...
- 小白日记10:kali渗透测试之端口扫描-UDP、TCP、僵尸扫描、隐蔽扫描
端口扫描 二三四层发现的目的只是为了准确发现所有活着主机IP,确定攻击面,端口扫描即发现攻击点,发现开放端口.端口对应网络服务及应用端程序,服务端程序的漏洞通过端口攻入.[所有的扫描结果,都不要完全相 ...
- Team Queue
Team Queue Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2471 Accepted: 926 Descrip ...
- Direct3D11-1 初始化
在使用一个东西之前,我们需要初始化他,好比汽车加油,手机充电.于是我们采取平时的编码习惯,试图写下如下代码 Direct3D11 _direct3d11; 事实上,我们 ...
- HTML+CSS实例——漂亮的查询部件(一)
一.参考网址:www.kuhnsjewelers.com 二.效果: 三.HTML <div id="search-box"> <asp:TextBox ID=& ...
- cx_Oracle安装说明
简介 cx_Oracle是用python连接oracle的驱动模块,参考文章: [cx_Oracle文档](http://cx-oracle.sourceforge.net/html/index.ht ...
- next_permutation()—遍历全排列
# next_permutation()--遍历全排列 template <class BidirectionalIterator> bool next_permutation (Bidi ...