CSS way to horizontally align table

前端 未结 10 1212
礼貌的吻别
礼貌的吻别 2020-11-30 18:46

I want to show a table of fixed width at the center of browser window. Now I use

But Visual S

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-30 19:41

    Steven is right, in theory:

    the “correct” way to center a table using CSS. Conforming browsers ought to center tables if the left and right margins are equal. The simplest way to accomplish this is to set the left and right margins to “auto.” Thus, one might write in a style sheet:

    table
    { 
        margin-left: auto;
        margin-right: auto;
    }
    

    But the article mentioned in the beginning of this answer gives you all the other way to center a table.

    An elegant css cross-browser solution: This works in both MSIE 6 (Quirks and Standards), Mozilla, Opera and even Netscape 4.x without setting any explicit widths:

    div.centered 
    {
        text-align: center;
    }
    
    div.centered table 
    {
        margin: 0 auto; 
        text-align: left;
    }
    
    
    

提交回复
热议问题