Setting max-height for table cell contents

前端 未结 7 1299
庸人自扰
庸人自扰 2020-12-03 02:06

I have a table which should always occupy a certain percentage of the height of the screen. Most of the rows are of fixed height, but I have one row that should stretch to f

相关标签:
7条回答
  • 2020-12-03 02:46

    Possibly not cross browser but I managed get this: http://jsfiddle.net/QexkH/

    basically it requires a fixed height header and footer. and it absolute positions the table.

        table {
            width: 50%;
            height: 50%;
            border-spacing: 0;
            position:absolute;
        }
        td {
            border: 1px solid black;
        }
        #content {
            position:absolute;
            width:100%;
            left:0px;
            top:20px;
            bottom:20px;
            overflow: hidden;
        }
    
    0 讨论(0)
  • 2020-12-03 02:52

    Just put the labels in a div inside the TD and put the height and overflow.. like below.

    <table>
    <tr>
      <td><div style="height:40px; overflow:hidden">Sample</div></td>
      <td><div style="height:40px; overflow:hidden">Text</div></td>
      <td><div style="height:40px; overflow:hidden">Here</div></td>
    </tr>
    </table>
    
    0 讨论(0)
  • 2020-12-03 02:52

    I've solved just using this plugin: http://dotdotdot.frebsite.nl/

    it automatically sets a max height to the target and adds three dots

    0 讨论(0)
  • 2020-12-03 02:53

    What I found !!!, In tables CSS td{height:60px;} works same as CSS td{min-height:60px;}

    I know that situation when cells height looks bad . This javascript solution don't need overflow hidden.

    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-03 03:01

    I had the same problem with a table layout I was creating. I used Joseph Marikle's solution but made it work for FireFox as well, and added a table-row style for good measure. Pure CSS solution since using Javascript for this seems completely unnecessary and overkill.

    html

    <div class='wrapper'>
        <div class='table'>
            <div class='table-row'>
                <div class='table-cell'>
                    content here
                </div>
                <div class='table-cell'>
                    <div class='cell-wrap'>
                        lots of content here
                    </div>
                </div>
                <div class='table-cell'>
                    content here
                </div>
                <div class='table-cell'>
                    content here
                </div>
            </div>
        </div>
    </div>
    

    css

    .wrapper {height: 200px;}
    .table {position: relative; overflow: hidden; display: table; width: 100%; height: 50%;}
    .table-row {display: table-row; height: 100%;}
    .table-cell {position: relative; overflow: hidden; display: table-cell;}
    .cell-wrap {position: absolute; overflow: hidden; top: 0; left: 0; width: 100%; height: 100%;}
    

    You need a wrapper around the table if you want the table to respect a percentage height, otherwise you can just set a pixel height on the table element.

    0 讨论(0)
  • 2020-12-03 03:07

    We finally found an answer of sorts. First, the problem: the table always sizes itself around the content, rather than forcing the content to fit in the table. That limits your options.

    We did it by setting the content div to display:none, letting the table size itself, and then in javascript setting the height and width of the content div to the inner height and width of the enclosing td tag. Show the content div. Repeat the process when the window is resized.

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