添加文本溢出:省略号;到表格单元格

Bit*_*ise 5 html css html-table ellipsis

我在表格中包含文本时遇到问题。我想给它一个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>
Run Code Online (Sandbox Code Playgroud)

CSS:

.article-table td {
    width: 20%;
    border-bottom: 1px solid #ddd;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: normal;
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。如果文本太长,它仍然会增加 td 的大小

Sti*_*ers 10

您将需要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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)