Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 993
leora Avatar asked Apr 15 '11 00:04

leora


4 Answers

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>
like image 197
ohmusama Avatar answered Oct 20 '22 10:10

ohmusama


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.

like image 20
Henry Merriam Avatar answered Oct 20 '22 08:10

Henry Merriam


Give the table a class:

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

And in the CSS, try the following:

table.myTable td {
  height: 20px;
  overflow: hidden;
}
like image 22
Alp Avatar answered Oct 20 '22 10:10

Alp


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>
like image 44
alignedfibers Avatar answered Oct 20 '22 08:10

alignedfibers