CSS way to horizontally align table

前端 未结 10 1050
礼貌的吻别
礼貌的吻别 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:40

    Simple. IE6 and above will happily center your table with "margin: 0 auto;" if only the page renders in "standards" mode. To make this happen you need a valid doctype declaration, such as

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    

    or

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    

    True, IE5.5 and below will still refuse to center the table but perhaps you can live with that, especially if the page is still functional with the table left aligned. I think by now users of IE5.5 and below are fairly used to some odd looking websites - but you still need to ensure that those visual glitches don't render your site unusable.

    Happy coding!

    EDIT: Sorry, I should perhaps point out that you do not have to have a "strict" doctype to get IE6 and up into "standards" rendering mode. I realised it might seem that way from the doctype examples I posted above. For example, this doctype declaration will of course work equally:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    0 讨论(0)
  • 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;
    }
    
    
    <div class="centered">
        <table>
        …
        </table>
    </div>
    
    0 讨论(0)
  • 2020-11-30 19:42

    Basically, it works like,

    <table align="center">
    ...
    

    But, you can do it in better way using CSS, and this will be a better approach to use CSS as it provides dynamic behavior to a web page and is responsive.

      <table style="text-align:center;">
    

    Also, if you need to display only table on a page then you can go with body tag alignment only as shown below,

     <body style="text-align:center;">
    <table>
      ...
    </table>
    

    If you want any other suggestions please let me know. Will surely help you on this. Also using bootstrap features this would be more easy and interactive.

    0 讨论(0)
  • 2020-11-30 19:45

    I'm just learning this and what finally worked for me was to first make a table with three rows. Set the margin for the left and right rows to 50%. Then put a single row, fixed width table inside of the "table data" of the center "table row".

    0 讨论(0)
提交回复
热议问题