Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align entire table to right with CSS?

How to align entire table to right with CSS?

margin-right does not help

Here: http://jsfiddle.net/dimskraft/Y2FKy/

HTML:

<table class="block logo">
    <colgroup>
        <col style="width:50%"/>
        <col style="width:50%"/>
    </colgroup>
    <tr>
        <td>something</td>
        <td>
            <table class="menu">
                <tr>
                    <td>Item 1</td>
                    <td>Item 2</td>
                </tr>
            </table>
        </td>
    </tr>
</table>

CSS:

table {
    width: 100%;
}
table.menu {
    width: auto;
    margin-right: 0px;
}
like image 850
Dims Avatar asked Mar 09 '14 17:03

Dims


People also ask

How do I align data in the right table?

HTML | <td> align Attribute left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align.

How do I align an entire table in HTML?

To center this table, you would need to add ;margin-left:auto;margin-right:auto; to the end of the style attribute in the <table> tag. The table tag would look like the following. Changing the style attribute in the <table> tag, as shown above, results in the table being centered on the web page, as shown below.


2 Answers

Use float: right instead:

table.menu {
    width: auto;
    float: right;
}

JSFiddle

Solution 2:

As float is a not a good practice to align elements, you can use <td style="text-align: right"> and display: inline-table in the table:

    <td style="text-align: right">
        <table border="1" class="menu">
            <tr>
                <td>Item 1</td>
                <td>Item 2</td>
            </tr>
        </table>
    </td>


    table.menu {
        width: auto;
        display: inline-table;
    }

JSFiddle solution 2

like image 41
Tony Dinh Avatar answered Sep 21 '22 19:09

Tony Dinh


You need to set the left margin to auto too. That will make the left margin push the table as far right as is allowed by the right margin.

table {
    width: 100%;
}
table, td {
    border: solid black 1px;
}
table.menu {
    width: auto;
    margin-right: 0px;
    margin-left: auto;
}
<table class="block logo">
    <colgroup>
        <col style="width:50%"/>
        <col style="width:50%"/>
    </colgroup>
    <tr>
        <td>something</td>
        <td>
            <table class="menu">
                <tr>
                    <td>Item 1</td>
                    <td>Item 2</td>
                </tr>
            </table>
        </td>
    </tr>
</table>
like image 176
Quentin Avatar answered Sep 21 '22 19:09

Quentin