Home >Web Front-end >CSS Tutorial >How Can I Clip Text with an Ellipsis in Table Cells Using CSS?

How Can I Clip Text with an Ellipsis in Table Cells Using CSS?

Susan Sarandon
Susan SarandonOriginal
2024-12-22 07:30:10837browse

How Can I Clip Text with an Ellipsis in Table Cells Using CSS?

Clipping Text with Ellipsis in Table Cells Using CSS

Question:

Can you clip text in a table cell with an ellipsis instead of allowing it to wrap to multiple lines?

Attempted Solution:

Using the CSS properties overflow: hidden, text-overflow: ellipsis, and white-space: nowrap didn't work as intended.

Answer:

To successfully clip overflowing text in a table cell, you need to add the max-width property to the td class:

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

This sets the maximum width for each table cell and applies the other properties to ensure that it clips with an ellipsis when the text exceeds the specified width.

In responsive layouts, you can use a dynamic maximum width:

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

Additionally, the containing table requires a specific width:

table {
  width: 100%;
}

The table cell widths can be set as percentages:

td.column_a {
  width: 30%;
}
td.column_b {
  width: 70%;
}

For older versions of Internet Explorer (IE 9 or less), this additional code is necessary to fix a rendering issue:

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

The above is the detailed content of How Can I Clip Text with an Ellipsis in Table Cells Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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