-1

I have a table on my webpage, and it automatically aligns to the left, the text is aligned in the center which is what I want, but I am unable to get the table to align to the center of the page.

<table style="border: 1px solid black;  width:300px">
    <tr>

            <td colspan="2">
                <b>&nbsp;Change Your Passwordsword</b>
                </td>
                </tr>
                <tr>
            <td>
                New Password: 
            </td>    
             <td>
                <input type="password" id="pwd1" name="pwd1" size = "8" />&nbsp;
              </td>
            <tr>
            <td>
            Confirm Password: 
            </td>
                    <td>

                <input type="password" id="pwd2" size = "8" name="pwd2" />&nbsp;
              </td> 
            </tr>  
            <tr>
            <td>
            </td>
            <td>

                <input type="submit" value="Confirm" /></td>

            </tr> 
            <tr>
            <td>
            <asp:Literal ID="msg" runat="server" />
            </td>
            </tr>
            </table>

Here is the code for my table.

Karan Ramchandani
  • 305
  • 2
  • 3
  • 11

4 Answers4

1

USe the following css property

table{
margin:auto;
}
<table style="border: 1px solid black;  width:300px">
    <tr>

            <td colspan="2">
                <b>&nbsp;Change Your Passwordsword</b>
                </td>
                </tr>
                <tr>
            <td>
                New Password: 
            </td>    
             <td>
                <input type="password" id="pwd1" name="pwd1" size = "8" />&nbsp;
              </td>
            <tr>
            <td>
            Confirm Password: 
            </td>
                    <td>

                <input type="password" id="pwd2" size = "8" name="pwd2" />&nbsp;
              </td> 
            </tr>  
            <tr>
            <td>
            </td>
            <td>

                <input type="submit" value="Confirm" /></td>

            </tr> 
            <tr>
            <td>
            <asp:Literal ID="msg" runat="server" />
            </td>
            </tr>
            </table>
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47
1

JSFiddle

Add align="center" to the <table>

Vincent Lee
  • 177
  • 1
  • 9
0

Just use

margin: 0 auto;

It will center your table.

Check: Fiddle.

ketan
  • 19,129
  • 42
  • 60
  • 98
0

To center a table, you need to set the margins, like this:

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

And then do this:

<table class="center"> ... </table>

At this point, Mozilla and Opera will center your table. Internet Explorer 5.5 and up, however, needs you to add this to your CSS as well:

body {text-align:center;}