我在表格中包含文字时遇到问题。我想给它一个text-overflow: ellipsis
。但我似乎无法让它发挥作用。
HTML:
<table class="article-table">
<thead class="article-table__headers">
<th>Title</th>
<th>Source</th>
<th>Abstract</th>
<th>Folder</th>
</thead>
<% @articles.each do |article| %>
<tr class="article-table__rows">
<td><%= article.title %></td>
<td><%= article.source %></td>
<td><%= article.abstract %></td>
<td><%= article.folder %></td>
<td><%= link_to "Download File", download_article_path(article) %></td>
</tr>
<% end %>
</table>
CSS:
.article-table td {
width: 20%;
border-bottom: 1px solid #ddd;
text-overflow: ellipsis;
overflow: hidden;
white-space: normal;
}
这不行。如果文字太长,它仍会增加td的大小
答案 0 :(得分:2)
您需要table-layout: fixed
以及为表格设置的宽度。省略号省略号只能使用固定的td
宽度。并将white-space: normal
的值更改为nowrap
。
看起来,您的表格结构也需要修复,<tr>
中缺少<thead>
,最好在<tbody>
下面添加<thead>
。
.article-table {
table-layout: fixed;
width: 100%;
}
.article-table td {
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
}
<table class="article-table">
<thead>
<tr>
<th>Title</th>
<th>Source</th>
<th>Abstract</th>
<th>Folder</th>
</tr>
</thead>
<tbody>
<tr>
<td>The quick brown fox jumps over the lazy dog</td>
<td>The quick brown fox jumps over the lazy dog</td>
<td>The quick brown fox jumps over the lazy dog</td>
<td>The quick brown fox jumps over the lazy dog</td>
</tr>
</tbody>
</table>
答案 1 :(得分:0)
省略号不适用于:%(。article-table td&gt;宽度:20%; - 是你的问题)。
当我想要保持“%”到该div时,我的诀窍是带有省略号的td是添加2个不同的维度:
max-width:300px; /*Inspect element in broswer and see how many pixels is 20%*/
width: 20%;
在这种情况下,省略号工作和我的div,td具有我想要的尺寸。
试试.article-table td class:
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
答案 2 :(得分:0)
在td标签中放置一个div并将样式应用于div将实现文本溢出:省略号;因此,无需在表格上应用固定的布局。
您还可以指定在...省略号生效之前要分配多少行。
CSS
.max-lines-2 {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 2;
-webkit-box-orient: vertical;
}
HTML
<table style="width: 100%">
<thead>
<th style="width: 25%">
<!-- add your table headers with the desired width -->
</thead>
<tbody>
<tr>
<td><div class="max-lines-2"></div></td>
<!-- add your table divs with the desired width -->
</tr>
</tbody>
</table>