Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS text-overflow in a table cell?

Tags:

css

overflow

I want to use CSS text-overflow in a table cell, such that if the text is too long to fit on one line, it will clip with an ellipsis instead of wrapping to multiple lines. Is this possible?

I tried this:

td {   overflow: hidden;   text-overflow: ellipsis;   white-space: nowrap; } 

But the white-space: nowrap seems to make the text (and its cell) continually expand out to the right, pushing the total width of the table beyond the width of its container. Without it, however, the text continues to wrap to multiple lines when it hits the edge of the cell.

like image 758
daGUY Avatar asked Mar 20 '12 15:03

daGUY


People also ask

How do you change text overflow ellipsis in a table?

To make an ellipsis work on a table cell, you can use the CSS display property set to its "block" or "inline-block" value. In our example below, besides the display property, we set the text-overflow to "ellipsis", use the "nowrap" value of the white-space property, set the overflow to "hidden".

How do I hide the overflow text in a table cell?

To use the CSS overflow property with its "hidden" value on the HTML <td> element, you need to set the table-layout property with the "fixed" value and an appropriate width on the <table> element. Then, you need to add the overflow property set to "hidden" and white-space property set to "nowrap" on the table cell.

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.


1 Answers

To clip text with an ellipsis when it overflows a table cell, you will need to set the max-width CSS property on each td class for the overflow to work. No extra layout div elements are required:

td {  max-width: 100px;  overflow: hidden;  text-overflow: ellipsis;  white-space: nowrap; } 

For responsive layouts; use the max-width CSS property to specify the effective minimum width of the column, or just use max-width: 0; for unlimited flexibility. Also, the containing table will need a specific width, typically width: 100%;, and the columns will typically have their width set as percentage of the total width

table {width: 100%;} td {  max-width: 0;  overflow: hidden;  text-overflow: ellipsis;  white-space: nowrap; } td.column_a {width: 30%;} td.column_b {width: 70%;} 

Historical: For IE 9 (or less) you need to have this in your HTML, to fix an IE-specific rendering issue

<!--[if IE]> <style> table {table-layout: fixed; width: 100px;} </style> <![endif]--> 
like image 175
TFD Avatar answered Sep 24 '22 19:09

TFD