如何强制表中的所有行具有相同的高度

时间:2011-04-15 00:09:38

标签: html css css-tables

我正在尝试构建一个html表但是我想强制所有行具有相同的高度(无论单元格中有多少内容)。如果一个单元格超出了空间,我希望它只是切断文本并隐藏其余部分。

这可能是使用CSS等吗?

4 个答案:

答案 0 :(得分:6)

仅限IE

CSS:

#fixedheight {
    table-layout: fixed;
}

#fixedheight td {
    height: 20px;
    overflow: hidden;
    width: 25%;
}

HTML:

<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>

通用解决方案

CSS:

#fixedheight {
    table-layout: fixed;
}

#fixedheight td {
    width: 25%;
}

#fixedheight td div {
    height: 20px;
    overflow: hidden;
}

HTML:

<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>

答案 1 :(得分:3)

将CSS height属性设置为您希望单元格高度的属性,并使用overflow: hidden(请参阅CSS overflow)以防止内容扩展单元格。

答案 2 :(得分:1)

给表格一个类:

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

在CSS中,尝试以下方法:

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

答案 3 :(得分:1)

您要设置的CSS样式是: display:block,min-height和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>