今天做项目时,碰到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.
写道
# re: text-align: center; not working in Firefox 5/3/2006 1:44 AM phil
After some research, a better solution is table { margins: auto; }
since table is a block element (apparantly :)
After some research, a better solution is table { margins: auto; }
since table is a block element (apparantly :)
2.
写道
# re: text-align: center; not working in Firefox 7/18/2007 2:18 PM kali_001
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
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.
写道
# re: text-align: center; not working in Firefox 10/18/2007 12:42 PM kees
<center> doesn't help you if you try to get you xhtml valid strict 1.0
<center> doesn't help you if you try to get you xhtml valid strict 1.0