今天做項目時,碰到div下的表格內容不能居中顯示。網上查了論壇,發現一個討論的帖子。(http://geekswithblogs.net/timh/archive/2006/04/19/75606.aspx)。現分享如下:
一、提出問題:
下面這段代碼.
<style type="text/css">
.container{
text-align: center;
border: solid 1px blue;
}
</style>
<div class=container>
<table>
<tr><td>Text</td></tr>
</table>
</div>
在IE下"Text"居中顯示,FF下"Text"靠左顯示.
二、解決問題:
方案1:
<style type="text/css">
.container{
text-align: center;
border: solid 1px blue;
}
table {
margin-left:auto;
margin-right:auto;
}
</style>
<div class=container>
<table>
<tr><td>Text</td></tr>
</table>
</div>
方案2:
<style type="text/css">
.container{
text-align:-moz-center; /*FF*/
#text-align: center; /*IE */
border: solid 1px blue;
}
</style>
<div class=container>
<table>
<tr><td>Text</td></tr>
</table>
</div>
方案3:
<style type="text/css">
.container{
border: solid 1px blue;
}
</style>
<center>
<div class=container>
<table>
<tr><td>Text</td></tr>
</table>
</div>
</center>
三、論道摘錄:
1.
After some research, a better solution is table { margins: auto; }
since table is a block element (apparantly :)
2.
I tried this and able to see it in both FF and IE
.centerMyTable{
text-align:-moz-center;
!text-align:center;
}
But You know what, insteand of ! you can put any NON alph-numeric character. I tried with ^ ~ @ # $ % ^ & * ( ) _ + and it worked
Walalalalala
3.
<center> doesn't help you if you try to get you xhtml valid strict 1.0