html - 使一列单元格确定行高

标签 html css html-table

我有一个包含两列的表格,我希望每行的高度由右列中内容的高度决定,这样左列中的单元格就会被垂直截断。

我可以在 CSS 中做到这一点吗?我已经尝试在左侧 td 设置 max-height=100%,并在左侧 td 使用 div 并在其上设置 max-height=100%。但行的大小仍会调整以适应最大的单元格,而不是忽略左侧单元格的大小。

最佳答案

您可以将 div 设置为 position:absoluteoverflow:hidden等,并设置一些 widthtd 上以防止它与内部唯一的绝对 div 一起折叠。

table {
  width: 200px;
}
td {
  width: 50%;
}
td:first-child {
  position: relative;
}
td:first-child  > div {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  overflow: hidden;
}
<table border="1">
  <tr>
    <td>
      <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</div>
    </td>
    <td>Lorem ipsum</td>
  </tr>
</table>

关于html - 使一列单元格确定行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42403500/

相关文章:

javascript - :even pseudo selector in jquery 的意外行为

html - knockout : nest input element in label tag with data-bind properties

javascript - 如何打开 div 并使消息可读?

html - 将 DIV 扩展到父列表项之外

jquery - 在调整大小时将元素保持在窗口的水平点上

php - 按行编辑表格

html - 根据所用设备的屏幕尺寸在电子邮件中加载图像

Javascript:用简单的代码展开/折叠多个 DIV?

html - 如何撤消对 contenteditable div 上的脚本所做的更改

css - 在表格中输入 TAG,CSS 类根本不起作用