breeze086 2021-02-27 16:36 采纳率: 100%
浏览 456
已采纳

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>

结果均为(居中失败):

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

  • 写回答

3条回答 默认 最新

  • wanmeikakaxi 2021-02-27 17:39
    关注

    改成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>

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)