Alex Pliutau
Alex Pliutau

Reputation: 21947

Combine cells in an HTML table

I have a table with three rows and three columns. How can I combine the last row columns? My HTML content:

<table>
  <tr><td></td><td></td><td></td></tr>
  <tr><td></td><td></td><td></td></tr>
  <tr><td>Here I need a cell by all width</td></tr>
</table>

Upvotes: 17

Views: 58385

Answers (2)

Alin P.
Alin P.

Reputation: 44386

For this exact purpose the colspan attribute was created.

<table>
  <tr><td></td><td></td><td></td></tr>
  <tr><td></td><td></td><td></td></tr>
  <tr><td colspan="3">Here I need a cell by all width</td></tr>
</table>

Similarly you can use use the rowspan attribute if you want to span multiple rows.

<table>
  <tr><td></td><td></td><td rowspan="3">Spans full table height</td></tr>
  <tr><td></td><td></td></tr>
  <tr><td></td><td></td></tr>
</table>

To learn more about these two attributes visit the official documentation on w3.org.

Also I urge you not to use tables unless you need to present tabular data.

Upvotes: 8

Oded
Oded

Reputation: 499382

Use the colspan attribute:

<tr><td colspan="3">Here I need a cell by all width</td></tr>

The colspan attribute defines the number of columns a cell should span.

There is a related attribute rowspan that achieves the same for rows.

Upvotes: 45

Related Questions