我有一个单元格内有两个div的表.我希望第一个div与单元格的顶部对齐,最后一个div与底部对齐.下面是表格可能的样子的一个示例:

<table>
  <tr>
    <td class="left-cell">
      <div class="cell-container">
        <div class="first-div">
          This content should be at top of the cell, but not overlap
        </div>
        <div class="last-div">
          This content should be at bottom of the cell, but not overlap
        </div>
      </div>
    </td>
    <td>
      Long Content
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
    </td>
  </tr>
  
  <tr>
    <td class="left-cell">
      <div class="cell-container">
        <div class="first-div">
          This content should be at top of the cell, but not overlap
        </div>
        <div class="last-div">
          This content should be at bottom of the cell, but not overlap
        </div>
      </div>
    </td>
    <td>
      Shorter Content
    </td>
  </tr>
</table>

我的第一个try 是在表格单元格上使用相对定位,在底部div上使用绝对定位.当表格单元格的高度足以容纳所有内容时,这是可行的,但绝对定位会导致较小单元格的两个div重叠.

CodePen:https://codepen.io/Adam-Petersen/pen/mdgJwaG

接下来,我try 使用Flexbox,但在表格单元格中使用Flexbox时,似乎存在一些不寻常的呈现逻辑.这是我到目前为止想出的最好的try :

CodePen:https://codepen.io/Adam-Petersen/pen/NWmqgeZ

这在Chrome上运行得很好,但在Firefox上就不行了.我不明白为什么td上需要height: 0px才能证明我在Chrome上想要的内容是正确的,但如果不能在Firefox上工作,我将不得不想出其他的东西.

有没有什么方法可以让这个CSS与Firefox兼容,或者有没有另一种我完全没有考虑过的方法?

推荐答案

这是一种hacky,但设置table,td {height: 100%;}是可行的.

table, th, td {
  border: 1px solid black;
  border-collapse: collapse;
  height: 100%;
}

.left-cell {
  width: 300px;
}

.cell-container {
  height: 100%;
  display: flex;
  flex-flow: column wrap;
  justify-content: space-between;
}
<table>
  <tr>
    <td class="left-cell">
      <div class="cell-container">
        <div class="first-div">
          This content should be at top of the cell, but not overlap
        </div>
        <div class="last-div">
          This content should be at bottom of the cell, but not overlap
        </div>
      </div>
    </td>
    <td>
      Long Content
      <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>...
    </td>
  </tr>
  <tr>
    <td class="left-cell">
      <div class="cell-container">
        <div class="first-div">
          This content should be at top of the cell, but not overlap
        </div>
        <div class="last-div">
          This content should be at bottom of the cell, but not overlap
        </div>
      </div>
    </td>
    <td>
      Shorter Content
    </td>
  </tr>
</table>

Css相关问答推荐

如何将CSS Paint API与Reaction一起使用

Safari CSS扫描动画效果不起作用

如何在react 路由中设置活动类?

InertiaJS - Vue - Laravel:Using CSS Files

SVG文本在移动浏览器上增长并溢出

本机CSS嵌套混乱

基于高度的容器查询不起作用

有没有办法将px单位添加到 Sass mixin 的参数数组中?

更改 20 个实例中的一个组件的样式

Tailwind CSS 隐藏和可见

为什么同一个 CSS 类连续出现两次

semantic-ui-react 使输入使用全宽度可用

CSS背景位置从顶部的给定像素开始?

如何使用 TailwindCSS 禁用环形阴影?

基于变量动态生成 CSS 类 - SCSS

为什么标题中的特定 div 与 Odoo 13 中 PDF 中的页面重叠?

CSS中的旋转地球

Div Size 自动调整内容大小

CSS - 在 id 中 Select 类的语法

从 textarea 中删除所有样式(边框、发光)