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

用 CSS 实现垂直居中并不容易。有些方法在一些浏览器中无效。下面我们看一下使对象垂直集中的5种不同方法,以及它们各自的优缺点。(可以看看测试页面,有简短解释。)

方法一

这个方法把一些 div 的显示方式设置为表格,因此我们可以使用表格的 vertical-align property 属性。

<div id="wrapper">
<div id="cell">
<div class="content">
Content goes here</div>
</div>
</div>
#wrapper {display:table;}
#cell {display:table-cell; vertical-align:middle;}

优点:
content 可以动态改变高度(不需在 CSS 中定义)。当 wrapper 里没有足够空间时, content 不会被截断

~~#cell{display:table-cell; vertical-align:middle;} 内容无需固定高度 经测试 ie6不支持display:table-cell;

缺点:
Internet Explorer(甚至 IE8 beta)中无效,许多嵌套标签(其实没那么糟糕,另一个专题)

方法二:

这个方法使用绝对定位的 div,把它的 top 设置为 50%,top margin 设置为负的 content 高度。这意味着对象必须在 CSS 中指定固定的高度。

因为有固定高度,或许你想给 content 指定 overflow:auto,这样如果 content 太多的话,就会出现滚动条,以免content 溢出。

<div class="content">
Content goes here</div>
#content {
position:absolute;
top:50%;
height:240px;
margin-top:-120px; /* negative half of the height */
}

优点:
适用于所有浏览器
不需要嵌套标签

~~position:absolute; top:50%; height:200px; margin-top:-100px; 绝对定位到50%的位置 再向上调整高度的1/2, 简单容易理解 兼容性好,但是内容需要有固定高度,若内容的多少不确定的话 就不适用了。

缺点:
没有足够空间时,content 会消失(类似div 在 body 内,当用户缩小浏览器窗口,滚动条不出现的情况)

方法三

这种方法,在 content 元素外插入一个 div。设置此 div height:50%; margin-bottom:-contentheight;。
content 清除浮动,并显示在中间。

<div id="floater"></div>
<div id="content">
Content here
</div>
#floater	{float:left; height:50%; margin-bottom:-120px;}
#content {clear:both; height:240px; position:relative;}

优点:
适用于所有浏览器
没有足够空间时(例如:窗口缩小) content 不会被截断,滚动条出现

~~~在内容div和容器div之间插入   一个高度 内容div和容器div高度差1/2的div,原理跟 方法2差不多;方法2用的是top:50%,这里没有绝对定位 用一个浮动div占据1/2高度差的空间

缺点:
唯一我能想到的就是需要额外的空元素了(也没那么糟,又是另外一个话题)

方法四

这个方法使用了一个 position:absolute,有固定宽度和高度的 div。这个 div 被设置为 top:0; bottom:0;。但是因为它有固定高度,其实并不能和上下都间距为 0,因此 margin:auto; 会使它居中。使用 margin:auto;使块级元素垂直居中是很简单的。

<div id="content">
Content here</div>
#content {
position:absolute;
top:0;
bottom:0;
left:0;
right:0;
margin:auto;
height:240px;
width:70%;
}

优点:
简单

缺点:
IE(IE8 beta)中无效
无足够空间时,content 被截断,但是不会有滚动条出现

~~~position:absolute; top:0; bottom:0; margin:auto; height:200px; width:200px;

方法五

这个方法只能将单行文本置中。只需要简单地把 line-height 设置为那个对象的 height 值就可以使文本居中了。

<div id="content">
Content here</div>
#content {height:100px; line-height:100px;}

优点:
适用于所有浏览器
无足够空间时不会被截断

~~~单行文本垂直居中 height:100px; line-height:100px;  实际上仅仅 line-height:100px;已经可以了。

缺点:
只对文本有效(块级元素无效)
多行时,断词比较糟糕

这个方法在小元素上非常有用,例如使按钮文本或者单行文本居中。

哪个方法?

我最喜欢的是方法三,缺点不多。因为 content 会清除浮动,所以可以在它上面放置别的元素,并且当窗口缩放时,
居中的 content 不会把另外的元素盖住。看例子

<div id="top">
<h1>Title</h1>
</div>
<div id="content">
Content Here</div>
#floater	{float:left; height:50%; margin-bottom:-120px;}
#top {float:right; width:100%; text-align:center;}
#content {clear:both; height:240px; position:relative;}

>> 个人觉得比较好的方法 不过要根据具体容器的高度 做一个换算

~~~ font-size实现多行文本垂直居中 跟裸文字垂直对齐

<div class="p"><div class="mtxt">dddsojsdoj</div></div>

.p{font-size:10em; height:1.14em; line-height:1;}

.mtxt{font-size:0.1em; display:inline-block; vertical-align:middle; /* 若.mtxt为块元素 加上这个 兼容ie6/7  */    *display:inline; *zoom:1; }

-----------------------------------------------------

<div class="p"><span class="mtxt">dddsojsdoj</span></div>

.p{font-size:10em; height:1.14em; line-height:1;}

.mtxt{font-size:0.1em; display:inline-block; vertical-align:middle; }

>> ~~~感谢群里网友提供一个 更好的方法 内容高度不固定 垂直居中 ** 推荐 **

<style>
span {width:0; height:100%;display:inline-block; vertical-align:middle;}
.in {display:inline-block; *display:inline; *zoom:1; vertical-align:middle; white-space: normal;}
.h200 {height:200px; border:1px solid red;}
</style>
<div class="h200" style="height:200px;">
<span></span>
<div class="in">sdfsfjklsjfdklsjfdsdfsfjk<br>
sdfsfjklsjfdklsjfdsdfsfjk<br>
sdfsdf</div>
</div>

现在你知道是怎么回事了,现在我们开始创建一个简单但是有趣的网站。最终的样子是这样的:

步骤一
以语义化标签开始是很好的。下面是我们的页面构成:

#floater/*把 content 置中*/
#contred/*centre 盒*/
#side
#logo
#nav/*无序列表*/
#content
#bottom/*放置版权等*/

这是我用到的 xhtml 代码:

A Centred Company
<div id="centered">
<div id="side">
<div id="logo">
<strong><span>A</span> Company</strong></div>
<ul id="nav">
<li><a href="#">Home</a></li>
<li><a href="#">Products</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</div>
<div id="content">
<h1>Page Title</h1>
Holisticly re-engineer value-added outsourcing after process-centric collaboration and idea-sharing.
Energistically simplify impactful niche markets via enabled imperatives.
Holisticly predominate premium innovation after compelling scenarios.
Seamlessly recaptiualize high standards in human capital with leading-edge manufactured products.
Distinctively syndicate standards compliant schemas before robust vortals.
Uniquely recaptiualize leveraged web-readiness vis-a-vis out-of-the-box information.
<h2>Heading 2</h2>
Efficiently embrace customized web-readiness rather than customer directed processes.
Assertively grow cross-platform imperatives vis-a-vis proactive technologies.
Conveniently empower multidisciplinary meta-services without enterprise-wide interfaces.
Conveniently streamline competitive strategic theme areas with focused e-markets.
Phosfluorescently syndicate world-class communities vis-a-vis value-added markets.
Appropriately reinvent holistic services before robust e-services.</div>
</div>
<div id="bottom">
Copyright notice goes here</div>

步骤二:
现在我们开始用一些基本的 CSS 来给页面添加样式。把以下代码放入在我们的 html 页面顶部被引入的 style.css。

html, body {
margin:0; padding:0;
height:100%;
}
 
body {
background:url('page_bg.jpg') 50% 50% no-repeat #FC3;
font-family:Georgia, Times, serifs;
}
 
#floater {
position:relative; float:left;
height:50%; margin-bottom:-200px;
width:1px;
}
 
#centered {
position:relative; clear:left;
height:400px; width:80%; max-width:800px; min-width:400px;
margin:0 auto;
background:#fff;
border:4px solid #666;
}
 
#bottom {
position:absolute;
bottom:0; right:0;
}
 
#nav {
position:absolute; left:0; top:0; bottom:0; right:70%;
padding:20px; margin:10px;
}
 
#content {
position:absolute; left:30%; right:0; top:0; bottom:0;
overflow:auto; height:340px;
padding:20px; margin:10px;
}

在我们能够把 content 垂直居中之前, body 和 html 应该被拉伸到 100% 的高度。由于 height
在 padding 和 margin 之内,所以我们要把它们设成 0 以防止因为很小的 margin 出现滚动条。

floater 的 margin-bottom 是 content 高度(400px)的一半, -200px。

现在可以看到一下效果:

#centred 的宽度为 80%。这可以市网页随着显示器的大小而变化。一般称作流体布局。设置 min-width 和
max-width 以避免网页过大或者过小。 但是 IE 不支持 min/max-width。显然可以用固定宽度来代替。

因为 #centred 是相对定位的,在它里面我们可以用绝对定位来定位元素。设置 #content 的 overflow:auto;
以避免滚动条的出现。IE 不怎么喜欢 overflow:auto; 除非我们指定高度(不是 top 和 bottom 的定位,也不是 %)
因此我们给它指定高度。

步骤三
最后要做的就是再添加点样式,让页面好看点。从目录开始吧。

#nav ul {
list-style:none;
padding:0; margin:20px 0 0 0; text-indent:0;
}
 
#nav li {
padding:0; margin:3px;
}
 
#nav li a {
display:block; background-color:#e8e8e8;
padding:7px; margin:0;
text-decoration:none; color:#000;
border-bottom:1px solid #bbb;
text-align:right;
}
 
#nav li a::after {
content:'»'; color:#aaa; font-weight:bold;
display:inline; float:right;
margin:0 2px 0 5px;
}
 
#nav li a:hover, #nav li a:focus {
background:#f8f8f8;
border-bottom-color:#777;
}
 
#nav li a:hover::after {
margin:0 0 0 7px; color:#f93;
}
 
#nav li a:active {
padding:8px 7px 6px 7px;
}

需要注意的是 #centred 的圆角。 CSS3 中,应该有 border-radius 属性来设定圆角的半径(可参考 CSS3之旅: border-radius(圆角) – 糖伴西红柿)。现在的
流行的浏览器都还不支持,除非用 -moz(Molilla Firefox) 或者 -webit(Safari/Webkit) 前缀.

兼容性注意事项
如你所想,IE 是唯一添麻烦的浏览器。
#floater 必须指定宽度,否则在任意版本 IE 中,它都啥也不干
IE 6 中目录被周围太多的空间打断
IE 8 有多余空间(作者遗漏)

更多的想法
利用居中的网页可以做很多有意思的事情。我在重新设计 SWFObject Generator 2.0(使用 SWFObject2.0 生成代码)使用了这个想法。这里有另外的一个想法。

资料
以下是我参考的一些资料,推荐阅读。

Understanding vertical-align, or “How (Not) To Vertically Center Content”
Vertical centering using CSS
Vertical Centering in CSS

糖伴西红柿说:
水平居中经常用,其实垂直居中也很有用的。平时用的最多的应该是方法五了,算是个小技巧吧。

转: css实现垂直居中的方法的更多相关文章

  1. CSS中垂直居中的方法

    昨天总结了css中水平居中的方法,今天来总结一下css中实现垂直居中的方法. line-height line-height用于实现单行文本的垂直居中,如下图中,我们要求单行文本垂直居中,只需要将di ...

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

    原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...

  3. 介绍一种css水平垂直居中的方法(非常好用!)

    这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...

  4. css水平垂直居中的方法与 vertical-align 的用法

    前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 1. 已知元素宽度 方法一:已知宽高,可以用position定位 + margin负值的方法 : 绝对定位 ...

  5. css实现垂直居中的方法整理

    1.表格布局法.(利用表格的显示模式)需要用到一些冗余的 HTML 元素,因此这里不多介绍. 2.行内块法.也不作讨论,因为在我看来这种方法 hack 的味道很浓. 如果你有兴趣,可以去看看 Chri ...

  6. css 水平垂直居中的方法总结

    在项目中经常会遇到设置元素水平垂直居中的需求.而且具体的场景也不同,所以将个人总结的方法做个汇总,希望对浏览者有用. 以下所举的例子都以一个html为准,这里规定好一些公用样式. body { bac ...

  7. CSS水平垂直居中常见方法总结

    1.元素水平居中 当然最好使的是: margin: 0 auto; 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是行内元素吧,行内元素和块元素的区 ...

  8. CSS水平垂直居中常见方法总结2

    1.文本水平居中line-height,text-align:center(文字)元素水平居中 margin:0 auo 方案1:position 元素已知宽度 父元素设置为:position: re ...

  9. [css]水平垂直居中的方法

    1.top:cale(50% - 2rem); left:cale(50% - 2rem);

随机推荐

  1. C#实现 ffmpeg视频转码、播放

    近来公司项目要求实现全景相机的视频截取,但是截取的视频需求转码上传.经过研究采用ffmpeg转码,奉上一个详细介绍的博文: 最简单的基于FFMPEG的转码程序 主要是转码的操作过程,能够实现了从相机获 ...

  2. iOS开发宝典:String用法大全

    一.NSString 创建字符串.  NSString *astring = @"This is a String!"; 创建空字符串,给予赋值.  NSString *astri ...

  3. Jquery简单动画的实现记录

    <div style="background:#98bf21;height:100px;width:100px;"> //从元素当前所在位置,往下消失 $(docume ...

  4. 经典mssql语句大全

    一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql server--- 创建 备 ...

  5. HDU2546:饭卡(01背包)

    HDU2546:饭卡 http://acm.hdu.edu.cn/showproblem.php?pid=2546 当我们遇到问题选择物体的价值和顺序相关时就需要,排完序后对其01处理.这题因为当我们 ...

  6. C++数组与指针

    指向数组元素的指针 一个变量有地址,一个数组包含若干元素,每个数组元素都在内存中占用存储单元,它们都有相应的地址.指针变量既然可以指向变量,当然也可以指向数组元素(把某一元素的地址放到一个指针变量中) ...

  7. relative、absolute和float

    relative.absolute和float   position:relative和position:absolute都可以改变元素在文档中的位置,都能激活元素的left.top.right.bo ...

  8. 转:说说angularjs中的$parse和$eval

    说说AngularJS中的$parse和$eval AngularJS的初学者常常会对$parse和$eval两个内建服务感到有些困惑,今天我们就来说说AngularJS中的$parse和$eval. ...

  9. BZOJ 3038 上帝造题的七分钟2 (并查集+树状数组)

    题解:同 BZOJ 3211 花神游历各国,需要注意的是需要开long long,还有左右节点需要注意一下. #include <cstdio> #include <cmath> ...

  10. 用git上传项目到github

    1 git  clone  github仓库地址 2 git add . 3 git  commit -m "changes log" 4 git remote add origi ...