What could be causing Line Clamp to fail when using text alignment justify?

How can I get proper ellipsis to show with -webkit-line-clamp when using text-align:justify? It's functioning correctly with text-align:left/right.

Does anyone have any suggestions or tricks?

div {
    text-align: justify;
    width:150px;
    padding: 0 5px;
    line-height: 18px;
    display: -webkit-box;
    display: -moz-box;
    display: -ms-box;
    max-width: 100%;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}
<div>Why Line Clamp is not working with text align justify</div>

Answer №1

My solution involved:

/* autoprefixer: off */
-webkit-box-orient: vertical;
/* autoprefixer: on */

Answer №2

WebKit flexbox is not fully developed and lacks standardization! I don't consider this an error at all. Even Opera has its own method for handling clamping, which can be quite frustrating.

.last-line {
  height: 3.6em; /* precisely three lines */
  text-overflow: -o-ellipsis-lastline;
}

If you prefer pure CSS solutions like myself, instead of using clamp.js to manage your ellipsis, give this a try: CSS Ellipsis: How to Manage Multi-Line Ellipsis in Pure CSS

Answer №3

Take a look at this JSFiddle. I made some changes to the CSS and experimented a bit, and now it's working fine for me! :)

CSS:

.text-clamp {
    text-align: center;
    width: 200px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

HTML

<div class="text-clamp">Why isn't Line Clamp working with text aligned in the center?</div>

I hope this solution works for you too or helps you resolve your problem.

Answer №4

The method you are attempting to utilize is an outdated approach that relies on an obsolete version of display: flex; and is best avoided.

Instead of using the JavaScript technique clamp.js, I have developed a pure CSS alternative which can be found here: http://codepen.io/n3ptun3/pen/meYKgZ?editors=110

CSS

p {
  position: relative;
  width: 400px;
  height: 120px;
  line-height: 30px;
  margin: 0px;
  padding: 0px;
  overflow: hidden;
  text-align: justify;
}

p::after {
  content: "...";
  background-color: #fff;
  height: 30px;
  width: 20px;
  position: absolute;
  bottom: 0;
  right: 0;
}

Keep in mind that this solution works best when used with a solid background, which is typically the case for body text.

Key points to remember:

  1. Adjust the line-height as needed for your design.
  2. Set the height to a multiple of the line-height.
  3. Ensure the overflow property is set to hidden.
  4. Add the ellipsis to the after pseudo-element and match its background color to that of the paragraph.

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

Error message: Incomplete JSX element

Can anyone help me figure out why my react JS code is failing to compile? There seems to be a missing brace or parenthesis in there somewhere, but I can't seem to find it. Is there a tool that can pinpoint exactly where in the code I am making a synt ...

What is the best way to include a .AVI file in an HTML document?

I have come across some examples of .AVI in HTML on the internet. However, my page seems to be having issues. It displays fine in Chrome on my computer. But in Internet Explorer, there is a bar at the top and the videos appear blank afterwards. It ...

Issue with Bootstrap 4.x tooltip positioning within an overflowing container

In the world of bootstap 4.x, a tooltip finds itself in a bit of a pickle when nestled within a parent element sporting an overflow property. Whether it's overflow: auto; or overflow: scroll;, the poor tooltip just can't seem to find its place. T ...

Get rid of the blue outline border when embedding Google Maps

Does anyone know how to remove the blue focus border that appears when clicking on an embedded Google Map? I've tried various CSS styles without success: *:focus { outline: none !important; border: none !important; } iframe { outline: no ...

Tips for creating a div element with a width that is close to 100%

I have a snippet of HTML code that looks like this: <div id="outer"> <div id="inner"></div> </div> Is there a way to adjust the #inner div so it takes up 100% width of #outer but with a 10px margin? This is my current CSS sty ...

Utilize function data to assign unique IDs in jQuery for HTML elements

Having trouble using the data function as an id in my code. Here is what I have tried so far but it's not working. Can anyone provide guidance on how to achieve this? Thank you in advance. function HIDE_SLIDE(SLIDEID) { $("#($SLIDEID).val()").pro ...

The system encountered an error stating that the class 'Database' could not be found

Whenever I try to establish a connection to my SQL database using my pdo_object.php file, I encounter the following error in my model.php: Fatal error: Class 'Db' not found in /path/model.php on line 8 I have double-checked that all permissions ...

Indicate the appropriate HTML code for bookmarking a webpage on an iPhone

I am currently working on optimizing a website specifically for iPhones. I've noticed that when users click the '+' button on their iPhone, they have the option to add a bookmark to the website on their home screen. Is there a way for me to ...

Stacking background images in CSS above other contained elements in a div

I've been experimenting with adjusting the z-index of elements in my code but haven't had any luck. Is it even possible to achieve what I want? Within a div element, I have set a background image using CSS. Inside this div, there are other eleme ...

Expand the size of the card box with CSS to create a larger and more prominent display

I recently came across a snippet of Javascript and CSS code. Here is the Javascript: $('.item-card').css("width", $('.item-card').width() + "px"); $('.item-card').css("font-size", $('.item-card').width() * .13 + "p ...

What steps should be taken to ensure that data can be transmitted from the input without allowing the user to edit the ID field?

Here is the sample HTML: <form action="/app02/user_edit-{{obj.id}}/" method="post" > <input type="text" name="id" value="{{obj.id}}" disabled> <input type="text" name="username" value="{{obj.username}}"> <i ...

vee-validate - Standalone form validation with distinct procedures

I currently have a situation where I am dealing with two forms, each in separate steps and having their own submit button. Using $validator.validateAll() validates all the inputs on the page, but I specifically need validation for each form individually. ...

Can you explain the contrast between body.onunload and body.onbeforeunload?

Similar Question: Explaining the distinction between onbeforeunload and onunload Tell me, what sets apart body.onunload from body.onbeforeunload events? From what I've gathered, I should be able to apply onunload and onbeforeunload to any HTML el ...

Using SVG sprites from external sources alongside child nodes

I am working on a website that contains multiple SVG icons and an SVG logo that need to be animated. To improve readability, I have implemented a sprite system using an external file. For loading the sprites, I am utilizing XMLHttpRequests at the start o ...

Some of the items in the dropdown menu are not fully displayed

I'm currently working on a drop-down menu, but I'm facing an issue where adding position: absolute; and display: block; is causing the second ul element to not be fully visible. My goal is to have both ul elements visible so that I can proceed to ...

Avoiding overlap in CSS Grid layout with Internet Explorer 11

Creating a date picker component that utilizes a calendar styled with CSS grid has been successful, except in IE11 where all elements appear in the top left corner. Is there a specific CSS property for IE11 that could fix this, or is it simply not compatib ...

What is the best way to vertically center the `tab-content` without relying on the tablist for

After examining the form below, my goal is to center only the fields while keeping the navigation buttons in their original position. Specifically, I want only the tab-content to be centered, excluding the tablists. <link rel="stylesheet" href="https ...

Transform the check box into a button with a click feature

I've customized a checkbox to resemble a button, but I'm encountering an issue where the checkbox is only clickable when you click on the text. Is there a way to make the entire button clickable to activate the checkbox? .nft-item-category-lis ...

Modifying an image on a website with XML information and jQuery

I'm in the process of revamping my website's HTML structure and switching to using an XML file for content. The jQuery script I have is as follows: <script> function extractSiteName() { var URL = window.location.href; var das ...

The text appears choppy as the javascript is in the process of loading

Upon integrating a basic javascript function into my website to request data from another source, I noticed that the text in the header div suddenly shifts left and then centers itself while the site is loading. This strange movement occurs every time the ...