html5 css替代center的问题

 我是个初学者,之前居中我都使用的center标签

由于center标签在html5中已经不被支持,我希望使用CSS来替换它,但是在实际使用中遇到了困难

使用center将body内的全部内容居中的代码如下:

<body>
<center>
    <table width="850" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td colspan="2">{include file='top.tpl'}</td>
        </tr>
        <tr>
            <td width="216" align="left" valign="top">
                {include file='login.tpl'}
                {include file='public.tpl'}
                {include file='links.tpl'}
            </td>
            <td width="634" height="700" align="center" valign="top">
                {include file='search.tpl'}
                {include file=$admin_p_html}
            </td>
        </tr>
    </table>
    <table width="850" border="0" cellspacing="0" cellpadding="0">
        <tr>
            <td>{include file='buttom.tpl'}</td>
        </tr>
    </table>
</center>
</body>

 

正常居中(报错请无视,代码还没敲完):

百度后我看到可以使用text-align:center

但是不论是在body中加入style="text-align:center"、style="text-align:center;margin:0 auto;"

还是定义一个div把body中的内容全部框进去再style="text-align:center"均不奏效

<body style="text-align: center">
    <table width="850" border="0" cellspacing="0" cellpadding="0">
        <!--略-->
    </table>
    <table width="850" border="0" cellspacing="0" cellpadding="0">
        <!--略-->
    </table>
</body>
<body style="text-align: center">
    <div style="text-align: center">
        <table width="850" border="0" cellspacing="0" cellpadding="0">
            <!--略-->
        </table>
        <table width="850" border="0" cellspacing="0" cellpadding="0">
            <!--略-->
        </table>
    </div>
</body>

 

结果均为(居中失败):

这种情况应该怎么写才能正确居中呢?

改成style="text-align:center;margin: 0 auto;"

即加了margin: 0 auto;

<body>
    <table width="850" border="0" cellspacing="0" cellpadding="0" style="text-align:center;margin: 0 auto;">
        <tr>
            <td colspan="2">{include file='top.tpl'}</td>
        </tr>
        <tr>
            <td width="216" align="left" valign="top">
                {include file='login.tpl'}
                {include file='public.tpl'}
                {include file='links.tpl'}
            </td>
            <td width="634" height="700" align="center" valign="top">
                {include file='search.tpl'}
                {include file=$admin_p_html}
            </td>
        </tr>
    </table>
    <table width="850" border="0" cellspacing="0" cellpadding="0" style="text-align:center;margin: 0 auto;">
        <tr>
            <td>{include file='buttom.tpl'}</td>
        </tr>
    </table>
</body>

<table text-align="center" width="850" border="0" cellspacing="0" cellpadding="0">

 

text-align:center 写法

是用在 style样式内

你用法没有错误,但是你要的是table居中,不是整个body居中

所以你要在<table 标签内加入 style

则这样才对 <table style="text-align:center" width="850" border="0" cellspacing="0" cellpadding="0"

text-align="center" 写法

是用在<table <tr <td 标签内

如:<table text-align="center" 或<td text-align="center"

 

<body>
    <table width="850" border="0" cellspacing="0" cellpadding="0" style="text-align: center">
        <tr>
            <td colspan="2">{include file='top.tpl'}</td>
        </tr>
        <tr>
            <td width="216" align="left" valign="top">
                {include file='login.tpl'}
                {include file='public.tpl'}
                {include file='links.tpl'}
            </td>
            <td width="634" height="700" align="center" valign="top">
                {include file='search.tpl'}
                {include file=$admin_p_html}
            </td>
        </tr>
    </table>
    <table width="850" border="0" cellspacing="0" cellpadding="0" style="text-align: center">
        <tr>
            <td>{include file='buttom.tpl'}</td>
        </tr>
    </table>
</body>

 

还是不对呀

而且我的目的就是body内的全部内容居中,就像我之前用center标签那样