垂直居中的方法

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

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

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


1.jpg


方法一 

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


  1. <div id="wrapper">
  2. <div id="cell">
  3. <div class="content">
  4. Content goes here</div>
  5. </div>
  6. </div>

  7. #wrapper {display:table;}
  8. #cell {display:table-cell; vertical-align:middle;}
复制代码

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

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


方法二:

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

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


  1. <div class="content">Content goes here</div>

  2. #content {
  3. position:absolute;
  4. top:50%;
  5. height:240px;
  6. margin-top:-120px; /* negative half of the height */
  7. }
复制代码

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

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


方法三 

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

  1. <div id="floater"></div>
  2. <div id="content">
  3. Content here
  4. </div>

  5. #floater {float:left; height:50%; margin-bottom:-120px;}
  6. #content {clear:both; height:240px; position:relative;}
复制代码

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

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


方法四 

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


  1. <div id="content">Content here</div>

  2. #content {
  3. position:absolute;
  4. top:0;
  5. bottom:0;
  6. left:0;
  7. right:0;
  8. margin:auto;
  9. height:240px;
  10. width:70%;
  11. }
复制代码

优点:
简单

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


方法五 

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


  1. <div id="content">Content here</div>

  2. #content {height:100px; line-height:100px;}
复制代码

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

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

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


哪个方法? 

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


  1. <div id="top">
  2. <h1>Title</h1>
  3. </div>
  4. <div id="content">Content Here</div>

  5. #floater {float:left; height:50%; margin-bottom:-120px;}
  6. #top {float:right; width:100%; text-align:center;}
  7. #content {clear:both; height:240px; position:relative;}
复制代码

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


2.jpg


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

  1. #floater/*把 content 置中*/
  2. #contred/*centre 盒*/
  3. #side
  4. #logo
  5. #nav/*无序列表*/
  6. #content
  7. #bottom/*放置版权等*/
复制代码

这是我用到的 xhtml 代码:

  1. A Centred Company
  2. <div id="centered">
  3. <div id="side">
  4. <div id="logo">
  5. <strong><span>A</span> Company</strong></div>
  6. <ul id="nav">
  7. <li><a href="#">Home</a></li>
  8. <li><a href="#">Products</a></li>
  9. <li><a href="#">Blog</a></li>
  10. <li><a href="#">Contact</a></li>
  11. <li><a href="#">About</a></li>
  12. </ul>
  13. </div>
  14. <div id="content">
  15. <h1>Page Title</h1>
  16. Holisticly re-engineer value-added outsourcing after process-centric collaboration and idea-sharing.
  17. Energistically simplify impactful niche markets via enabled imperatives.
  18. Holisticly predominate premium innovation after compelling scenarios.
  19. Seamlessly recaptiualize high standards in human capital with leading-edge manufactured products.
  20. Distinctively syndicate standards compliant schemas before robust vortals.
  21. Uniquely recaptiualize leveraged web-readiness vis-a-vis out-of-the-box information.
  22. <h2>Heading 2</h2>
  23. Efficiently embrace customized web-readiness rather than customer directed processes.
  24. Assertively grow cross-platform imperatives vis-a-vis proactive technologies.
  25. Conveniently empower multidisciplinary meta-services without enterprise-wide interfaces.
  26. Conveniently streamline competitive strategic theme areas with focused e-markets.
  27. Phosfluorescently syndicate world-class communities vis-a-vis value-added markets.
  28. Appropriately reinvent holistic services before robust e-services.</div>
  29. </div>
  30. <div id="bottom">
  31. Copyright notice goes here</div>
复制代码

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

  1. html, body {
  2. margin:0; padding:0;
  3. height:100%;
  4. }

  5. body {
  6. background:url('page_bg.jpg') 50% 50% no-repeat #FC3;
  7. font-family:Georgia, Times, serifs;
  8. }

  9. #floater {
  10. position:relative; float:left;
  11. height:50%; margin-bottom:-200px;
  12. width:1px;
  13. }

  14. #centered {
  15. position:relative; clear:left;
  16. height:400px; width:80%; max-width:800px; min-width:400px;
  17. margin:0 auto;
  18. background:#fff;
  19. border:4px solid #666;
  20. }

  21. #bottom {
  22. position:absolute;
  23. bottom:0; right:0;
  24. }

  25. #nav {
  26. position:absolute; left:0; top:0; bottom:0; right:70%;
  27. padding:20px; margin:10px;
  28. }

  29. #content {
  30. position:absolute; left:30%; right:0; top:0; bottom:0;
  31. overflow:auto; height:340px;
  32. padding:20px; margin:10px;
  33. }
复制代码

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

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

现在可以看到一下效果:


3.jpg


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

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

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


  1. #nav ul {
  2. list-style:none;
  3. padding:0; margin:20px 0 0 0; text-indent:0;
  4. }

  5. #nav li {
  6. padding:0; margin:3px;
  7. }

  8. #nav li a {
  9. display:block; background-color:#e8e8e8;
  10. padding:7px; margin:0;
  11. text-decoration:none; color:#000;
  12. border-bottom:1px solid #bbb;
  13. text-align:right;
  14. }

  15. #nav li a::after {
  16. content:'?'; color:#aaa; font-weight:bold;
  17. display:inline; float:right;
  18. margin:0 2px 0 5px;
  19. }

  20. #nav li a:hover, #nav li a:focus {
  21. background:#f8f8f8;
  22. border-bottom-color:#777;
  23. }

  24. #nav li a:hover::after {
  25. margin:0 0 0 7px; color:#f93;
  26. }

  27. #nav li a:active {
  28. padding:8px 7px 6px 7px;
  29. }
复制代码

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


4.jpg


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


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


div 垂直居中的多种方法详细介绍

在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?接下来为您详细介绍
在说到这个问题的时候,也许有人会问CSS中不是有vertical-align属性来设置垂直居中的吗?即使是某些浏览器不支持我只需做少许的CSS Hack技术就可以啊!所以在这里我还要啰嗦两句,CSS中的确是有vertical-align属性,但是它只对(X)HTML元素中拥有valign特性的元素才生效,例如表格元素中的<td>、<th>、<caption>等,而像<div>、<span>这样的元素是没有valign特性的,因此使用vertical-align对它们不起作用。 
相关教程:div水平居中的N种方法

一、单行垂直居中 
如果一个容器中只有一行文字,对它实现居中相对比较简单,我们只需要设置它的实际高度height和所在行的高度line-height相等即可。 
如: 

复制代码
代码如下:

div { 
height:25px; 
line-height:25px; 
overflow:hidden; 


这段代码很简,后面使用overflow:hidden的设置是为了防止内容超出容器或者产生自动换行,这样就达不到垂直居中效果了。更多CSS教 
程。 

复制代码
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 单行文字实现垂直居中 </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
body { font-size:12px;font-family:tahoma;} 
div { 
height:25px; 
line-height:25px; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 

</style> 
</head> 
<body> 
<div>现在我们要使这段文字垂直居中显示!</div> 
</body> 
</html> 

二、多行未知高度文字的垂直居中  
如果一段内容,它的高度是可变的那么我们就可以使用上一节讲到的实现水平居中时使用到的最后一种方法,就是设定Padding,使上下的 
padding值相同即可。同样的,这也是一种“看起来”的垂直居中方式,它只不过是使文字把<div>完全填充的一种访求而已。可以使用类似下 
面的代码: 

复制代码
代码如下:

div { 
padding:25px; 


这种方法的优点就是它可以在任何浏览器上运行,并且代码很简单,只不过这种方法应用的前提就是容器的高度必须是可伸缩的。 

复制代码
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 多行文字实现垂直居中 </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
body { font-size:12px;font-family:tahoma;} 
div { 
padding:25px; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 

</style> 
</head> 
<body> 
<div><pre>现在我们要使这段文字垂直居中显示! 
div { 
padding:25px; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 

</pre></div> 
</body> 
</html> 

三、多行文本固定高度的居中  
在本文的一开始,我们已经说过CSS中的vertical-align属性只会对拥有valign特性的(X)HTML标签起作用,但是在CSS中还有一个display 
属性能够模拟<table>,所以我们可以使用这个属性来让<div>模拟<table>就可以使用vertical-align了。注意,display:table和 
display:table-cell的使用方法,前者必须设置在父元素上,后者必须设置在子元素上,因此我们要为需要定位的文本再增加一个<div>元素: 

复制代码
代码如下:

div#wrap { 
height:400px; 
display:table; 

div#content { 
vertical-align:middle; 
display:table-cell; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 



复制代码
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 多行文字实现垂直居中 </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
body { font-size:12px;font-family:tahoma;} 
div#wrap { 
height:400px; 
display:table; 

div#content { 
vertical-align:middle; 
display:table-cell; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 

</style> 
</head> 
<body> 
<div id="wrap"> 
<div id="content"><pre>现在我们要使这段文字垂直居中显示! Webjx.Com 
div#wrap { 
height:400px; 
display:table; 

div#content { 
vertical-align:middle; 
display:table-cell; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 

</pre></div> 
</div> 
</body> 
</html> 

这个方法应该是很理想了,但是不幸的是Internet Explorer 6 并不能正确地理解display:table和display:table-cell,因此这种方法在 
Internet Explorer 6及以下的版本中是无效的。嗯,这让人很郁闷!不过我们还其它的办法 
四、在Internet Explorer中的解决方案  
在Internet Explorer 6及以下版本中,在高度的计算上存在着缺陷的。在Internet Explorer 6中对父元素进行定位后,如果再对子元素 
进行百分比计算时,计算的基础似乎是有继承性的(如果定位的数值是绝对数值没有这个问题,但是使用百分比计算的基础将不再是该元素的 
高度,而从父元素继承来的定位高度)。例如,我们有下面这样一个(X)HTML代码段: 

复制代码
代码如下:

<div id="wrap"> 
<div id="subwrap"> 
<div id="content"> 
</div> 
</div> 
</div> 

如果我们对subwrap进行了绝对定位,那么content也会继承了这个这个属性,虽然它不会在页面中马上显示出来,但是如果再对content进 
行相对定位的时候,你使用的100%分比将不再是content原有的高度。例如,我们设定了subwrap的position为40%,我们如果想使content的上 
边缘和wrap重合的话就必须设置top:-80%;那么,如果我们设定subwrap的top:50%的话,我们必须使用100%才能使content回到原来的位置上去 
,但是如果我们把content也设置50%呢?那么它就正好垂直居中了。所以我们可以使用这中方法来实现Internet Explorer 6中的垂直居中: 

复制代码
代码如下:

div#wrap { 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:400px; 
position:relative; 

div#subwrap { 
position:absolute; 
border:1px solid #000; 
top:50%; 

div#content { 
border:1px solid #000; 
position:relative; 
top:-50%; 


当然,这段代码只能在Internet Exlporer 6等计算存在问题的浏览器中才会有作用。(不过我不解,我查阅了很多文章,不知道是因为出 
处相同还是什么原因,似乎很多人都不愿意去解释Internet Exlporer 6中这这个Bug的原理,我也只是了解了一点皮毛,还要再研究) 

复制代码
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 多行文字实现垂直居中 </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
body { font-size:12px;font-family:tahoma;} 
div#wrap { 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:400px; 
position:relative; 

div#subwrap { 
position:absolute; 
top:50%; 

div#content { 
position:relative; 
top:-50%; 

</style> 
</head> 
<body> 
<div id="wrap"> 
<div id="subwrap"> 
<div id="content"><pre>现在我们要使这段文字垂直居中显示! 
div#wrap { 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:500px; 
position:relative; 

div#subwrap { 
position:absolute; 
border:1px solid #000; 
top:50%; 

div#content { 
border:1px solid #000; 
position:relative; 
top:-50%; 
}</pre> 
</div> 
</div> 
</div> 
</body> 
</html> 

五、完美的解决方案  
那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSS hack的知识。对于如果使用CSS Hack来区分浏览器,你可 
以参考这篇“简单CSS hack:区分IE6、IE7、IE8、Firefox、Opera”: 

复制代码
代码如下:

div#wrap { 
display:table; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:400px; 
_position:relative; 
overflow:hidden; 

div#subwrap { 
vertical-align:middle; 
display:table-cell; 
_position:absolute; 
_top:50%; 

div#content { 
_position:relative; 
_top:-50%; 


至此,一个完美的居中方案就产生了。 

复制代码
代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<title> 多行文字实现垂直居中 </title> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<style type="text/css"> 
body { font-size:12px;font-family:tahoma;} 
div#wrap { 
display:table; 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:400px; 
_position:relative; 
overflow:hidden; 

div#subwrap { 
vertical-align:middle; 
display:table-cell; 
_position:absolute; 
_top:50%; 

div#content { 
_position:relative; 
_top:-50%; 

</style> 
</head> 
<body> 
<div id="wrap"> 
<div id="subwrap"> 
<div id="content"><pre>现在我们要使这段文字垂直居中显示! 
div#wrap { 
border:1px solid #FF0099; 
background-color:#FFCCFF; 
width:760px; 
height:500px; 
position:relative; 

div#subwrap { 
position:absolute; 
border:1px solid #000; 
top:50%; 

div#content { 
border:1px solid #000; 
position:relative; 
top:-50%; 
}</pre> 
</div> 
</div> 
</div> 
</body> 
</html> 

p.s. 垂直居中vertical-align的值是middle,而水平居中align的值是center,虽然同是居中但关键字不同
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值