Setting a max height on a table

前端 未结 9 1325
猫巷女王i
猫巷女王i 2020-12-29 01:10

I am trying to design a page where there are some tables. It seems that styling tables is much more painful than it ought to be.

The problem is the following: The ta

相关标签:
9条回答
  • 2020-12-29 01:45

    In Tables, For minimum table cells height or rows height use css height: in place of min-height:

    AND

    For Limiting max-height of all cells or rows in table with Javascript:

    This script is good for horizontal overflow tables.

    This script increase the table width 300px each time (maximum 4000px) until rows shrinks to max-height(160px) , and you can also edit numbers as your need.

    var i = 0, row, table = document.getElementsByTagName('table')[0], j = table.offsetWidth;
    while (row = table.rows[i++]) {
        while (row.offsetHeight > 160 && j < 4000) {
            j += 300;
            table.style.width = j + 'px';
        }
    }
    

    Source: HTML Table Solution Max Height Limit For Rows Or Cells By Increasing Table Width, Javascript

    0 讨论(0)
  • 2020-12-29 01:46
    <table  style="border: 1px solid red">
                <thead>
                    <tr>
                        <td>Header stays put, no scrolling</td>
                    </tr>
                </thead>
                <tbody id="tbodyMain" style="display: block; border: 1px solid green; height: 30px; overflow-y: scroll">
                    <tr>
                        <td>cell 1/1</td>
                        <td>cell 1/2</td>
                    </tr>
                    <tr>
                        <td>cell 2/1</td>
                        <td>cell 2/2</td>
                    </tr>
                    <tr>
                        <td>cell 3/1</td>
                        <td>cell 3/2</td>
                    </tr>
                </tbody>
            </table>
    


    Javascript Section

    <script>
    $(document).ready(function(){
       var maxHeight = Math.max.apply(null, $("body").map(function () { return $(this).height(); }).get());
       // alert(maxHeight);
        var borderheight =3 ; 
        // Added some pixed into maxheight 
        // If you set border then need to add this "borderheight" to maxheight varialbe
       $("#tbodyMain").css("min-height", parseInt(maxHeight + borderheight) + "px");             
    });
    
    </script>
    


    please, refer How to set maximum possible height to your Table Body
    Fiddle Here

    0 讨论(0)
  • 2020-12-29 01:58

    NOTE this answer is now incorrect. I may get back to it at a later time.

    As others have pointed out, you can't set the height of a table unless you set its display to block, but then you get a scrolling header. So what you're looking for is to set the height and display:block on the tbody alone:

    <table style="border: 1px solid red">
        <thead>
            <tr>
                <td>Header stays put, no scrolling</td>
            </tr>
        </thead>
        <tbody style="display: block; border: 1px solid green; height: 30px; overflow-y: scroll">
            <tr>
                <td>cell 1/1</td>
                <td>cell 1/2</td>
            </tr>
            <tr>
                <td>cell 2/1</td>
                <td>cell 2/2</td>
            </tr>
            <tr>
                <td>cell 3/1</td>
                <td>cell 3/2</td>
            </tr>
        </tbody>
    </table>
    

    Here's the fiddle.

    0 讨论(0)
提交回复
热议问题