【发布时间】:2017-07-15 22:25:57
【问题描述】:
我有一个带有表格的页面,该表格会定期更新。该表有一个标题,后跟许多行。每行有 3 个字段:球员位置、球员姓名(也是球员个人资料的链接)和球员时间。表格占据了整个页面宽度。
html 看起来像这样:
<table id="raceTable">
<tbody>
<tr><th colspan="3">3 Players</th></tr>
<tr><td>1st</td><td><a>Link to profile of player A</a></td><td>00:22:12</td></tr>
<tr><td>2st</td><td><a>Link to profile of player B</a></td><td>00:23:12</td></tr>
<tr><td>3st</td><td><a>Link to profile of player C</a></td><td>00:24:15</td></tr>
</tbody>
</table>
现在,我想让表格可扩展。如果用户改变了窗口的宽度,表格应该被调整大小,中间列的文本不适合时应该被剪掉。
这是我尝试过的 CSS:
body{
overflow: hidden;
}
table {
width:100%;
line-height: 50px;
border-collapse: collapse;
}
th {
height: 50px;
width: 100%;
background-color: magenta;
}
td {
height: 50px;
}
#raceTable {
width: 100%;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(1) {
padding: 10px;
background-color: red;
float: left;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(2) {
width: 100%;
background-color: gray;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(2) > a{
display: block;
background-color: white;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
}
#raceTable > tbody > tr:not(:first-child) > td:nth-child(3) {
padding: 10px;
background: green;
float: right;
}
我在这里有一个 JSFiddle:http://jsfiddle.net/Gikkman/5wpts61p/17/
我的问题是,如何使text-overflow: ellipsis; 用于表格中间列的链接文本?我无法更改网站的 HTML 结构,我只能应用 CSS。这可能吗?
【问题讨论】:
-
文本溢出不适用于内联元素。将 a 重置为块或内联块 + 宽度。但 td 会完成这项工作
-
@GCyrillus 实际上 OP 已将 设置为在 css 中阻止,除了我的回答之外,如果您想提出建议,我希望看到任何更简单的方法。
-
@Pangloss 我想我在阅读 css 和浮动右/左时感到困惑我将 display:block 用于 td ...否则基本想法是 jsfiddle.net/5wpts61p/18 这也是你提出的:)(表格布局:固定)
-
在使用 table-layout: fixed 时是否可以明确设置左右表格行的宽度(例如左侧 30px 和右侧 100px)?
-
是的,如果您使用一些额外的标记来设置每个列的宽度(或避免 colspan 属性),我会在下面用 sn-p 回答
标签: html css html-table ellipsis