HTML Table help alternating number of cells
-
06-07-2019 - |
Question
What is the proper HTML if I want to alternate the number of cells in each row? I want 2 cells is row 1, 3 in row 2, 2 in 3 , 3 in 4 etc... Similar to a brick wall. I have this so far, but it doesn't render like I would like. I know I could do this nesting tables, but can I do this with one table?
<table border="1" cellpadding="10">
<tr>
<td colspan="1.5"> <span> X </span> </td>
<td colspan="1.5"> <span> X </span> </td>
</tr>
<tr>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
</tr>
<tr>
<td colspan="1.5"> <span> X </span> </td>
<td colspan="1.5"> <span> X </span> </td>
</tr>
<tr>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
</tr>
<tr>
<td colspan="1.5"> <span> X </span> </td>
<td colspan="1.5"> <span> X </span> </td>
</tr>
<tr>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
</tr>
</table>
Solution
You can't have a cell span fractional widths. Instead, you need to find the least common denominator for the number of rows in the lower and upper ranges. In your example, 2 and 3, which is 6 - so your table is 6 cells wide, with the even rows spanning 3 each and the odd rows spanning 2 each.
<table width="360">
<tr>
<td align="center" width="180" colspan="3">x</td>
<td align="center" width="180" colspan="3">x</td>
</tr>
<tr>
<td align="center" width="120" colspan="2">x</td>
<td align="center" width="120" colspan="2">x</td>
<td align="center" width="120" colspan="2">x</td>
</tr>
</table>
Note the table width is easily divisible by 2 and 3, and the widths are explicitly set in each cell.
OTHER TIPS
One way to do it would be via CSS. You would set up a table that is as large as you need it (4x4, for example), then apply a style to each cell as appropriate. There would be two styles, brick and empty, one signifying a "brick" and the other signifying empty space.
So, you could do something like the following:
<html>
<head>
<style type="text/css">
td { border-style:none; width:30px; }
td.brick { border-style:solid; border-color:black; border-width:1px; }
</style>
</head>
<body>
<table>
<tr><td class="brick"> </td><td></td><td></td><td></td></tr>
<tr><td class="brick"> </td><td class="brick"> </td><td></td><td></td></tr>
<tr><td class="brick"> </td><td class="brick"> </td><td class="brick"> </td><td></td></tr>
<tr><td class="brick"> </td><td class="brick"> </td><td class="brick"> </td><td class="brick"> </td></tr>
</table>
</body>
</html>
<table>
<tr>
<td colspan="2">X</td>
<td colspan="2">X</td>
<td colspan="2">X</td>
<td>Y</td>
</tr>
<tr>
<td>Y</td>
<td colspan="2">X</td>
<td colspan="2">X</td>
<td colspan="2">X</td>
</tr>
I am pretty sure that colspans must have an integer value, fractions aren't allowed. You could try adding a dummy cell with 0 width, 0 padding, 0 margin on the 3-cell line.
<tr>
<td colspan="2"> <span> X </span> </td>
<td colspan="2"> <span> X </span> </td>
</tr>
<tr>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
<td> <span> X </span> </td>
<td><!-- dummy !--></td>
</tr>