在响应表text-overflow:ellipsis
中,当数据增加时th
(随着col-xs-2
宽度增加),该表不起作用。
代码如下:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th class="col-xs-2" style="text-overflow: ellipsis;">Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</th>
<th class="col-xs-1">Firstname</th>
<th class="col-xs-1"> Lastname</th>
<th class="col-xs-4">Age</th>
<th class="col-xs-2">City</th>
<th class="col-xs-2">Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td>USA</td>
</tr>
</tbody>
</table>
</div>
text-overflow属性仅影响沿其内联进度方向MDN溢出了块容器元素的内容
为了text-overflow
工作,text-overflow: ellipsis
单独指定不会有什么好处-您应该同时使用以下样式:
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
span, div, th, td {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
max-width: 100px;
}
<span>Inline element overflow ellipsis do not work</span>
<div>Block elements overflow ellipsis works</div>
<table>
<tr><th>Table - Overflow test</th></tr>
<tr><td>This is a long text</td><td>This is a long text</td></tr>
</table>
因此,text-overflow
适用于block
元素,但是td
是一个table-cell
元素-表始终棘手的处理,因为他们使用的渲染默认表格布局算法。调整表格及其单元格的宽度以适合其内容。
通常,指定用于获取省略号的常规属性可能会起作用:
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
如果它们不起作用,或者您开始发现表算法对您有欺骗作用,那么您可以将它们与max-width: 0
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
max-width: 0;
.table .text {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
max-width: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th class="col-xs-2 text">
Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum
</th>
<th class="col-xs-1">Firstname</th>
<th class="col-xs-1">Lastname</th>
<th class="col-xs-4">Age</th>
<th class="col-xs-2">City</th>
<th class="col-xs-2">Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td>USA</td>
</tr>
</tbody>
</table>
</div>
另一个技巧是将文本与伪元素一起包装在和一起放置的span
位置。absolute
td
width: 100%
inline-block
.table .text {
position: relative;
}
.table .text span {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
position: absolute;
width: 100%;
}
.text:before {
content: '';
display: inline-block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<div class="table-responsive">
<table class="table">
<thead>
<tr>
<th class="col-xs-2 text">
<span>
Lorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem IpsumLorem Ipsum</span>
</th>
<th class="col-xs-1">Firstname</th>
<th class="col-xs-1">Lastname</th>
<th class="col-xs-4">Age</th>
<th class="col-xs-2">City</th>
<th class="col-xs-2">Country</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Anna</td>
<td>Pitt</td>
<td>35</td>
<td>New York</td>
<td>USA</td>
</tr>
</tbody>
</table>
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句