Show text with condensed lines in CSS without breaking words

Within this div, I have a long text that needs to be displayed on multiple lines based on the fixed width and height of the div. The challenge is to ensure that words do not break in the middle when moving to the next line, and also add an ellipsis at the end of the last sentence.

To achieve this using CSS:

white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;

However, this results in displaying the text in one line even when it should span across multiple lines within the div.

An alternative approach could be:

word-wrap: break-word;

While this method breaks the words and displays the sentences on several lines as intended, it does not include an ellipsis at the end to indicate truncated text.

Is there a way to achieve the following requirements?

1) Displaying multiple sentences within the div's height

2) Preventing word breaks within sentences

3) Adding an ellipsis at the end for text truncation

For an example, refer to this jsfiddle link: https://jsfiddle.net/vghup5jf/1/ where currently only one line is displayed.

Answer №1

Ellipsis may not work effectively for multiple lines and achieving this effect with pure CSS is not possible in all browsers. However, there is a clever workaround that works specifically on Chrome and Safari due to their -webkit rendering engines.

.ellipsis {
    display: block;
    display: -webkit-box;
    max-width: 400px;
    max-height: 100px;
    font-size: 20px;
    line-height: 1.4;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}
<div class="ellipsis">
     <span>
       I have a lengthy text that needs to be displayed within a div with specific dimensions. The goal is to show the text in multiple lines (matching the height of the div) without breaking words across lines. Additionally, an ellipsis should be added at the end of the last visible sentence.
      </span>
</div>

To ensure cross-browser compatibility, it's recommended to use either SASS or JavaScript.

Using JS

https://css-tricks.com/line-clampin/

Using CSS-SASS

http://codepen.io/martinwolf/pen/qlFdp

Answer №2

Big shoutout to Superman for the help, here is the end result:

To fix the issue, simply include the following CSS:

display: -webkit-box !important; -webkit-line-clamp: 2 !important;
   -webkit-box-orient: vertical !important;

Make sure to delete: white-space: nowrap;

http://jsfiddle.net/h7m3zrpq/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I want to create a custom jQuery slider totally from scratch

Greetings everyone, I have been tasked with creating a slider using only HTML / jQuery code. Here is the template: And here is the HTML code for the template above: <div id="viewport-container"> <section id="sliding-container"> & ...

Guide on implementing an onClick event for rows in an Angular Mat-Table and navigating to a separate page

I'm seeking guidance on how to implement a click event for a specific row that will navigate to the next form while carrying over the values of the clicked row. I find this concept a bit perplexing at the moment. <!-- Position C ...

Aligning container - property: inline-block;

Having trouble centering two divs that are set to "display: inline-block;" in your code? Removing the inline-block class centers them, but they end up stacking vertically instead of horizontally. See below for an example: #news { background-image: url ...

Unable to execute xmlHttp.responseXML on a server that is offline

Currently, I am diving into the world of AJAX and XML. However, I recently encountered a frustrating issue. I attempted to create a basic program that would display everything I input into an input box within a <div>. Strangely enough, my program fa ...

There seems to be a clash between Modal Semantic UI React and Bootstrap causing issues

I created a project using .net core MVC. I have integrated the semantic-ui-react modal by importing its library and linking the CSS for it to function properly. However, I encountered an issue where the bootstrap CSS was conflicting with the CDN for semant ...

Photos on Display are Misaligned

I'm currently in the process of developing a responsive photo gallery based on this tutorial. Within my project, there exists a component known as MemoryList which is defined as shown below: import React from 'react'; import _ from 'lo ...

Python Selenium - encountering issues with interactability of elements

Trying to complete the form on () is proving tricky for me. When I utilize Javascript, document.getElementsByName("userName")[0].value = "Hello", I am successful in entering text into a form. However, when I apply the same logic in Sel ...

Generating DOM elements at specific intervals using Jquery

I am looking to dynamically create 1 div container every x seconds, and repeat this process n times. To achieve this, I have started with the following code: $(document).ready(function() { for (var i = 0; i < 5; i++) { createEle(i); } }); f ...

Enhancing web pages with a touch of JSP glow

Is there a tool out there that can beautify HTML code and handle JSP elements without any issues? I've been on the hunt for one but haven't had much luck. I'm not interested in an HTML validator, just something that can properly format the c ...

The current issue with this javascript function is that it is failing to produce any output

function calculateOverallCGPA() { let cumulativeGPA = 0.00; for (let i = 1; i <= semNum; i++) { const GPAforOneSubject = parseFloat(getElementById(`subs${i}`).value); cumulativeGPA += GPAforOneSubject; } const finalCGPA = ...

Is it true that AngularJS is unable to update data attributes?

I'm working with an input element that utilizes the type-ahead plugin from bootstrap. My goal is to dynamically set the data-source attribute as the user types, creating a real-time search effect. <input id="test" data-provide="typeahead" data-sou ...

Having trouble dividing the Ajax response into individual Div elements

I'm looking to split the Ajax response into two separate Divs instead of receiving it in one Div. Below is the code I am currently using: $(document).ready(function() { $.post("check.php", { job: exactdatainner, attrb: getattr }, ...

Using Selenium to scroll down to an element until its 'style' changes

I'm in the process of scraping a review page similar to this one. While this specific page has only a few reviews, there are others with a larger volume that require extensive scrolling. Upon observation, I noticed that when the page is not complete ...

Using JQuery to eliminate the current div in an image slider

I currently have an image slider that allows users to switch between images. There are two buttons - one adds a new item to the slider, while the other is supposed to remove the current image from the slider, but it doesn't seem to be working properly ...

The elements within the side navigation menu are not displaying correctly within the app component's HTML file

In my Models.ts file, I created a code that requires me to call the headers in side-nav.component.ts to app.component.html. However, when I try to call app.index.html by typing <side-nav><side-nav>, the component seems to be not specific. mode ...

Is there a way for me to retrieve the icons through a content query?

Currently, I am working on customizing a background slider. However, in the CSS file: span.cbp-binext:before { content: "\e000";} The issue I am facing is that I am only getting squares as icons. Is there a way for me to replace these squares with a ...

What is the best way to specify the CSS :hover state within a jQuery selector?

I am trying to change the background color of a div element when hovered using jQuery, but for some reason the following code is not working as expected: $(".myclass:hover div").css("background-color","red"); Can someone provide guidance on how to achiev ...

Rendering basic HTML in a React component

As I experiment with the react-draft-wysiwyg editor, my progress has been steady. However, I have hit a roadblock when it comes to displaying the output of the editor. For instance, let's say the body is the output of the wysiwyg editor: function Sh ...

Is it possible to disable a function by clicking on it?

I currently have a website that is utilizing the following plugin: This plugin enables an image to be zoomed in and out through CSS transforms, with default controls for zooming in and out. My goal is to incorporate a reset button that returns the image ...

Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home p ...