Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add text-overflow: ellipsis; to table cell

I'm having trouble containing text in a table. I want to give it a text-overflow: ellipsis. But I can't seem to get it to work.

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;
}

This isn't working though. it still increases the size of the td if text is too long

like image 725
Bitwise Avatar asked Apr 22 '17 16:04

Bitwise


People also ask

How do you overflow an ellipsis text?

To clip at the transition between characters you can specify text-overflow as an empty string, if that is supported in your target browsers: text-overflow: ''; . This keyword value will display an ellipsis ( '…' , U+2026 HORIZONTAL ELLIPSIS ) to represent clipped text.

How do I put text on top of a table cell in HTML?

To place an item at the top or bottom of its cell, insert the "VALIGN=" attribute within the code for that cell. To vertically align an entire row (e.g., placing all data in that row at the tops of the cells), insert the "VALIGN=" attribute within the code for that row.

How do you truncate text in a HTML table cell?

Within the table-cell that you want to apply truncation, simply include a container div with css table-layout: fixed. This container takes the full width of the parent table cell, so it even acts responsive. Make sure to apply truncation to the elements in the table.

How do you make a table with multiple lines on long text in CSS?

When the text in a single table cell exceeds a few words, a line break (<BR>) may improve the appearance and readability of the table. The line break code allows data to be split into multiple lines. Place the line break code <BR> within the text at the point(s) you want the line to break.


2 Answers

You will need table-layout: fixed along with width set for the table. Otherwise ellipsis can only work with fixed td width. And change the value of white-space: normal to nowrap.

Looks like, your table structure also needs to be fixed, missing <tr> in <thead>, and better to add <tbody> below <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>
like image 135
Stickers Avatar answered Oct 17 '22 15:10

Stickers


Placing a div inside your td tag and applying the styling to the div will achieve the text-overflow: ellipsis; So no need to apply a fixed layout to the table.

You could also designate how many lines you want to allocate before the ... ellipsis should go into effect.

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>
    <tr>
      <th style="width: 25%"></th>
       <!-- add your table headers with the desired width -->
    </tr>
  </thead>
  <tbody>
    <tr>
       <td><div class="max-lines-2"></div></td>
       <!-- add your table divs with the desired width -->
    </tr>
  </tbody>
 </table>
like image 21
chri3g91 Avatar answered Oct 17 '22 15:10

chri3g91