Home > Web Front-end > CSS Tutorial > How Can I Truncate Long Strings with CSS and Display an Ellipsis Across Browsers?

How Can I Truncate Long Strings with CSS and Display an Ellipsis Across Browsers?

DDD
Release: 2024-12-17 12:44:26
Original
410 people have browsed it

How Can I Truncate Long Strings with CSS and Display an Ellipsis Across Browsers?

Truncating Long Strings with CSS: A Cross-Browser Solution

Truncating dynamic text to fit within fixed layouts can be a challenge. No native CSS property provides cross-browser support for truncating text and displaying an ellipsis. However, creative workarounds can achieve this functionality.

Justin Maxwell's Ellipsis Technique

Justin Maxwell developed a CSS solution that works across browsers, but it has one limitation: it does not allow text selection in Firefox.

.ellipsis {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    -o-text-overflow: ellipsis;
    -moz-binding: url('assets/xml/ellipsis.xml#ellipsis');
}
Copy after login

The ellipsis.xml file contains an XBL markup that implements the ellipsis functionality.

Updating Node Content

In Firefox, updating the content of a node using innerHTML can break the ellipsis functionality. To address this, use the following code:

function replaceEllipsis(node, content) {
    node.innerHTML = content;
    // Check for the gecko browser
    if (YAHOO.env.ua.gecko) {
        var pnode = node.parentNode,
            newNode = node.cloneNode(true);

        pnode.replaceChild(newNode, node);
    }
}
Copy after login

Note: Text-overflow: ellipsis is now supported natively in Firefox 7. This update is a welcome improvement, but the cross-browser CSS solution remains a useful reference for older browsers.

The above is the detailed content of How Can I Truncate Long Strings with CSS and Display an Ellipsis Across Browsers?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template