Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different number of Columns in rows of a table

Is it possible to create a table with different number of cells in each and every row with the same width and height ..?? If so how it can be done in a simpler way ..???

Note:

  1. Row width and height are same
  2. Cell width differs in each and every row

    <table>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
    

    This is what i have tried using coll span ..Here let's say first row cells width is 30px,30px,30px . if i use coll span , it will be like 60px,30px but i want it as 50px,40px with only 2 cells

I want like thisenter image description here

like image 784
Prasath K Avatar asked Mar 27 '13 11:03

Prasath K


People also ask

Can different rows have different number of columns?

yes, simply use colspan.

Can an HTML table have different columns in different rows?

Cells within HTML tables can span multiple rows and multiple columns. The cell default is one row and one column. However, with the ROWSPAN attribute a cell can span more than one row, and with the COLSPAN attribute a cell can span more than one column.

How do I create a table with different columns?

Under Table Tools, click the Layout tab. Do one of the following: To add a column just to the left of the cell that you clicked in, in the Rows and Columns group, click Insert Left. To add a column just to the right of the cell that you clicked in, in the Rows and Columns group, click Insert Right.

How do I change the number of columns in a table?

Add a row or column To insert a single row or column, click a cell in the row adjacent to where you want to add the new row or column. To insert multiple rows or columns, select the same number of rows or columns that you want to insert. On the Table menu, point to Insert, and then click the option you want.


1 Answers

You can use colspan to create cells that span multiple columns.

jsFiddle

enter image description here

<table>
    <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="3">&nbsp;</td>
    </tr>
</table>

If you want all to be the same width and height but only have the number of cells differ you can just not style certain cells in the <table>.

jsFiddle

enter image description here

<table>
    <tr>
        <td class="content">&nbsp;</td>
        <td></td>
        <td></td>
    </tr>
    <tr>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td></td>
    </tr>
    <tr>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
        <td class="content">&nbsp;</td>
    </tr>
</table>

Update

Your update with the image, yes you can accomplish this using colspan:

jsFiddle

enter image description here

<table>
    <tr>
        <td>&nbsp;</td>
        <td colspan="2">&nbsp;</td>
        <td>&nbsp;</td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>

This uses four columns, the middle two are smaller than the others, here is an image that illustrates how the columns are set up:

enter image description here

Update #2

Here is an example of more randomly sizes cells. The first row 10%, 90% and the second row 55%, 45%.

jsFiddle

enter image description here

HTML

<table>
    <tr>
        <td></td>
        <td colspan="2"></td>
    </tr>
    <tr>
        <td colspan="2"></td>
        <td></td>
    </tr>
</table>

CSS

table {
    width:100px;
}
td {
    border: 1px solid #000;
    height:1em;
}
tr:first-child td:first-child {
    width:10%;
}
tr:first-child td:last-child {
    width:90%;
}
tr:last-child td:first-child {
    width:55%;
}
tr:last-child td:last-child {
    width:45%;
}
like image 160
Daniel Imms Avatar answered Sep 28 '22 20:09

Daniel Imms