Home > Web Front-end > CSS Tutorial > How to Implement CSS Text Overflow with Ellipsis in a Table Cell?

How to Implement CSS Text Overflow with Ellipsis in a Table Cell?

Patricia Arquette
Release: 2024-12-28 18:43:11
Original
914 people have browsed it

How to Implement CSS Text Overflow with Ellipsis in a Table Cell?

CSS text-overflow in a Table Cell

The goal is to implement CSS text overflow in a table cell, ensuring that text exceeding the cell's width is clipped with an ellipsis, preventing it from wrapping to multiple lines.

Attempt:

An initial attempt was made using the following CSS:

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

However, the white-space: nowrap property caused the text and its cell to expand indefinitely to the right, exceeding the table container.

Solution:

To achieve the desired effect, one must set the max-width CSS property for each table cell. Here's an example:

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

Setting the max-width ensures that the overflow is contained within the specified width.

Responsive Layouts:

For responsive layouts, consider using max-width to specify the minimum width of the column or setting it to max-width: 0; for full flexibility. The containing table should have a specific width, such as width: 100%;.

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%;}
Copy after login

IE Considerations:

For IE 9 or earlier, the following HTML hack is necessary to resolve a rendering issue:

<!--[if IE]>
<style>
table {table-layout: fixed; width: 100px;}
</style>
<![endif]-->
Copy after login

The above is the detailed content of How to Implement CSS Text Overflow with Ellipsis in a Table Cell?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template