html - 自动将单元格宽度调整为 CSS 中的内容并溢出

标签 html css html-table overflow

我有下表,每个单元格中的内容宽度不同。我想要 nowrap 和 overflow 旁边的 long。

我尝试了这里提到的内容:Why does overflow:hidden not work in a <td>?

但结果是所有单元格的宽度都相同,我需要单元格调整到内部内容,所以在内容较短的单元格(例如复选框单元格)中没有那么多的腰部空白。

我不能改为应用 div,而且我不能为每一列应用特定的宽度,因为它是一个动态表格,所以列数和内容可能会改变。

¿任何线索?

这是代码。查看example at JSfiddle

<html>
  <head>
    <style>
    table {
      width: 100%;
    }
    table td {
      border: 1px solid #ccc;
      max-width: 1px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    </style>
  </head>
  <body>
    <table>
      <tr>
        <td>
          <input type="checkbox" />
        </td>
        <td>
          ID001
        </td>
        <td>
          Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-
        </td>
      </tr>
    </table>
  </body>
</html>

最佳答案

table {
  width: 100%;
  table-layout: fixed;
  width: 100%;
}

table td:nth-of-type(1) {
  width: 20px;
  padding: 5px 0;
}
table td:nth-of-type(2) {
  width: 100px;
}
table td {
  border: 1px solid #ccc;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 5px 10px;
}
<table>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>
      ID001
    </td>
    <td>
      Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-Long-text-I-want-to-overflow-without-making-the-other-columns-wider-
    </td>
  </tr>
</table>

关于html - 自动将单元格宽度调整为 CSS 中的内容并溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43785175/

相关文章:

jquery - div 中的流体跨度宽度

css - IE10 和 IE11 饱和过滤器

html - 为什么使用表格进行网站布局如此邪恶

HTML 元素 COL、颜色和宽度 CSS 属性不起作用

javascript - replaceState() 与 pushState()

javascript - 更改 onmouseover 和 onmouseout 的过渡时间?

javascript - 删除 JavaScript 中的元素

css - div 内的 IE7 换行符弄乱了底部边距

html - IE6 中的 CSS 问题

javascript - 响应式表格设计: Make click() expand tr using JQuery