-1

I have such html structure:

<table border=1 >
    <tr> <!--this tr has one <td> that needs to be 100% width-->
        <td></td>
    </tr>
    <tr> <!--this tr has two <td> that each need to be 50% width-->
        <td></td>
        <td></td>
    </tr>
    <tr> <!--this tr has three <td> that each need to be 33.3333% width-->
        <td></td>
        <td></td>
        <td></td>
    </tr>
    <tr> <!--this tr has one <td> that needs to be 100% width-->
        <td></td>
    </tr>
</table>

how can i do, that for example first tr td is for 100%, second tr two td's are both 50%, but all them for 100% and so over, i need to fill td to tr width, and if there more than one td, to do that they divide this width...

http://jsfiddle.net/ujMgM/

possibly without using js...

update: NO! colspan

Josh Lim
  • 5
  • 5
brabertaser19
  • 5,678
  • 16
  • 78
  • 184
  • The descriptions is underspecified: what should the widths of the third row be? If, in general, the available width should be evenly divided between the cells, please specify this in the question. (Then the answer is generally “you can’t, you must use a structure other than a table.”) – Jukka K. Korpela Feb 17 '14 at 09:12
  • @JukkaK.Korpela why? i recieved a good answer by James Donnelly – brabertaser19 Feb 17 '14 at 09:48
  • Future visitors will not benefit from answers if they do not know what the question is. Your “and so over” suggests equal widths, but you have accepted an answer that does not do that. – Jukka K. Korpela Feb 17 '14 at 11:48

3 Answers3

1

Using HTML

Just use the colspan HTML attribute:

This attribute specifies the number of columns spanned by the current cell. The default value of this attribute is one ("1").

This means that if your table has a total of 3 columns and you want one cell to span all 3 columns, you'd specify a colspan of "3":

<table border=1 >
    <tr>
        <td colspan="3">
        </td>
    </tr>
    <tr>
        <td colspan="2">
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td colspan="3">
        </td>
    </tr>
</table>

Using jQuery

You can instead use jQuery to add in the colspan attributes for you:

// Get a reference to the table's tbody element, and define the number of columns
var $tbody = $('table').find('tbody'),
    columns = 3;

// Loop through each tr within the table's tbody
$tbody.find('tr').each(function() {

    // Determine the number of cells, and set the colspan
    var children = $(this).children().size(),
        colspan = columns / children;

    // If the colspan variable is set to *.5, give the first cell higher colspan
    if (colspan % 1 === 0.5) {
        $(this).children('td').attr('colspan', colspan - 0.5);
       $(this).children('td:first').attr('colspan', colspan + 0.5);
    }
    // Otherwise give all cells equal colspan
    else
        $(this).children('td').attr('colspan', colspan);
});

JSFiddle demo.

Note how I'm using the tbody here? Ideally your table should have a tbody element, but most browsers will add this in for you.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
0

You have to use colspan="3" if you want to fit the td for all the three columns.

About colspan:

This attribute contains a non-negative integer value that indicates for how many columns the cell extends. Its default value is 1; if its value is set to 0, it extends until the end of the , even if implicitly defined, that the cell belongs to. Values higher than 1000 will be considered as incorrect and will be set to the default value

colspan Reference mdn

The modified code looks like

<table border=1 >
    <tr>
        <td colspan="3">
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td colspan="2">
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
        </td>
        <td>
        </td>
    </tr>
    <tr>
        <td colspan="3">
        </td>
    </tr>
</table>

Fiddle Demo

0

Use colspan="0"

Or use jQuery to calculate most td in rows and add colspan dynamically.

Community
  • 1
  • 1
Justinas
  • 41,402
  • 5
  • 66
  • 96