利用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种方法的更多相关文章

  1. [转]-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 ...

  2. 顽石系列:CSS实现垂直居中的五种方法

    顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...

  3. CSS实现垂直居中的5种方法

    利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些方法在一些浏览器 ...

  4. 纯CSS实现垂直居中的几种方法

    垂直居中是布局中十分常见的效果之一,为实现良好的兼容性,PC端实现垂直居中的方法一般是通过绝对定位,table-cell,负边距等方法.有了css3,针对移动端的垂直居中就更加多样化. 方法1:tab ...

  5. 纯CSS实现垂直居中的7种方法

    今天申请博客通过了,给大家讲讲我所看到过的纯css实现垂直居中的各种方法.为什么要把它作为第一篇文章呢?因为这是我刚开始接触前端学到的对我最有用的知识,希望大家也可以从中获益! 在CSS中实现水平居中 ...

  6. css元素垂直居中的8中方法

    1. 通过vertical-align:middle实现CSS垂直居中 通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的 ...

  7. css 水平居中垂直居中的几种方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. css一个元素垂直居中的6种方法

    方法一: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  9. CSS水平垂直居中的几种方法2

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

随机推荐

  1. Android中图表AChartEngine学习使用与例子

    很多时候项目中我们需要对一些统计数据进行绘制表格,更多直观查看报表分析结果.基本有以下几种方法: 1:可以进行android  api进行draw这样的话,效率比较低 2:使用开源绘表引擎,这样效率比 ...

  2. POJ 2420 A Star not a Tree? 爬山算法

    B - A Star not a Tree? Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/co ...

  3. JellyViewPager

    https://github.com/jfeinstein10/JazzyViewPager https://github.com/chiemy/JellyViewPager JellyViewPag ...

  4. 编写3个不同版本的程序,令其均能输出ia的元素

    #include<iostream> #include<vector> #include<string> using namespace std; int main ...

  5. Stupid Tower Defense

    Problem Description FSF is addicted to a stupid tower defense game. The goal of tower defense games ...

  6. Android(java)学习笔记98:关于string类型的面试题

    package cn.itcast_02; /* * String s = new String(“hello”)和String s = “hello”;的区别? * 有.前者会创建2个对象,后者创建 ...

  7. 无法挂载 “7.9 GB Filesystem”.

    有个8G的U盘,格式化成exfat格式.插入电脑后点击盘符,弹出错误提示: 无法挂载 “7.9 GB Filesystem”. Error mounting: mount exited with ex ...

  8. 用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败/OLE DB 错误: OLE DB 或 ODBC 错误 :

    用户 'NT AUTHORITY\NETWORK SERVICE' 登录失败/OLE DB 错误: OLE DB 或 ODBC 错误 : 2012-2-23 上午 ,弄SAAS时,发现在生成多维数据集 ...

  9. 给jdk写注释系列之jdk1.6容器(10)-Stack&Vector源码解析

    前面我们已经接触过几种数据结构了,有数组.链表.Hash表.红黑树(二叉查询树),今天再来看另外一种数据结构:栈.      什么是栈呢,我就不找它具体的定义了,直接举个例子,栈就相当于一个很窄的木桶 ...

  10. ZOJ 3209 Treasure Map (Dancing Links)

    Treasure Map Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit S ...