Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The text-overflow: ellipsis property can only work in combination with the overflow: hidden property. It is used to truncate long text and replace it with an ellipsis when it overflows the container. Therefore, if you want to use the text-overflow: ellipsis property, you must also set the overflow property to hidden.

However, if you want to avoid using the overflow: hidden property, you could consider using a JavaScript solution instead.

One possible approach is to measure the width of the text and the container and determine whether the text overflows the container. If it does, you can truncate the text and add an ellipsis at the end. Here's an example using jQuery:

$(function() {
  $('li').each(function() {
    var $li = $(this);
    var $span = $('<span>').text($li.text()).appendTo('body');
    var liWidth = $li.outerWidth();
    var textWidth = $span.width();
    $span.remove();

    if (textWidth > liWidth) {
      var truncatedText = $li.text().replace(/\s+?(\S+)?$/, '');
      $li.text(truncatedText + '...');
    }
  });
});

This code loops through each li element, measures the width of the text and the container, and truncates the text if necessary. It then adds an ellipsis at the end of the truncated text. Note that this approach requires jQuery and may have some performance issues if you have a large number of elements on the page.