Home >Web Front-end >CSS Tutorial >Why Doesn't 'text-overflow: ellipsis' Work on My Span Element?

Why Doesn't 'text-overflow: ellipsis' Work on My Span Element?

Barbara Streisand
Barbara StreisandOriginal
2024-11-10 09:35:02395browse

Why Doesn't

"text-overflow: ellipsis" Not Functional

When attempting to use "text-overflow: ellipsis" to truncate text within a element, the ellipsis does not appear when the window size is reduced.

Solution

To enable "text-overflow: ellipsis" functionality, it is necessary to include the following CSS properties in addition to "text-overflow":

  • Overflow: Ensures the element's content exceeds its containing area and is not hidden
  • Width (or max-width): Defines the maximum width of the element within which the text will be contained
  • Display: Specifies the display type of the element (e.g., block, inline-block)
  • White-space: Controls how spaces are handled within the element

Example Code

span {
    border: solid 2px blue;
    white-space: nowrap;
    text-overflow: ellipsis;
    width: 100px;
    display: block;
    overflow: hidden
}

Explanation

In the provided example, the element is given a width of 100px and the overflow property is set to "hidden," ensuring that any excess content is hidden within the element's boundaries. The display property is set to "block" to force the element to occupy a rectangular block of space. Finally, the white-space property is set to "nowrap" to prevent line breaks within the text.

With these properties applied, the "text-overflow: ellipsis" property becomes functional, truncating the text within the element and adding an ellipsis (...) when the window size is reduced to a point where the text exceeds the element's width.

The above is the detailed content of Why Doesn't 'text-overflow: ellipsis' Work on My Span Element?. 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