我创建了一个由全部使用display: flex的div元素组成的表.我还在单元格元素上使用了display: flex,因为我使用了其他flex属性来居中文本.

我的问题是,表格单元格中的文本没有缩短,即使它在css中设置了text-overflow: ellipsis,而且溢出了.

从单元格元素中删除display: flex会使文本换行并实际使用text-overflow: ellipsis,但我真的希望这可以与Flexbox一起使用,并且我确信有一个解决方案.

.table {
  display: flex;
  flex-direction: column;
  width: fit-content;
  border: 1px solid #000;
}

.row {
  display: flex;
  height: 30px;
}

.row:not(:last-child) {
  border-bottom: 1px solid #000;
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-inline: 5px;
}

.cell:not(:last-child) {
  border-right: 1px solid #000;
}
<div class="table">
  <div class="row">
    <div class="cell"></div>
    <div class="cell">Column 1</div>
    <div class="cell">Column 2</div>
  </div>
  <div class="row">
    <div class="cell">1</div>
    <div class="cell">Text that is really long</div>
    <div class="cell">Text that is even longer than the really long one</div>
  </div>
  <div class="row">
    <div class="cell">2</div>
    <div class="cell">Text that is really long</div>
    <div class="cell">Text that is even longer than the really long one</div>
  </div>
  <div class="row">
    <div class="cell">3</div>
    <div class="cell">Text that is really long</div>
    <div class="cell">Text that is even longer than the really long one</div>
  </div>
</div>

推荐答案

text-overflow属性仅适用于块容器.您可以将文本换行到单元格内的单独元素中,然后将text-overflow: ellipsis应用于该元素.

...
.text-wrapper {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  width: 100%;
}
...
<div class="cell">
  <div class="text-wrapper">
    Text that is even longer than the really long one
  </div>
</div>

Html相关问答推荐

Tailwincss:自动设置网格高度

使用响应迅速的网格系统,将两列保持在同一行,同时允许更多列用于更大的屏幕

在悬停时应用文本装饰

我无法动态嵌入Instagram帖子,因为无法分配Instagram固定链接

如何将文本环绕心形(而不仅仅是圆形)?

悬停表格单元格文本时,使元素的字体大小更大,同时保持表格单元格的高度不变

按钮悬停效果不影响按钮内的图标

为什么 html 元素的 max-width 和 width 一起工作?

为什么可滚动弹性项目需要 flex-basis: 0 ?

如何创建淡出研磨背景

Tailwind 网格行高度可防止拉伸到最高行的所有相同高度

您如何动态更改 Vue 中更高级别的标签的 CSS

css 网格创建空行和列

修复 Vue 3 Component 中的 CSS 以显示后面的 Select 器样式而无需 !important

使用 R 中的 rvest 包获取搜索结果的第一个链接

获取 div 中每个元素的 href 并使用它

在身体外部创建 tanget 45° div

Bootstrap 5 英雄

按部分划分的表行带

为什么 `html` 上的 overflow: hidden 将滚动框移动到正文?