CSS:固定行高

发布于 2024-08-12 05:13:56 字数 589 浏览 2 评论 0原文

我有这样的标记:

<style>
    table
    {
        border:1px solid black;
        width:400px;
        height:300px;
        border-collapse:collapse;
    }
    table tbody
    {
        border:1px solid red;
    }
    table td
    {
        background:yellow;
        padding:10px;
        border-bottom:1px solid green;
        height:20px;
    }
</style>


<table>
<tbody>
    <tr><td>test</td></tr>
    <tr><td>test</td></tr>
</tbody>
</table>

我需要的是行的高度不会拉伸。 有没有办法固定行高?

I'm having this markup:

<style>
    table
    {
        border:1px solid black;
        width:400px;
        height:300px;
        border-collapse:collapse;
    }
    table tbody
    {
        border:1px solid red;
    }
    table td
    {
        background:yellow;
        padding:10px;
        border-bottom:1px solid green;
        height:20px;
    }
</style>


<table>
<tbody>
    <tr><td>test</td></tr>
    <tr><td>test</td></tr>
</tbody>
</table>

What I need is that the rows won't stretch in height.
Is there a way to have a fixed row height?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夕色琉璃 2024-08-19 05:13:56

如果表格高度大于行的高度,HTML 表格行高通常会与表格高度成比例地变化。由于表格强制设置行高,因此您可以删除表格高度来解决该问题。如果这是不可接受的,您还可以给行明确的高度,并添加第三行,该行将自动调整到剩余的表格高度。

CSS2 中的另一个选项是 Max-Height 属性,尽管它可能会导致表格中出现奇怪的行为。http://www.w3schools.com/cssref/pr_dim_max-height.asp

HTML Table row heights will typically change proportionally to the table height, if the table height is larger than the height of your rows. Since the table is forcing the height of your rows, you can remove the table height to resolve the issue. If this is not acceptable, you can also give the rows explicit height, and add a third row that will auto size to the remaining table height.

Another option in CSS2 is the Max-Height Property, although it may lead to strange behavior in a table.http://www.w3schools.com/cssref/pr_dim_max-height.asp

.

大海や 2024-08-19 05:13:56

只需将 style="line-height:0" 添加到每个单元格即可。这在 IE 中有效,因为它将现有文本和不存在文本的行高设置为大约 19px,并强制单元格在大多数版本的 IE 中垂直扩展。无论是否有文本,IE 都需要执行此操作才能正确显示高度小于 20 像素的行。

Simply add style="line-height:0" to each cell. This works in IE because it sets the line-height of both existant and non-existant text to about 19px and that forces the cells to expand vertically in most versions of IE. Regardless of whether or not you have text this needs to be done for IE to correctly display rows less than 20px high.

一杯敬自由 2024-08-19 05:13:56

如果您需要的话,您也可以尝试一下:

<style type="text/css">
   ....
   table td div {height:20px;overflow-y:hidden;}
   table td.col1 div {width:100px;}
   table td.col2 div {width:300px;}
</style>


<table>
<tbody>
    <tr><td class="col1"><div>test</div></td></tr>
    <tr><td class="col2"><div>test</div></td></tr>
</tbody>
</table>

You can also try this, if this is what you need:

<style type="text/css">
   ....
   table td div {height:20px;overflow-y:hidden;}
   table td.col1 div {width:100px;}
   table td.col2 div {width:300px;}
</style>


<table>
<tbody>
    <tr><td class="col1"><div>test</div></td></tr>
    <tr><td class="col2"><div>test</div></td></tr>
</tbody>
</table>
悲歌长辞 2024-08-19 05:13:56

我还没有尝试过,但如果你在表格单元格集中放置一个 div,以便它在需要时具有滚动条,那么你可以在其中插入,在 div 上具有固定的高度,并且它应该使你的表格行保持固定高度。

I haven't tried it but if you put a div in your table cell set so that it will have scrollbars if needed, then you could insert in there, with a fixed height on the div and it should keep your table row to a fixed height.

缱绻入梦 2024-08-19 05:13:56
    
    table tbody
    {
        border:1px solid red;
    }
    table td
    {
        background:yellow;
        
        border-bottom:1px solid green;
        
        
    }
    .tr0{
        line-height:0;
     }
     .tr0 td{
        background:red;
     }
<table>
<tbody>
    <tr><td>test</td></tr>
    <tr><td>test</td></tr>    
    <tr class="tr0"><td></td></tr>
</tbody>
</table>

    
    table tbody
    {
        border:1px solid red;
    }
    table td
    {
        background:yellow;
        
        border-bottom:1px solid green;
        
        
    }
    .tr0{
        line-height:0;
     }
     .tr0 td{
        background:red;
     }
<table>
<tbody>
    <tr><td>test</td></tr>
    <tr><td>test</td></tr>    
    <tr class="tr0"><td></td></tr>
</tbody>
</table>

九厘米的零° 2024-08-19 05:13:56

此方法非常适合具有固定高度的行,并阻止表格布局移动。
您必须添加 来填充表格的其余部分,因此您的行不必展开并可以保存其固定高度。

<table class="table" >
  <tr class="fixed-height" >
    foo1
  </tr>
  <tr class="fixed-height" >
    foo2
  </tr>
  <tr class="fixed-height" >
    foo3
  </tr>
  <tr class="auto-height" > // responsible for filling up the table
  </tr>
</table>
.table {
  height: 80vh;
}

.fixed-height {
  height: 50px; /*fixed height for table rows */ 
}

.auto-height {
  height: calc(100% - 100px); /* Remaining height assigned to a non visible row */
}

This method works very well for having rows with fixed height, and stop the table layout shifting.
You will have to add a <tr class="auto-height"></tr> that will fill up the rest of the table for you, so your rows won't have to expand and can save their fixed height.

<table class="table" >
  <tr class="fixed-height" >
    foo1
  </tr>
  <tr class="fixed-height" >
    foo2
  </tr>
  <tr class="fixed-height" >
    foo3
  </tr>
  <tr class="auto-height" > // responsible for filling up the table
  </tr>
</table>
.table {
  height: 80vh;
}

.fixed-height {
  height: 50px; /*fixed height for table rows */ 
}

.auto-height {
  height: calc(100% - 100px); /* Remaining height assigned to a non visible row */
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文