在我的表格中,我设置列中第一个单元格的宽度为100px。 但是,当此列中的某个单元格中的文本太长时,列的宽度就会超过100px。如何禁用此扩展?
当前回答
我玩了一会儿,因为我搞不懂。
你需要设置单元格宽度(th或td工作,我设置两者)和设置表格布局固定。出于某种原因,单元格宽度似乎只有在设置表宽度时才会保持固定(我认为这很愚蠢,但无所谓)。
此外,将overflow属性设置为hidden也很有用,可以防止任何额外的文本从表中输出。
你也应该确保所有的边界和大小都留给CSS。
好了,这是我得到的:
表{ 边框:1px纯黑色; 表布局:固定; 宽度:200 px; } th, td { 边框:1px纯黑色; 宽度:100 px; 溢出:隐藏; } <表> < tr > < th >头1 < / th > < th >头234567895678657 < / th > < / tr > < tr > < td >数据asdfasdfasdfasdfasdf td > < / < td >数据2道明> < / < / tr > 表> < /
在JSFiddle中
这个家伙有一个类似的问题:表单元格宽度-固定宽度,换行/截断长单词
其他回答
根据我在这里的回答,也可以使用一个表头(可以是空的),并为每个表头单元应用相对宽度。表体中所有单元格的宽度将与其列头的宽度一致。例子:
HTML
<table>
<thead>
<tr>
<th width="5%"></th>
<th width="70%"></th>
<th width="15%"></th>
<th width="10%"></th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Some text...</td>
<td>May 2018</td>
<td>Edit</td>
</tr>
<tr>
<td>2</td>
<td>Another text...</td>
<td>April 2018</td>
<td>Edit</td>
</tr>
</tbody>
</table>
CSS
table {
width: 600px;
border-collapse: collapse;
}
td {
border: 1px solid #999999;
}
查看结果
或者,按照Hyathin的回答建议使用colgroup。
在餐桌上找到合适的尺寸是很棘手的。唯一对我有效的方法是使用表格布局:fixed;与每条线的指定宽度相结合。和宽度:auto;在你不介意生长的一列上。
下面是一个使用无类表的示例。如果您正在处理一些动态列,则需要一个类化版本。
table { table-layout: fixed; } th,td { text-align: left; vertical-align: top; /* use this in the columns where you're not concerned with new lines */ word-break: break-word; } th:first-child, th:last-child { width: 5ch; text-align: center; } th:nth-child(n + 2):nth-child(-n + 3) { background: red; width: 25%; } th:nt-child(4) { background: blue; width: auto; } <table> <thead> <tr> <th>ID</th> <th>First Name</th> <th>Last Name</th> <th>Description</th> <th>Age</th> </tr> </thead> <tbody> <tr> <th>1</th> <th>John</th> <th>Smith</th> <th>Vivamus fermentum elit purus, eget egestas nunc convallis ac. Vestibulum faucibus dolor nunc, vitae rutrum mauris porta at. Ut id ante quis lectus consectetur interdum vel in leo. Ut ut convallis ipsum, quis aliquet erat. Maecenas ipsum dolor, rhoncus et ultrices a</th> <th>30</th> </tr> <tr> <th>2</th> <th>John</th> <th>Smith</th> <th>Vivamus fermentum elit purus, eget egestas nunc convallis ac. Vestibulum faucibus dolor nunc, vitae rutrum mauris porta at. Ut id ante quis lectus consectetur interdum vel in leo. Ut ut convallis ipsum, quis aliquet erat. Maecenas ipsum dolor, rhoncus et ultrices a</th> <th>30</th> </tr> <tr> <th>3</th> <th>John</th> <th>Smith</th> <th>Vivamus fermentum elit purus, eget egestas nunc convallis ac. Vestibulum faucibus dolor nunc, vitae rutrum mauris porta at. Ut id ante quis lectus consectetur interdum vel in leo. Ut ut convallis ipsum, quis aliquet erat. Maecenas ipsum dolor, rhoncus et ultrices a</th> <th>30</th> </tr> <tr> <th>4</th> <th>John</th> <th>Smith</th> <th>Vivamus fermentum elit purus, eget egestas nunc convallis ac. Vestibulum faucibus dolor nunc, vitae rutrum mauris porta at. Ut id ante quis lectus consectetur interdum vel in leo. Ut ut convallis ipsum, quis aliquet erat. Maecenas ipsum dolor, rhoncus et ultrices a</th> <th>30</th> </tr> </tbody> </table>
我玩了一会儿,因为我搞不懂。
你需要设置单元格宽度(th或td工作,我设置两者)和设置表格布局固定。出于某种原因,单元格宽度似乎只有在设置表宽度时才会保持固定(我认为这很愚蠢,但无所谓)。
此外,将overflow属性设置为hidden也很有用,可以防止任何额外的文本从表中输出。
你也应该确保所有的边界和大小都留给CSS。
好了,这是我得到的:
表{ 边框:1px纯黑色; 表布局:固定; 宽度:200 px; } th, td { 边框:1px纯黑色; 宽度:100 px; 溢出:隐藏; } <表> < tr > < th >头1 < / th > < th >头234567895678657 < / th > < / tr > < tr > < td >数据asdfasdfasdfasdfasdf td > < / < td >数据2道明> < / < / tr > 表> < /
在JSFiddle中
这个家伙有一个类似的问题:表单元格宽度-固定宽度,换行/截断长单词
只需在<td>或<th>定义宽度<div>内添加<div>标签。 这对你有帮助。别的都不管用。
eg.
<td><div style="width: 50px" >...............</div></td>
如果您需要一个或多个固定宽度的列,而其他列需要调整大小,请尝试将min-width和max-width设置为相同的值。
推荐文章
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 设备像素比到底是什么?
- 我如何做一个半透明的背景?
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 在CSS网格布局中等高行
- $(window).width()与媒体查询不一样
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?
- 输入触发器按钮单击
- 如何以及在哪里使用::ng-deep?
- Angular 2模板中的标签是什么意思?