Most common way of writing a HTML table with vertical headers?

HtmlCssDatatableSemantic Markup

Html Problem Overview


Hi all it's been a while since I've asked something, this is something that has been bothering me for a while, the question itself is in the title:

> What's your preferred way of writing HTML tables that have vertical headers?

By vertical header I mean that the table has the header (<th>) tag on the left side (generally)

> Header 1 data data data
Header 2 data data data
Header 3 data data data

They look like this, so far I've come up with two options

First Option


<table id="vertical-1">
<caption>First Way</caption>
<tr>
<th>Header 1</th>
<td>data</td><td>data</td><td>data</td>
</tr>
<tr>
<th>Header 2</th>
<td>data</td><td>data</td><td>data</td>
</tr>
<tr>
<th>Header 2</th>
<td>data</td><td>data</td><td>data</td>
</tr>
</table>


The main advantage of this way is that you have the headers right (actually left) next to the data it represents, what I don't like however is that the <thead>, <tbody> and <tfoot> tags are missing, and there's no way to include them without breaking the nicelly placed together elements, which lead me to the second option.

Second Option


<style type="text/css">
#vertical-2 thead,#vertical-2 tbody{
display:inline-block;
}



	&lt;/style&gt;
	&lt;table id="vertical-2"&gt;
		&lt;caption&gt;Second Way&lt;/caption&gt;
		&lt;thead&gt;
			&lt;tr&gt;
				&lt;th colspan="3"&gt;Header 1&lt;/th&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;th colspan="3"&gt;Header 2&lt;/th&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;th colspan="3"&gt;Header 3&lt;/th&gt;
			&lt;/tr&gt;
		&lt;/thead&gt;
		&lt;tbody&gt;
			&lt;tr&gt;
				&lt;td&gt;row 1&lt;/td&gt;
				&lt;td&gt;row 1&lt;/td&gt;
				&lt;td&gt;row 1&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;data&lt;/td&gt;
				&lt;td&gt;data&lt;/td&gt;
				&lt;td&gt;data&lt;/td&gt;
			&lt;/tr&gt;
			&lt;tr&gt;
				&lt;td&gt;data&lt;/td&gt;
				&lt;td&gt;data&lt;/td&gt;
				&lt;td&gt;data&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/tbody&gt;
		&lt;tfoot&gt;
			&lt;tr&gt;
				&lt;td colspan="4"&gt;Footer&lt;/td&gt;
			&lt;/tr&gt;
		&lt;/tfoot&gt;
	&lt;/table&gt;





The main advantage here is that you have a fully descriptive html table, the drawbacks are that proper representation needs a bit of CSS for the tbody and thead tags and that the relation between the headers and data isn't very clear as I had my doubts when creating the markup.


So, both ways render the table how it should, here a pitcure:

render
With the headers on the left or right side if you would prefer it, so, any suggestions, alternatives, browser issues?

Html Solutions


Solution 1 - Html

First, your second option isn't quite valid HTML in the sense that all of the rows (TR) in a table should contain an equal number of columns (TD). Your header has 1 while the body has 3. You should use the colspan attribute to fix that.

Reference: "The THEAD, TFOOT, and TBODY sections must contain the same number of columns." - Last paragraph of section 11.2.3.

With that being said, the first option is the better approach in my opinion because it's readable regardless of whether or not I have CSS enabled. Some browsers (or search engine crawlers) don't do CSS and as such, it'll make your data make no sense as the header will then represent columns instead of rows.

Solution 2 - Html

The First Option... I think it is the better and simple approach..

Solution 3 - Html

Honestly, option 1. I would suggest you to look at this example from W3.org(link below). I think this method is the best, because this way your headings will also be interpreted right on screen readers.

https://www.w3.org/WAI/tutorials/tables/one-header/#table-with-header-cells-in-the-first-column-only

Solution 4 - Html

If you want to show a data-bound control element (like asp repeater) in your table, then first option won't be possible. Second option can be used as follows.


<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

    <asp:Repeater ID="hours" runat="server">
      <HeaderTemplate>
        <table id="vertical-table">
          <thead>
            <tr>
              <th colspan="0">hours:</th>
            </tr>
            <tr>
              <th colspan="1">Monday</th>
            </tr>
            <tr>
              <th colspan="1">Tuesday</th>
            </tr>
            <tr>
              <th colspan="1">Wednesday</th>
            </tr>
            <tr>
              <th colspan="1">Thursday</th>
            </tr>
            <tr>
              <th colspan="1">Friday</th>
            </tr>
            <tr>
              <th colspan="1">Saturday</th>
            </tr>
            <tr>
              <th colspan="1">Sunday</th>
            </tr>
          </thead>
          <tbody>
      </HeaderTemplate>
      <ItemTemplate>
            <tr>
              <td>
                <%# Container.DataItem %>
              </td>
            </tr>
      </ItemTemplate>
      <FooterTemplate>
          </tbody>
        </table>
      </FooterTemplate>
    </asp:Repeater>

<!-- end snippet -->

Solution 5 - Html

div.vertical {
  margin-left: -85px;
  position: absolute;
  width: 215px;
  transform: rotate(-90deg);
  -webkit-transform: rotate(-90deg);
  /* Safari/Chrome */
  -moz-transform: rotate(-90deg);
  /* Firefox */
  -o-transform: rotate(-90deg);
  /* Opera */
  -ms-transform: rotate(-90deg);
  /* IE 9 */
}

th.vertical {
  height: 220px;
  line-height: 14px;
  padding-bottom: 20px;
  text-align: left;
}

<table>
  <thead>
    <tr>
      <th class="vertical">
        <div class="vertical">Really long and complex title 1</div>
      </th>
      <th class="vertical">
        <div class="vertical">Really long and complex title 2</div>
      </th>
      <th class="vertical">
        <div class="vertical">Really long and complex title 3</div>
      </th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Example</td>
      <td>a, b, c</td>
      <td>1, 2, 3</td>
    </tr>
  </tbody>
</table>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTristianView Question on Stackoverflow
Solution 1 - HtmlFrancois DeschenesView Answer on Stackoverflow
Solution 2 - HtmlPhilemon philip KunjumonView Answer on Stackoverflow
Solution 3 - HtmlDmitri ChebotarevView Answer on Stackoverflow
Solution 4 - HtmlBaz GuvenkayaView Answer on Stackoverflow
Solution 5 - HtmlDeenathaiyalan ShanmugamView Answer on Stackoverflow