Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a table cell into two columns in HTML

I have the following table:

<table border="1">   <tr>     <th scope="col">Header</th>     <th scope="col">Header</th>     <th scope="col">Header</th>   </tr>   <tr>     <th scope="row">&nbsp;</th>     <td>&nbsp;</td>     <td>Split this one into two columns</td>   </tr> </table> 

And I wish to split the cell which contains "Split this one into two columns" into two cells/columns. How do I go about this?

Fiddle

like image 958
user1038814 Avatar asked Oct 01 '13 11:10

user1038814


People also ask

How do you split a table cell into two columns in HTML?

Use an extra column in the header, and use <colspan> in your header to stretch a cell for two or more columns. Insert a <table> with 2 columns inside the td you want extra columns in.

How do you split a table in HTML?

Tables can be divided into three portions − a header, a body, and a foot. The head and foot are rather similar to headers and footers in a word-processed document that remain the same for every page, while the body is the main content holder of the table. <thead> − to create a separate table header.

How do I split one row into multiple rows in HTML?

in column one, has a single row. in column two, is split into 2 sub rows. in column three, is split into 4 sub rows (row one from the previous column is split into 2 sub rows, and row 2 from the previous column is split into 2 sub rows).


2 Answers

Like this http://jsfiddle.net/8ha9e/1/

Add colspan="2" to the 3rd <th> and then have 4 <td>'s in your second row.

<table border="1">   <tr>     <th scope="col">Header</th>     <th scope="col">Header</th>     <th scope="col" colspan="2">Header</th>   </tr>   <tr>     <th scope="row">&nbsp;</th>     <td>&nbsp;</td>     <!-- The following two cells will appear under the same header -->     <td>Col 1</td>     <td>Col 2</td>   </tr> </table> 
like image 98
punkrockbuddyholly Avatar answered Sep 20 '22 23:09

punkrockbuddyholly


I came here for a similar problem I was facing with my table headers.

@MrMisterMan's answer, as well as others, were really helpful, but the borders were beating my game. So, I did some research to find the use rowspan.

Here's what I did and I guess it might help others facing something similar.

<table style="width: 100%; margin-top: 10px; font-size: 0.8em;" border="1px">      <tr align="center" >          <th style="padding:2.5px; width: 10%;" rowspan="2">Item No</th>          <th style="padding:2.5px; width: 55%;" rowspan="2">DESCRIPTION</th>          <th style="padding:2.5px;" rowspan="2">Quantity</th>          <th style="padding:2.5px;" colspan="2">Rate per Item</th>          <th style="padding:2.5px;" colspan="2">AMOUNT</th>      </tr>      <tr>          <th>Rs.</th>          <th>P.</th>          <th>Rs.</th>          <th>P.</th>      </tr>  </table>
like image 24
lu5er Avatar answered Sep 21 '22 23:09

lu5er