Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autosize HTML table cell height based on content when rowspan is involved

Is there a way to autosize HTML table height based on content? Also if it's a cell (or cells) next to a neighbor cell with multiple rowspans.

E.g. if I have a table like this (cell on the right has Rowspan="2" and height of the cell content = 600px, in each cell on the left height of the cell content = 150px):

Cells evenly spaces

there is a gap between 2 cell consents on the left because cells themselves autosized their height. I'd like it to look like this:

Cell height adjusted to content

Where top cells automatically collapse to cell content height. Is there anyway to achieve this?

like image 433
Yuriy Galanter Avatar asked Jun 06 '12 17:06

Yuriy Galanter


People also ask

How to increase the size of a table cell in HTML?

To manipulate the height or width of an entire table, place the size attribute (either "WIDTH=" or "HEIGHT=") within the <TABLE> code. To manipulate individual cells, place the size attribute within the code for that cell.

How do I add height in TD?

HTMLheight AttributeThe HTML <td> height Attribute is used to specify the height of the table cell. If the <td> height attribute is not set then it takes default height according to content. Attribute Values: pixels: It sets the height of the table cell in terms of pixels.

How to increase column size in table in HTML?

The width of the columns i.e. td in a table can be fixed very easily. This can be done by adding the width attribute in the <td> tag. If the width is not specified, the width of the column changes according to the change in the content.


2 Answers

This sets the last row of cells to the correct height (demo):

function grow(td) {
    var table, target, high, low, mid;

    td = $(td);
    table = td.closest('table');
    target = table.height();
    low = td.height();

    // find initial high
    high = low;
    while (table.height() <= target) {
        td.height(high *= 2);
    }

    // binary search!
    while (low + 1 < high) {
        mid = low + Math.floor((high - low) / 2);
        td.height(mid);
        if (table.height() > target) {
            high = mid;
        } else {
            low = mid;
        }
    }

    td.height(low);
}

$('tr:last-child td').each(function() { grow(this); });

​ It should be trivial to convert this into plain JavaScript.


Update: For more complicated tables, you'll want to replace the last line with this (demo):

$.each($('td').get().reverse(), function() { grow(this); });

​ The idea is to call grow() on every cell, starting with the last row and working upwards.

like image 109
Jeffery To Avatar answered Oct 26 '22 18:10

Jeffery To


considering table id="mytable" it would be:

    $("#mytable").find("td").each(function(){ 

    var ContentHeight = $($(this).html()).height();
    $(this).height(ContentHeight);

 });
like image 32
Ashkan Mobayen Khiabani Avatar answered Oct 26 '22 16:10

Ashkan Mobayen Khiabani