Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to center-align an HTML child table within a parent table via CSS

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?

like image 393
Jed Avatar asked May 17 '11 14:05

Jed


People also ask

How do you center align a table in HTML CSS?

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.

How do I center my child in CSS?

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.


1 Answers

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

like image 190
MikeM Avatar answered Oct 05 '22 08:10

MikeM