Open In App

How to Apply an Ellipsis to Multiline Text in CSS ?

Last Updated : 16 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In web design, showing only part of a long paragraph is a common need — especially when space is limited. While many developers know how to add an ellipsis (...) to a single line, about 80% are unsure how to do it for multiple lines. This article will show you how to apply an ellipsis to multiline text in CSS in a simple and clear way.

Here are three methods to apply an ellipsis to multiline text using CSS:

1. Using -webkit-line-clamp

The -webkit-line-clamp property is a convenient way to limit text to a specific number of lines and add an ellipsis.

HTML
<!--Driver Code Starts-->
<html>
<head>
    <style>
<!--Driver Code Ends-->

        .clamp {
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 3;
            overflow: hidden;
        }
    </style>

<!--Driver Code Starts-->
</head>
<body>
    <div class="clamp">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
    </div>
</body>
</html>

<!--Driver Code Ends-->
  • The -webkit-line-clamp: 3 limits the text to three lines and truncates the rest.
  • overflow: hidden ensures that overflowing text is not displayed.

2. Using max-height and overflow

You can use max-height combined with line-height to limit the visible lines.

HTML
<html>
<head>
    <style>
        .truncate {
            overflow: hidden;
            text-overflow: ellipsis;
            display: block;
            line-height: 1.5em;
            max-height: 4.5em; /* 3 lines * line-height */
        }
    </style>
</head>
<body>
    <div class="truncate">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
      Vivamus lacinia odio vitae vestibulum. Fusce volutpat odio nec luctus dapibus. 
      Praesent a vehicula sapien, ac feugiat nisl. Integer accumsan turpis at ligula fermentum.
    </div>
</body>
</html>
  • line-height defines the height of a single line.
  • max-height ensures only a specific number of lines are displayed.

3. Using JavaScript for Browser Compatibility

For broader compatibility, JavaScript can dynamically limit the text and add an ellipsis.

HTML
<html>
<head>
    <style>
        .js-truncate {
            overflow: hidden;
            text-overflow: ellipsis;
            display: block;
        }
    </style>
    <script>
        function truncateText(selector, lines) {
            const element = document.querySelector(selector);
            const lineHeight = parseFloat(getComputedStyle(element).lineHeight);
            element.style.maxHeight = `${lineHeight * lines}px`;
        }
        window.onload = () => truncateText('.js-truncate', 3);
    </script>
</head>
<body>
    <div class="js-truncate">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
      Vivamus lacinia odio vitae vestibulum. Fusce volutpat odio nec luctus dapibus. 
      Praesent a vehicula sapien, ac feugiat nisl. Integer accumsan turpis at ligula fermentum.
    </div>
</body>
</html>
  • JavaScript calculates the height dynamically based on the line count and applies it as a max-height.
  • This approach works in browsers without support for -webkit-line-clamp.

Note: It is ideal for browsers like firefox and IE that don't support -webkit-line-clamp, this method takes more effort but offers the best compatiblity and flexiblity.

Additional Notes:

  • Always test on multiple browsers to ensure proper behavior.
  • Keep responsive design in mind—multiline ellipsis may look different on mobile vs. desktop.
  • If content is hidden with ellipsis, consider adding a “Read more” or expand option for accessibility.
  • Avoid using ellipsis to hide critical information (like prices, errors, or links).
  • Use CSS gracefully—not every situation needs text to be trimmed.

Next Article

Similar Reads