CSS way to horizontally align table

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

    This should work:

    <div style="text-align:center;">
      <table style="margin: 0 auto;">
        <!-- table markup here. -->
      </table>
    </div>
    
    0 讨论(0)
  • 2020-11-30 19:22

    Although it might be heresy in today's world - in the past you would do the following non-css code. This works in everything up to and including today's browsers but - as I have said - it is heresy in today's world:

    <center>
    <table>
       ...
    </table>
    </center>
    

    What you need is some way to tell that you want to center a table and the person is using an older browser. Then insert the "<center>" commands around the table. Otherwise - use css.

    Surprisingly - if you want to center everything in the BODY area - you just can use the standard

    text-align: center;
    

    css command and in IE8 (at least) it will center everything on the page including tables.

    0 讨论(0)
  • 2020-11-30 19:23
    <style>
        .abc {
            text-align: center;
        }
    </style>
    
    <table class="abc">
        <tr>
            <td>Item1</td>
            <td>Item2</td>
        </tr>
    </table>
    
    0 讨论(0)
  • 2020-11-30 19:26
    style="text-align:center;" 
    

    (i think)

    or you could just ignore it, it still works

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

    Try this:

    <table width="200" style="margin-left:auto;margin-right:auto">
    
    0 讨论(0)
  • 2020-11-30 19:38

    Add to the div style:

    width: fit-content;
    
    0 讨论(0)
提交回复
热议问题