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;
}
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.
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.
Use float: right
instead:
table.menu {
width: auto;
float: right;
}
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;
}
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With