如何截断固定宽度表格单元格中的文本?

发布于 2025-01-14 10:11:58 字数 1121 浏览 4 评论 0原文

我有一个包含 3 个表格单元格的表格。我的目标是为每个设置固定的宽度。如果 td 太长,中间的 td 也应该剪切文本,如果文本太长,则显示点。 (文本截断)

我尝试了不同的可能性(表固定、最大宽度等),但它仍然不起作用。也许有人可以帮助我。

/* table styles added by community for demonstration */
table {
  table-layout: fixed;
  width: 100%;
  border: 1px solid #ccc;
}

td:nth-child(2) {
  background: pink;
}

.cut-text {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<table>
  <td style="width: 10%">XXX</td>
  <td style="width: 70%" class="cut-text">LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT </td>
  <td style="width: 20%">XXX</td>
</table>

普通表格如下所示:

没有长文本的普通表格

如果中间的文本太长,我想显示像这样的点(我通过中间单元格中的 div 实现此目的。然后我设置 div 宽度并添加类“cut-text”。但是没有第三个单元格。)

带有长文本的表格(这里我错过了第三个td)

也许有人可以帮助我。

亲切的问候。

I have a table with 3 table cells. My goal is to set a fixed width for each. The td in the middle should also cut the text if its to long for the td and show dots if the text is too long. (text-truncate)

I tried different possibilities (table-fixed, max-width etc.), but it still doesn't work. Maybe someone can help me.

/* table styles added by community for demonstration */
table {
  table-layout: fixed;
  width: 100%;
  border: 1px solid #ccc;
}

td:nth-child(2) {
  background: pink;
}

.cut-text {
  text-overflow: ellipsis;
  white-space: nowrap;
  overflow: hidden;
}
<table>
  <td style="width: 10%">XXX</td>
  <td style="width: 70%" class="cut-text">LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT LOOOOOOOONG TEXT </td>
  <td style="width: 20%">XXX</td>
</table>

The normal Table looks like this:

Normal table without a long text

And if the text in the middle is to long I want to show dots like this (I achieve this with a div in the middle cell. Then I set the div width and added the class "cut-text". But then there is no third cell.)

Table with a long text (here i miss the third td)

Maybe someone can help me.

Kind regards.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文