I have the following HTML table format:
<table style="width: 100%;">
<tr>
<td>
//How to center this table within the parent-table cell?
<table style="width: 760px;">
<tr>
<td>
</td>
</tr>
</table>
</td>
</tr>
</table>
Since HTML5 doesn't support the 'align=center' for tables, I am trying to figure out how to simulate the 'align=center' in CSS for the sub-table in my example above.
I've tried messin' around with the CSS margin attribute to no avail.
How do I center the sub-table in the example above?
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.
Like last time, you must know the width and height of the element you want to center. Set the position property of the parent element to relative . Then set the child's position property to absolute , top to 50% , and left to 50% . This just centers the top left corner of the child element vertically and horizontally.
table {border:solid 1px #0f0}
table table {border:solid 1px #f00;margin: 0 auto}
<table style="width: 100%;">
<tr>
<td>
//How to center this table within the parent-table cell?
<table style="width: 760px;">
<tr>
<td>
Center This Table Dawg
</td>
</tr>
</table>
</td>
</tr>
</table>
margin:0 auto;
worked in this example, tested/worked in IE 7-9, FF 4, Chrome 11
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