Search code examples
html-tablealignmentcenter

I can't seem to figure out how to center align my table


<table border="0" align="center">
<tbody>
<tr>
<td>Cell1</td>
<td>Cell1</td>
</tr>
<tr>
<td>Cell1</td>
<td>Cell1</td>
</tr>
<tr>
<td>Cell1</td>
<td>Cell1</td>
</tr>
<tr>
<td>Cell1</td>
<td>Cell1</td>
</tr>
</tbody>
</table>

I am not good with coding. I've tried but cannot figure it out. Your help would be greatly appreciated.


Solution

  • // CSS:
    
    #container {
        text-align:center; // needed if you expect IE 5
    }
    
    .centered {
        margin:0px auto; // this sets left/right margin to auto and
                         // centers the element
    }
    
    
    // HTML:
    
    <div id="container">
        <table class="centered" border="0">
            ...
        </table>
    </div>
    

    Or if you want to style inline:

    <div style="text-align:center;">
        <table style="margin:0px auto;" border="0">
            ...
        </table>
    </div>