【问题标题】:CSS table width 100% and td overflow hidden with no td widthCSS 表格宽度 100% 和 td 溢出隐藏,没有 td 宽度
【发布时间】:2022-04-08 04:47:21
【问题描述】:

我有一个使用 100% 区域宽度的表格,它有一些隐藏溢出的 td,我需要的是当文本太长时将它们的文本省略。

我可以使用固定的 td 大小来做到这一点,但我需要使它们相对于内容

这个问题是 3 年前在这里提出的:CSS: 100% width table, with cells that have hidden overflow and irregular/minimal cell widths

没有回复,不知现在是否可以。

这是 jsFiddle 上的一个示例:https://jsfiddle.net/gd1fogee/

这是一个sn-p:

table{
  width:100%;
}
td{
	/*max-width: 150px !important; */
	text-overflow: ellipsis;
	white-space: nowrap;
	overflow: hidden;
      padding: 3px 5px;
}
<div style="width:400px;display:block;">
<table>
  <tr>
    <td>one</td><td>two</td><td>three</td>
  </tr>
  <tr>
    <td>this is a very very long text to show what i want to achieve</td><td>two</td><td>three</td>
  </tr>
</table>
</div>

提前感谢您的帮助!

【问题讨论】:

  • 我不知道你现在有没有你想要的,但如果没有,请举例说明你希望结果是什么样的(通过图片或硬编码示例)

标签: html css html-table overflow


【解决方案1】:

我不知道这是否是你想要的,但它就是这样。

https://jsfiddle.net/Tanikimura/4vypvwcn/

将文本溢出应用于 p 标签。

table{
  width:100%;
}
td {
	max-width: 150px !important; 
	
}
td p{
	
	text-overflow: ellipsis;
	white-space: nowrap;
	overflow: hidden;
      padding: 3px 5px;
}
<div style="width:400px;display:block;">
<table>
  <tr>
    <td>one</td><td>two</td><td>three</td>
  </tr>
  <tr>
    <td><p>this is a very very long text to show what i want to achieve</p></td><td>two</td><td>three</td>
  </tr>
</table>
</div>

【讨论】:

    【解决方案2】:

    当某些内容太大时,您无法真正使其大小相对于内容...因此您必须通过您选择的 max-width 方法来做到这一点。如果您在需要限制的列上放置一个类,可能会使它的大小相对更大,这将具有更大的内容。如果 tr 的特定子 td 只有 2 个左右的列将具有过大的内容,则定位它。否则将最大宽度应用于所有这些。

    【讨论】:

    • 不是一个特定的列,每个单元格都可以更长,类似于PHPMyAdmin的剪切文本,但它不是使用CSS而是使用PHP。
    • 您可以将其设置为表格布局:固定宽度为 100%;这将使每一列的宽度相同并且仍然具有响应性,并且它会使用您当前的代码制作省略号。然后,您不必为列设置特定宽度。您可以添加更多列而无需重新计算宽度。
    • 我试过了,好像 table-layout:fixed;使所有列的宽度相同,它们不会根据内容变大或变小:(
    • 那是因为你说随机单元格会有很多内容..所以你不能根据内容调整它们的大小,你必须给它们一个固定的大小。否则你可以做的是使用js来做。获取 td 的内容,如果超过一定数量的字符,将其截断,然后您可以将 ... 附加到截断字符串的末尾。但是,这意味着 td 将是最大内容的宽度,默认情况下是您提供的最大字符长度。 stackoverflow.com/questions/17249593/…
    • 使用 JS 是个不错的主意...我现在使用的是最大宽度,不是最好的解决方案,但它是...谢谢您的帮助!
    【解决方案3】:

    所以...我正在尝试为此考虑一个体面的 JS 解决方案。虽然这是一个需要改进的地方。如果有人看到这个并想改进它,请成为我的客人并在 cmets 中告诉我。

    const table = document.getElementsByTagName('table')[0];
    
    shrinkCells(table, .75);
    
    function shrinkCells(tbl, maxWidth) {
      // Check if it fits within the parent
      if (tbl.offsetWidth <= tbl.parentElement.offsetWidth || maxWidth < .2) {
        return tbl;
      }
      // set the maxWidth for every element that is bigger than the current maxwidth
      Array.from(table.getElementsByTagName('td')).forEach(function (el) {
        if (el.offsetWidth > (tbl.offsetWidth * maxWidth))
          el.style.maxWidth = (tbl.offsetWidth * maxWidth) + "px";
      });
      
      shrinkCells(tbl, maxWidth - .05);
    }
    table{
      width:100%;
      border-collapse: collapse;
    }
    td{
      border: solid 1px #000;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
      padding: 3px 5px;
    }
    <div style="width:400px;display:block;">
    <table>
      <tr>
        <td>one</td><td>two</td><td>three</td>
      </tr>
      <tr>
        <td>this is a very very long text to show what i want to achieve asdhudfaslkjdfhdf asdf asdfasdfa asdf asdfasdf asdf a</td><td>two asdf asd fasd fasd fradfsddf asdf asdf asd fdsaf</td><td>three</td>
      </tr>
    </table>
    </div>

    再一次,这是为了好玩而写的。让我知道可以进行哪些更改以使其变得更好。

    【讨论】:

      【解决方案4】:

      大约 5 年后,我仍然无法找到使用表格的完美解决方案,但我找到了使用网格及其列自动宽度的解决方法:

      grid-template-columns: repeat(3, auto);
      

      .grid{
          width:100%;
          display:grid;
          grid-template-columns: repeat(3, auto);
      }
      .grid div{
          /*max-width: 150px !important; */
          text-overflow: ellipsis;
          white-space: nowrap;
          overflow: hidden;
          padding: 3px 5px;
      }
      <div style="width:400px;display:block;">
        <div class="grid">
          <div>one</div><div>two</div><div>three</div>
          <div>this is a very very long text to show what i want to achieve</div><div>two</div><div>three</div>
        </div>
      </div>

      【讨论】:

        猜你喜欢
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2018-02-22
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        相关资源
        最近更新 更多