.outer {
display:table; width:578px; overflow:hidden;
background: #eee; height: 42px;
}
.middle {display:table-cell; vertical-align:middle; margin-left 10px;}
/*下面的CSS是针对IE7,IE6*/
<!--[if lte IE 7]>
<style>
.outer{position:relative;}
.middle{position: absolute; top: 50%;}
.inner{position: relative; top:-50%}
</style>
<![endif]-->
这里我们需要定义三个DIV:
外层(outer) 可以根据需要定义高度、宽度,但是display必须为table。
中间层(middle) 可以有特定的CSS样式,例如:margin-left,但是display必须为table-cell,vertical-align必须为middle。
内层(inner) 主要是用来对付IE6和IE7的(注意:IE8支持table-cell居中,所以无需hack)。
有了上面的CSS,HTML代码可以这么写:
<div class="outer">
<div class="middle">
<div class="inner">
把你要居中的文本放在这里
这是第二行
...
</div>
</div>
</div>