【问题标题】:Hide overflow of column with ellipsis, shrink other cols to fit content, don't set any table/col widths, and expand the table to fit within a div用省略号隐藏列溢出,缩小其他列以适应内容,不设置任何表格/列宽,并扩展表格以适应 div
【发布时间】:2018-03-15 11:38:44
【问题描述】:

这是我的代码:

<style type="text/css">
div {
  width: 250px;
}

table {
  white-space: nowrap;
}

th {
  max-width: 175px;
  overflow: hidden;
  text-overflow: ellipsis;
}
th:hover {
  overflow: visible;
  position: relative;
}
th:hover span {
  background-color: white;
}
td {
  width: 100%;
}
</style>

<div>
  <table border="1">
    <tr>
      <th><span>Short Header</span></th>
      <td>value</td>
    </tr>
    <tr>
      <th><span>Quite Long Header</span></th>
      <td>value</td>
    </tr>
    <tr>
      <th><span>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</span></th>
      <td>value</td>
    </tr>
  </table>
</div>

这里有一个关于 JSFiddle 的例子:https://jsfiddle.net/3ptc3fL5/

请注意表格最后一行中的第一列是如何因太宽而无法容纳的,因此它会以省略号溢出。但是,如果您将鼠标悬停在它上面,它会展开以显示整个值。

但是,如果我摆脱 th 的 max-width 风格,一切都会崩溃。该表不再适合父 div。这是一个问题,因为我想在不必预先指定宽度的情况下达到同样的效果。

我的原因是我想创建一个通用实用程序类,我可以在不同响应页面的不同表格中使用。在一般情况下,我不知道父 div 的宽度(尤其是在响应式页面上),我不知道表格中其他列的宽度,我不想通过和在每种情况下手动计算出来,并在每次更改时保持最新。

我只希望除第一个列之外的所有列尽可能缩小而不会溢出,并且第一个列尽可能地扩展同时将表保持在父 div 内同时隐藏其内容的任何溢出都以省略号结尾。

我知道这里有大约 5000 个类似的问题,但在这一点上,我想我已经浏览了所有这些问题,但仍然没有找到任何可行的方法。 This is pretty close,但不考虑包装 div 的大小。有什么想法吗?

【问题讨论】:

  • 可能是错的,但我怀疑在某处不指定宽度的情况下是否有可能。您是否在标题上尝试过max-width: 20ch;?第二列是否比value更长?
  • 第二列可以比value长或短。也可以有 2 列以上。
  • 为了清楚起见......当有超过 2 列时,我希望额外的列都像 value 列一样处理(尽可能收缩而不溢出,然后让第一列尽可能扩大,但隐藏其溢出)。
  • 我刚刚在下面编辑了我的答案,请检查一下,希望对您有所帮助。

标签: html css


【解决方案1】:

我想这就是你要找的。检查sn-p。

编辑:在第一列添加一个绝对定位的 div 并使用 text-overflow: ellipsis; 并将其他列的宽度固定为 1px 或 1% 并使其成为 white-space: nowrap;

div {
  width: 400px;
}

table {
  width: 100%;
}

td, th {
  position: relative;
}

tr td:not(:first-child), tr th:not(:first-child){
    width:1%;
    white-space:nowrap;
}

.first-row{
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  white-space: nowrap;
  text-overflow: ellipsis;
}
<div>
  <table border="1">
    <tr>
      <th><div class="first-row">Header Loooooooooooooooooooooooooooooooooooooooooon</div></th>
      <th>First Value</th>
      <th>Value</th>
      <th>Value</th>
    </tr>
    <tr>
    <td><div class="first-row"> Loooooooooooooooooooooooooooooooooonooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text</div></td>
      <td>Value two</td>
      <td>Value</td>
      <td>Value</td>
    </tr>
    <tr>
    <td><div class="first-row">Second Loooooooooooooooooooooooooooooooooonooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text</div></td>
      <td>Value</td>
      <td>Value</td>
      <td>Value</td>
    </tr>
    <tr>
    <td><div class="first-row">Third Loooooooooooooooooooooooooooooooooonooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooog text</div></td>
      <td>Value</td>
      <td>Value</td>
      <td>Value</td>
    </tr>
  </table>
</div>

希望这会有所帮助。

【讨论】:

  • 如果 OP 明确指定 I just want all the columns except the first to shrink as much as possible without overflow, and for the first to column expand as much as it can while keeping the table inside the parent div while hiding any overflow of its contents with an ellipsis at the end.,为什么我会投反对票
  • 我会给你一个赞成票来抵消反对票,但你的答案不是我想要的。我看到i.imgur.com/ib4GhcS.png 没有做这部分——“除了第一个列之外的所有列都尽可能地缩小而不溢出”——相反,有大量的溢出,你甚至看不到任何一个的第一个字母其他列。
  • 我误解了您的 without overflow 逻辑,因为我认为您的意思是内容不应该超出单元格。无论如何,我已经编辑了我的答案并试图给你一个可能的解决方案。看看吧。
  • 哇,这真的有效!我不认为这是可能的!非常感谢你,你太棒了。
  • 我给了你一个 - 投反对票的是别人,我对此无能为力:)
【解决方案2】:

text-overflowapplies to block containers,所以它不能在表格单元容器上正常工作。您应该将文本包装在一个带有如下类的块容器中:

.ellip_wrap {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

已编辑:

我重构并删除了以下css:

.ellip_wrap:hover {
  overflow: visible;
  position: relative;
}
.ellip_wrap:hover span {
  background-color: white;
}

.table_wrap {
  width: 250px;
}
.table_item {
  table-layout: fixed;
  box-sizing: border-box;
  width: 100%;
}

.ellip_wrap {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
<div class="table_wrap">
  <table class="table_item" border="1">
    <tr>
      <th><div class="ellip_wrap">Short Header</div></th>
      <td>value</td>
    </tr>
    <tr>
      <th><div class="ellip_wrap">Quite Long Header</div></th>
      <td>value</td>
    </tr>
    <tr>
      <th><div class="ellip_wrap">MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</div></th>
      <td>value</td>
    </tr>
  </table>
</div>

【讨论】:

  • 谢谢!我试了一下jsfiddle.net/uneuz22w - 也许我做错了什么(如果是,请纠正我)但这也不太奏效。包含div.ellip_wrap 的th 扩展为填充整个容器div,因此整个表溢出。我希望整个表格都可以放入容器div。
  • 响应您的编辑,它仍然不是我想要的。第二列中有大量额外的空白,而第一列的部分内容是隐藏的。我希望第二列尽可能地缩小而不会溢出,然后剩余的空间应该用于第一列,如果需要的话可以溢出。我在想也许这是不可能的:)
【解决方案3】:

你可以这样做:

div {
  width: 250px;
  border:1px solid green;
}

table {
  white-space: nowrap;
  table-layout: fixed;/* added this*/
  width: 100%;/* added this*/
}

th {
  overflow: hidden;
  text-overflow: ellipsis;
}
<div>
  <table border="1">
    <tr>
      <th>Short Header</th>
      <td>value</td>
    </tr>
    <tr>
      <th>Quite Long Header</th>
      <td>value</td>
    </tr>
    <tr>
      <th>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</th>
      <td>value</td>
    </tr>
  </table>
</div>
<div style="width:300px">
  <table border="1">
    <tr>
      <th>Short Header</th>
      <td>value</td>
    </tr>
    <tr>
      <th>Quite Long Header</th>
      <td>value</td>
    </tr>
    <tr>
      <th>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</th>
      <td>value</td>
    </tr>
  </table>
</div>

<div style="width:300px">
  <table border="1">
    <tr>
      <th>Short Header</th>
      <td>value</td>
      <td>value</td>
    </tr>
    <tr>
      <th>Quite Long Header</th>
      <td>value</td>
      <td>value</td>
    </tr>
    <tr>
      <th>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</th>
      <td>value</td>
      <td>value</td>
    </tr>
  </table>
</div>
<div style="width:240px">
  <table border="1">
    <tr>
      <th>Short Header</th>
      <td>value</td>
      <td>value</td>
      <td>value</td>
      <td>value</td>
    </tr>
    <tr>
      <th>Quite Long Header</th>
      <td>value</td>
      <td>value</td>
      <td>value</td>
      <td>value</td>
    </tr>
    <tr>
      <th>MMMMMMMMMMMMMMMMMMMMMMMMMMMassiveHeader</th>
      <td>value</td>
      <td>value</td>
      <td>value</td>
      <td>value</td>
    </tr>
  </table>
</div>

【讨论】:

  • 谢谢,但这似乎不太奏效。例如i.imgur.com/smcjWgJ.png 看看第一个表,第二列中有大量额外的空白,而第一列的部分内容被隐藏了。正如我在帖子中所写的那样,“我只希望除第一个之外的所有列尽可能缩小而不会溢出,并且第一个列尽可能多地扩展,同时将表保持在父 div 内,同时隐藏任何溢出其内容的末尾带有省略号。”
  • @dumbmatter 在这种情况下,我认为使用 table 是不可能的 :) 这与 table 的工作方式背道而驰,所以不确定我们是否可以强制执行......顺便说一下,你有义务使用 table ?
  • 你可能是对的,这是不可能的,我只是通过提出这个问题来保持乐观:)。我想我不需要它是一个表格,但我确实需要它来显示表格数据(多行,其中每一行都有相同大小的列,其中一些大小是基于内容的动态)。如果无法使用表格,我仍然会对如何在没有表格的情况下进行操作感兴趣,尽管这会使其更难在我的网站上使用。
  • @RajanBenipuri 但你永远不会满足所有条件,这就是为什么用桌子强制东西有点不可行,我猜有更好的方法;)jsfiddle.net/adfcceca/3
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2014-06-19
  • 2018-06-15
  • 1970-01-01
  • 2019-07-22
相关资源
最近更新 更多