3

Sample table

<table>
    <tr>
        <td>Age</td>
        <td>12</td>
        <td>Gender</td>
        <td>Male</td>
        <td>Some long label</td>
        <td>Some long label value</td>
        ...
        ...
        could be more...
    </tr>
</table>

Without the width:100% content should fit to each column container but I would like to make the table expand across the whole page. Setting table width: 100% equally distributes the column. I would like to make each label (Labels: Age, Gender, Some long label) fit it's column container and the rest equally divided among themselves (Values: 12, Male, Some long label value).

I know setting <td width="5%">Age</td> or setting it in css should do the job but I think this is counter productive especially if you have to do that with a lot of columns.

Is there a way to accomplish this in css with lesser code, javascript or jquery maybe? Any hint or direction on how this can be done?

Note:

I've read this but I would like to avoid injecting width="%" inside html.

Community
  • 1
  • 1
splucena
  • 383
  • 3
  • 8
  • 17

4 Answers4

3

May be colgroup can help you out. Try this

The HTML is:

<table border = "1" cellspacing = "0" cellpadding = "0">
  <colgroup>
    <col>
    <col style="width:40%">
    <col>
    <col style="width:40%">
  </colgroup>
    <tr>
        <td>Age</td>
        <td>12</td>
        <td>Gender</td>
        <td>Male</td>
    </tr>
</table>
vibhu
  • 89
  • 8
2

If I understand you correctly, this is what you’re looking for:

table { width: 100% }
td:nth-child(odd) { width: 5% }

or if you want the content to be “hugged” by a cell:

table { width: 100% }
td:nth-child(odd) { width: 1%; white-space: nowrap }


$(function(){
        $(window).load(function() {
            updateCellWidth()
        })
        $(window).resize(function() {
            updateCellWidth()
        })
})

function updateCellWidth() {
    var width = 0, cols = 0
    $("table td:nth-child(even)").each(function(){
        ++cols
        width += $(this).width()
    })

    if (cols > 0) {
        var evenCellWidth=($("table").width()-width)/cols
        $("table td:nth-child(even)").css("width", evenCellWidth + "px")
    }
}
Jarod
  • 485
  • 5
  • 16
0

Are you looking for something like this.?

Then border-collapse, cellspacing & cellpadding might help you.

Ganesh Salunkhe
  • 596
  • 1
  • 4
  • 18
0

This is what I came up with. Thanks for the ideas.

<style type="text/css">
    table {
        border-collapse: collapse;
        width: 100%;
    }

    table td {
        border: 1px solid #000;
        white-space: nowrap;
    }
</style>
<body>
    <table id="tbl1">
        <tr>
            <td>Age</td>
            <td>12</td>
            <td>Gender</td>
            <td>Male</td>
            <td>Some Long Label</td>
            <td>Some Long Label Value</td>
        </tr>
    </table>
    <script type="text/javascript">
        var tbl = document.getElementById('tbl1');
        var rowCount = tbl.rows.length;
        var colCount = tbl.rows[0].cells.length;

        for (var i = 0 ; i < colCount; i++) {
            if (i%2 == 0) {

                tbl.rows[0].cells[i].style.width = tbl.rows[0].cells[i].innerHTML.length + 'px';

            }
        }
    </script>
splucena
  • 383
  • 3
  • 8
  • 17