In my table I set the width of the first cell in a column to be 100px.
However, when the text in one of the cell in this column is too long, the width of the column becomes more than 100px. How could I disable this expansion?

推荐答案

我玩了一会儿,因为我搞不清楚.

您需要设置单元格宽度(thtd工作,我两者都设置),并将table-layout设置为fixed.出于某种原因,单元格宽度似乎只有在设置了表格宽度的情况下才会保持不变(我认为这很愚蠢,但没什么).

此外,将overflow属性设置为hidden也很有用,以防止表中出现任何额外的文本.

您还应该确保将所有的边框和大小都留给CSS.

好的,下面是我得到的:

table {
  border: 1px solid black;
  table-layout: fixed;
  width: 200px;
}

th,
td {
  border: 1px solid black;
  width: 100px;
  overflow: hidden;
}
<table>
  <tr>
    <th>header 1</th>
    <th>header 234567895678657</th>
  </tr>
  <tr>
    <td>data asdfasdfasdfasdfasdf</td>
    <td>data 2</td>
  </tr>
</table>

Here it is in JSFiddle

这家伙也有类似的问题:Table cell widths - fixing width, wrapping/truncating long words

Html相关问答推荐

如何将值从Google Sheets侧边栏中的表单中的输入传递到Google Sheets单元格中使用Textile()

应用于文本而不是弹性容器的Flexbox属性

如何使用CSS在<;表格中<;SVG&>?

高度:适合-内容不拉伸以适合内部长度

默认情况下使用Disbale Scroll,但在单击图标后将其激活

如何 Select 表格';TMS Web Core中的body html元素?

30000ms后超时重试:期望找到元素:someElement,但从未找到它

如何在 Bootstrap 5 中将两个导航栏元素放在末尾?

将箭头图标添加到工具提示包装器时遇到问题

如何根据剪辑路径边缘设置文本边距?

理解图像与其内容框之间的关系

我如何使用 html 在表格的单元格内实现下面显示的背景 colored颜色 加载器(请判断设计参考的图像链接)

并排对齐两个文本区域列

如何配置 prettier 使其以 Vue.js 模板语法的特定方式运行?

在 iPhone 上分隔 HTML 邮箱输入中的多个邮箱条目

如何将自定义图像插入 Shiny plot header?

当我将鼠标悬停在测试类上时,左侧类的 colored颜色 没有改变

如何让 backdrop-filter: blur() 效果在 iOS 上运行?

如何在 Tailwind CSS 中创建响应式网格布局?

CSS Padding 将右对齐的对象推离页面