开发者

How can I force all rows in a table to have the same height

I am trying to build a html table but I want to force all rows to have the same height (no matter how much content is in the cells). If a cell overruns the space, I want it to just cut off the text开发者_高级运维 and hide the rest.

Is this possible using CSS, etc?


IE only

    #fixedheight {
        table-layout: fixed;
    }
    
    #fixedheight td {
        height: 20px;
        overflow: hidden;
        width: 25%;
    }
    <table id="fixedheight">
        <tbody>
            <tr>
                <td>content</td>
                <td>lots of content that should spend way more time wrapping down than it should if I were just to have a short bit of stuff, that would be invaded by zombies and the such</td>
                <td>more content</td>
                <td>small content</td>
                <td>enough already</td>
            </tr>
        </tbody>
    </table>


Universal solution

    #fixedheight {
        table-layout: fixed;
    }
    
    #fixedheight td {
        width: 25%;
    }
    
    #fixedheight td div {
        height: 20px;
        overflow: hidden;
    }
    
    <table id="fixedheight">
        <tbody>
            <tr>
                <td>
                    <div>content</div>
                </td>
                <td>
                    <div>lots of content that should spend way more time wrapping down than it should if I were just to have a short bit of stuff, that would be invaded by zombies and the such</div>
                </td>
                <td>
                   <div>more content</div>
                </td>
                <td>
                   <div>small content</div>
                </td>
                <td>
                   <div>enough already</div>
                </td>
            </tr>
        </tbody>
    <table>


Set the CSS height property to what you want the cell heights to be, and use overflow: hidden (see CSS overflow) to prevent contents from expanding the cells.


Give the table a class:

<table class="myTable">...</table>

And in the CSS, try the following:

table.myTable td {
  height: 20px;
  overflow: hidden;
}


The CSS Styles you will want to set are: display:block, min-height, and max-height.

    <!DOCTYPE html>
      <html>
        <head>
          <meta charset="utf-8">
          <meta name="viewport" content="width=device-width">
          <title>JS Bin</title>
          <style>
            html{font-size:16px;}
            table{}
            table tr{
              display:block;
              border-bottom:solid green 1px;    
              height:.8em;
              min-height:.8em;
              max-height:.8em;
              background-color:#E300E3;
              overflow:hidden;
           }
         </style>
       </head>
       <body>
         <table id="MyTable">
           <tr><td>16px Font-Size</td><td>Column2</td></tr>
         </table>
       </body>
     </html>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜