Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align TH Header with TD in TBody

Tags:

I'm having problems trying to embed a table in an existing HTML page with some CSS.

This CSS is hiding the header of the table by default with a style definition like:

.tablestuff thead {
     display: none;
}

But I want the table to show, so I tried setting the style on the thead element with "display:block" (with javascript). That makes the header display, but the columns of the header don't line up with the td columns.

I have reduced my HTML to the following (hopefully with minimal typos) and showing the style on the thead element as set by javascript.

<div class="tablestuff">
<table border="1">
<thead style="display:block">
<tr>
<th id="th1" style="width: 20px"></th>
<th id="th2" style="width: 20px"></th>
</tr>
</thead>
<tbody>
<tr>
<td headers="th1" style="width: 20px"></td>
<td headers="th2" style="width: 20px"></td>
</tr>
</tbody>
</table>
</div>

How can I make both the header show and also align correctly with the td columns?

like image 890
George Hernando Avatar asked Aug 28 '12 05:08

George Hernando


People also ask

How do I align TD content to the right?

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. justify: It stretches the text of paragraph to set the width of all lines equal.

How do you align text in a table header in HTML?

To center align text in table cells, use the CSS property text-align. The <table> tag align attribute was used before, but HTML5 deprecated the attribute. Do not use it. So, use CSS to align text in table cells.

Do we have to write th tag in between the TD start and end tag?

Console OutputThe start tag is mandatory. The end tag may be omitted, if it is immediately followed by a <th> or <td> element or if there are no more data in its parent element. A <tr> element.


1 Answers

CSS includes more display modes than the commonly used none, inline, inline-block, and block. There are quite a few, in fact.

In this case, it appears what you want to use instead of display:block; is display:table-header-group;.

Here are some examples of the different styles, as applied to your table:

http://jsfiddle.net/CrYdz/1

like image 158
aroth Avatar answered Oct 16 '22 14:10

aroth