日期:2014-05-17  浏览次数:20827 次

求div的align属性的替代方法
w3school的文档中明确写到div的align属性不被赞成使用,那么我想达到相同的效果还有什么其它简便的方法吗?

仅仅用css添加text-align:center是不行的,火狐对这个属性的理解和IE对这个属性的理解完全不同,前者的理解是div内部的元素居中,后者的理解是div块居中

下面是我的测试代码:
HTML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
            div.center
            {
                text-align:center;
            }
        </style>
        <title>center测试</title>
    </head>
    
    <body>
        <div class="center">
            <table border="1">
                <tr>
                    <td>一</td>
                    <td>二三</td>
                </tr>
                <tr>
                    <td>四五六</td>
                    <td>七八九十</td>
                </tr>
            </table>
        </div>
    </body>
</html>



firefox运行结果


ie运行结果


网上还有一种说法是使用margin:0 auto,但是我没有成功过,我对这条语句的真实性表示怀疑

求教,既然div的align属性不被赞成使用的话,那我应该用什么来代替它呢?

------解决方案--------------------
http://w3help.org/zh-cn/causes/RT8003
------解决方案--------------------
div.center table{ margin:auto;}
这个是对居中DIV的子元素使用的
------解决方案--------------------
HTML code

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <style type="text/css">
            * {
                margin: 0px auto;
                padding: 0px;
            }
        </style>
        <title>center测试</title>
    </head>
    
    <body>
        <div class="center">
            <table border="1">
                <tr>
                    <td>一</td>
                    <td>二三</td>
                </tr>
                <tr>
                    <td>四五六</td>
                    <td>七八九十</td>
                </tr>
            </table>
        </div>
    </body>
</html>