CSS中怎样设置才能使对象中的文字垂直居中于对象,这个问题也是捆扰很多朋友的难题。现在网上的很多代码都做不到浏览器兼容。我把网上有关的方法整理了一下,做了些改动,完全可以兼容各主流浏览器。
下面具体说一下在不同情况下垂直居中的方法。
一、一行文字垂直居中
看一下下面的代码:
Code
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
5 <title>垂直居中</title>
6 <script type="text/javascript" language="javascript">
7 function sel(id) {switch(id) {case "1":document.getElementById("sub").style.lineHeight = "normal";break;case "2":document.getElementById("sub").style.lineHeight = "20px";break;case "3":document.getElementById("sub").style.lineHeight = "28px";break;}}
8 </script>
9 <style type="text/css">
10 #all {
11 width:240px;
12 padding:10px;
13 font-size:12px;
14 color:#FFF;
15 background-color:#CCC;
16 }
17 #sub {
18 width:230px;
19 padding:0 5px;
20 height:20px;
21 overflow:hidden;
22 background-color:#F90;
23 }
24 #sel {
25 margin-top:5px;
26 }
27 select {
28 width:260px;
29 }
30 </style>
31 </head>
32 <body>
33 <div id="all">
34 <div id="sub">一行文字垂直居中,此对象高度为20px</div>
35 </div>
36 <div id="sel"><select onchange="sel(this.value)"><option value="1">默认值,设置行高为:normal</option><option value="2">设置行高和对象高度相同:20px</option><option value="3">设置行高比对象的高度大:28px</option></select></div>
37 </body>
38 </html
方法:line-height:20px。设置相同的行高和对象高度。
说明:这种设置简单又兼容各种浏览器,支持内联对象。但如果是固定高度的对象,只能显示一行。当有两行文字时,如果不设置“overflow:hidden”会把对象撑开。
二、多行文字,且高度自适应
请看下面的代码:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
5 <title>垂直居中</title>
6 <script type="text/javascript" language="javascript">
7 function sel(id) {switch(id) {case "1":document.getElementById("sub").innerHTML = "三行文字垂直居中,设置相同的内上边距padding-top和内下边距padding-bottom就可以。";break;case "2":document.getElementById("sub").innerHTML = "两行文字垂直居中,设置相同的内上下边距padding-top和padding-bottom就可以。";break;case "3":document.getElementById("sub").innerHTML = "一行文字垂直居中,内上下边距相同。";break;}}
8 </script>
9 <style type="text/css">
10 #all {
11 float:left;
12 width:240px;
13 padding:10px;
14 font-size:12px;
15 color:#FFF;
16 background-color:#CCC;
17 }
18 #sub {
19 float:left;
20 width:230px;
21 padding:10px 5px;
22 background-color:#F90;
23 }
24 #sel {
25 clear:both;
26 padding-top:5px;
27 width:260px;
28 }
29 select {
30 width:260px;
31 }
32 </style>
33 </head>
34 <body>
35 <div id="all">
36 <div id="sub">三行文字垂直居中,设置相同的内上边距padding-top和内下边距padding-bottom就可以。</div>
37 </div>
38 <div id="sel"><select onchange="sel(this.value)"><option value="1">三行文字垂直居中</option><option value="2">两行文字垂直居中</option><option value="3">一行文字垂直居中</option></select></div>
39 </body>
40 </html>
41 <br />
42 <DIV id="page_123" class="cGreen"><a href='#'>上一页</a><strong>1</strong><a href='07-1359_2.html'>2</a> <a href='07-1359_2.html'>下一页</a> </DIV>
43 <!--操作-->
44 <div class="context">
45 <div id='digg1359' class='diggArea'>
46 <div class='diggNum'>0</div>
47 <div class="diggLink"><a href="javascript:Digg('digg',1359);">顶一下</a></div>
48 </div>
49
50 <div style="float:left; margin-left:20px; margin-top:10px">
51 上一篇:<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1358.html'>web标准中Iframe自适应高度[ie_firefox下都可用]</a>
52 <br />
53 下一篇:<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1360.html'>让IE6乖乖的使用mediatype样式</a>
54 </div>
55
56 </div>
57
58 <DIV id=ArtInfo style="FONT-SIZE: 12px"> <span>[ <a href="/plus/stow.php?arcID=1359">收藏 </a>]</span> <span>[<a href="/plus/recommend.php?arcID=1359"> 推荐 </a>]</span> <span>[<a href="/plus/feedback.php?arcID=1359"> 评论 </a>]</span> <span>[ <a href="#" onClick="window.print();">打印 </a>]</span> <span>[<a href="#" onClick="window.close();"> 关闭 </a>]</span> </DIV>
59 <DIV id=Preadblock></DIV>
60 <!--[if !IE]>|xGv00|def10af2b706d708d2fae346402958b1<![endif]--><!--/link--><!--/link--></DIV>
61 <SCRIPT type=text/javascript>
62 var tclStr = document.getElementById("Preadblock").innerHTML;
63 var artInfoStr2 = document.getElementById("ArtInfo2");
64 var artInfoStr1 = document.getElementById("ArtInfo");
65 if(tclStr.length>0){
66 artInfoStr2.innerHTML=artInfoStr1.innerHTML;
67 artInfoStr2.className="yanselan";
68 artInfoStr1.innerHTML="";
69 artInfoStr1.style.display = "none";
70
71 }
72 else{
73 artInfoStr2.style.display = "none";
74 artInfoStr2.className="";
75 }
76 </SCRIPT>
77 <!--[if !IE]>|xGv00|41346b3287db3847e4d38b597a7b0a4f<![endif]-->
78 <!--[if !IE]>|xGv00|8562b5198b819aae2599422d037fbd8e<![endif]-->
79 <DIV id=newsAbout>
80 <DL>
81 <DT>
82 <P class=Ltitle>相关新闻</P></DT>
83 <DD>
84 <TABLE cellSpacing=0 cellPadding=0 width="100%" align=center bgColor=#666666
85 border=0>
86 <TBODY>
87 <TR>
88 <TD
89 style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
90 vAlign=top width="62%" bgColor=#ffffff>
91 <TABLE height="100%" cellSpacing=0 cellPadding=0 width="100%"
92 align=center bgColor=#666666 border=0><!--relaTitleS-->
93 <TBODY>
94 <TR>
95 <TD
96 width="40%" align="center" bgColor=#ffffff
97 style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px"> </TD>
98 <TD
99 width="60%" align="left" bgColor=#ffffff
100 style="PADDING-RIGHT: 5px; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; PADDING-TOP: 5px">·<a href='/html/jianzhanjiaocheng/wangyesheji/200806/08-1031.html'>客齐集OEM的CSS解析</a>(06-08)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1362.html'>用Gnome HIG原则指导你的W</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1361.html'>使三种浏览器IE FireFox O</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1363.html'>教你识别CSS中ID和CLASS的</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1347.html'>WEB标准中元素水平居中方</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1360.html'>让IE6乖乖的使用mediatype</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1355.html'>DIV CSS常见错误汇总</a>(09-07)<br/>·<a href='/html/jianzhanjiaocheng/wangyesheji/200809/07-1356.html'>web标准中右下角广告代码</a>(09-07)<br/> </TD>
101 </TR><!--relaTitleE--><!--relaCatalogS--><!--relaCatalogE--><!--relaAuthorS--><!--relaAuthorE--><!--relaSourceS--><!--relaSourceE--></TBODY></TABLE></TD></TR></TBODY></TABLE></DD></DL></DIV>
102 <DIV id=pinlun>
103 <DIV class=ptou>发表评论</DIV>
104 <div class="comment">
105 <!--评论:标题-->
106 <div class="commenttitle">
107 <div class="title"><strong>最新评论</strong>共有 0 位网友发表了评论 </div>
108 <div class="gotopost"><a href="/plus/feedback.php?arcID=1359">查看所有评论</a></div>
109 </div>
110 <!--评论内容-->
111 <div class="commentbox">
112 <script src="/plus/feedback_js.php?arcID=1359"></script>
113 </div>
114 <!--发表评论:标题-->
115 <div class="posttitle"><strong>发表评论</strong></div>
116 <!--发表评论-->
117 <form action="/plus/feedback.php?action=send" method="post" name="feedback">
118 <input type="hidden" name="arcID" value="1359">
119 <div class="postbox">
120 <div class="txtbox">
121 <strong>评论内容:</strong>不能超过250字,需审核,请自觉遵守互联网相关政策法规。
122 <textarea name="msg" cols="60" rows="6"