职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 363|回复: 9

text-align: center; not working in Firefox

[复制链接]
broken 发表于 2011-7-24 09:52 | 显示全部楼层 |阅读模式
今天做项目时,碰到div下的表格内容不能居中显示。网上查了论坛,发现一个讨论的帖子。(http://geekswithblogs.net/timh/archive/2006/04/19/75606.aspx)。现分享如下:

一、提出问题:
    下面这段代码.
Html代码  
<style type="text/css">  
.container{   
  text-align: center;   
  border: solid 1px blue;   
}   
</style>  
  
<div class=container>  
    <table>  
        <tr><td>Text</td></tr>  
    </table>  
</div>  

<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:
   
Html代码  
<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>  

<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:
   
Html代码  
<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>  

<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:
   
Html代码  
<style type="text/css">  
.container{   
  border: solid 1px blue;   
}   
</style>  
  
<center>  
<div class=container>  
    <table>  
        <tr><td>Text</td></tr>  
    </table>  
</div>  
</center>  

<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 :)

     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


    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


   

叫我小乖 发表于 2011-7-24 09:52 | 显示全部楼层
put a table in a div which feeling strangeness

if you only want make the elements abover the div(which means the table) you can do just like this

<table align='center'>

</table>

this must can be work!

have a try!

曾经的小孩 发表于 2011-7-24 09:52 | 显示全部楼层
求LS MM联系方式...

哈哈...

芷馨 发表于 2011-7-24 09:53 | 显示全部楼层
lz根本没搞懂问题原因

紫衿 发表于 2011-7-24 09:53 | 显示全部楼层
askjsp
put a table in a div which feeling strangeness

if you only want make the elements abover the div(which means the table) you can do just like this

<table align='center'>

</table>

this must can be work!

have a try!


首先,thanks

put a table in a div which feeling strangeness ??? div用于布局,table用于显示数据,有什么问题吗?

<table align='center'>

</table>    这样是可以的,但是我是从布局层面来考虑问题的。

江南枫 发表于 2011-7-24 09:53 | 显示全部楼层
sohighthesky
lz根本没搞懂问题原因

能说说问题的原因是什么吗? 求点化

fossil 发表于 2011-7-24 09:53 | 显示全部楼层
你可以设置body样式为文本居中,然后再设置边界自动,这样不管是IE还是FF都兼容了。。
body{text-align: center;}
.container{   
  margin: auto;   
  border: solid 1px blue;   
}

醉倚西风 发表于 2011-7-24 09:53 | 显示全部楼层
wangking717
你可以设置body样式为文本居中,然后再设置边界自动,这样不管是IE还是FF都兼容了。。
body{text-align: center;}
.container{   
  margin: auto;   
  border: solid 1px blue;   
}


ff表示鸭梨很大

叫我小乖 发表于 2011-7-24 09:53 | 显示全部楼层
Html代码  
<html>  
<head>  
<style>  
body { text-align: center; margin: 0; padding: 0; }   
/* margin,padding进行reset */   
#wrapper {   
  margin:0px auto;   
  text-align: left;   
  width: 600px;   
  height: 700px;   
  background: blue;   
}   
/* 1. 还原成默认的left  2. 如果不加宽度居中没有意义。 */   
</style>  
</head>  
<body>  
<div id="wrapper"></div>  
</body>  
</html>  

<html>
<head>
<style>
body { text-align: center; margin: 0; padding: 0; }
/* margin,padding进行reset */
#wrapper {
  margin:0px auto;
  text-align: left;
  width: 600px;
  height: 700px;
  background: blue;
}
/* 1. 还原成默认的left  2. 如果不加宽度居中没有意义。 */
</style>
</head>
<body>
<div id="wrapper"></div>
</body>
</html>


郁闷小男人 发表于 2011-7-24 09:54 | 显示全部楼层
这里有详细解释:http://www.w3help.org/zh-cn/causes/RT8003


您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-4-20 23:16 , Processed in 0.134208 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表