29

I want to have one table header be centered over two table columns side by side. Is this possible?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lizza
  • 2,769
  • 5
  • 39
  • 72

4 Answers4

55

<th colspan="2">. This .</th>

To extrapolate a bit...

<table>
  <thead>
    <tr>
      <th>Single Column</th>
      <th colspan="2">Double Column</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Column One</td>
      <td>Column Two</td>
      <td>Column Three</td>
    </tr>
  </tbody>
</table>

That should give you enough to work from.

SpoonNZ
  • 3,780
  • 1
  • 20
  • 25
10

If you only have 2 columns then I would suggest using <caption>. Otherwise, use colspan as suggested in other answers.

<table>
  <caption>This will span all columns.</caption>
  <tr><td>column one</td><td>column two</td></tr>
</table>
Travis J
  • 81,153
  • 41
  • 202
  • 273
5

Of course. Please refer to this page. You are looking for attribute called colspan for table headers cells.

Andrzej Ośmiałowski
  • 1,464
  • 12
  • 21
4

We can do it in better way.

<table border="1"> 
  <tr>
    <th colspan="1" scope="colgroup">Test Heading</th>
    <th colspan="3" scope="colgroup">Mars</th>
    <th colspan="3" scope="colgroup">Venus</th>
    <th colspan="3" scope="colgroup">Test</th>
  </tr>
  <tr>
    <th rowspan="2"></th>
    <th scope="col">Produced</th>
    <th scope="col">Sold</th>
    <th scope="col">Sold</th>
    <th scope="col">Produced</th>
    <th scope="col">Sold</th>
    <th scope="col">Sold</th>
    <th scope="col">Test 1</th>
    <th scope="col">Test 2</th>
    <th scope="col">Test 3</th>
  </tr>
</table>

Please visit Table Ref From w3.org if you want to know more.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
Srikrushna
  • 4,366
  • 2
  • 39
  • 46