What is the best way to implement CSS text-overflow after a specific amount of text, such as two lines?

Although this question is similar to another one (Wrap a text within only two lines inside div), I couldn't find a satisfactory answer in that thread.

Here's my dilemma: I have a DIV with fixed width and height, but it needs to accommodate text of varying lengths. If the text extends beyond the first line, it should wrap to the second line; if it goes past the second line, it should be truncated with ellipses.

This is what I've tried so far:

.name {
  white-space: nowrap;
  text-overflow: ellipsis;
  overflow: hidden;
  height: 20px;
  line-height: 10px;
  width: 64px;
}

However, this solution doesn't handle wrapping properly; making some adjustments allows me to wrap to the second line, but then the ellipses aren't applied when it wraps further.

Among the responses in the other thread, this one was the most promising, but it added ellipses to both lines of text instead of just the second.

Is there a way to achieve this using only CSS, or will I need to use JavaScript for this task?

Answer №1

This question is quite intriguing, however, finding a satisfactory answer seems challenging. I personally lean towards the suggestion by sparklemuffin and the response from dazzlequeen, as alternative methods like utilizing pseudo elements or vendor prefixes could be clunky. While using jQuery may not be perfect, it offers a cross-platform solution that avoids compatibility issues with older browsers.

Answer №2

To give the appearance of a longer text, use a pseudo-element positioned at the bottom corner over the existing text. When applied to a single line, text-overflow:ellipsis can help achieve this effect.

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

Unable to resize icons within bar-footer

My footer has a bar: <div class="crop modal"> <div class="crop-center-container"> <div class="crop-img" ng-style="{width: width + 'px', height: height + 'px'}"></div> </div> <div cla ...

``I am having difficulty with Bootstrap as it seems to be hiding my field or

I'm currently working on a webpage that uses Bootstrap. It has a form with a label and text input field. When I add the class="custom-control-input" to the input field, Bootstrap hides it. Can someone explain why? If I don't include the class, ...

What is the best way to evenly divide divs vertically on a Bootstrap website?

I am currently working on a responsive page design and have come across this useful resource: http://www.bootply.com/2sfiCehqac My main objective is to ensure that when the screen size is medium or large: 1) div_left and div_right (orange and blue) adjus ...

Changing the color of the Bootstrap-Vue b-dropdown button when it is open

When it comes to customizing the button color and hover effect, the code below does the job perfectly. However, there seems to be an issue where the button color reverts back to gray when the menu is open and the cursor moves away from the button. How can ...

Linking a button to a (click) event within HTML content fetched from the backend

Hey there, I'm facing a scenario where I have an angular service that sends a HTTP request to the backend for some HTML code. Once the HTML is received, I'm inserting it into the component's HTML using <div [innerHTML]="..."/>. The iss ...

Ways to incorporate a background image using CSS

I tried to add a background image using CSS, but unfortunately, I was unsuccessful in my attempts. body { font-family: Arial, Helvetica, sans-serif; } /* .login-form{ width: 400px; } */ /* Full-width input fields */ input[type=text], input[type ...

Alter the button's color seamlessly while staying on the same page

I have implemented a feature that allows me to change the category of photos without having to leave the page and it works perfectly. My next goal is to create a button system where the pre-defined category of a photo is indicated by a button with a green ...

Generate an image on Canvas using a URL link

I have a unique URL that generates canvas images with specific parameters. It functions properly when loaded in a browser. The final line of code is: window.location = canvas.toDataURL("image/png"); However, when attempting to use this URL from another ...

Tips for saving true or false values in a dynamic form?

Is there a way to store boolean values in a reactive form where a button can have the value of true or false? The goal is to be able to access the inputs of these buttons. However, I am facing an issue because there is another form on this page for text in ...

The validation tool from Google Rich Results indicates that no enriched content was found on this particular website link

My website () is designed to showcase rich results by implementing JSON-LD schemas on every page, which are validated correctly by the Schema.org validator. However, Google's Rich Results Test seems to be failing to recognize them for some reason. Cu ...

Table featuring identical submit buttons in each row: initial submit button is nonfunctional (potential issue with multiple identical IDs)

My table displays product files, with the option to add notes. If a note is added, it shows in a row. If not, a text area field with a submit button appears instead. Everything seems to be working well, except for the first row without a note. After addin ...

Two hyperlinks are not visible

I've added 2 links in my header component, but for some reason, they are not displaying. It's strange because everything appears to be correct. These 2 links are part of a list within a ul element. <nav class="navbar navbar-expand-lg navbar-d ...

Add a plethora of images to the canvas

Just starting out with Fabric.js and trying to figure out how to draw a picture on the canvas after a drop event. I managed to do it once, but am struggling with inserting more pictures onto the canvas (each new drop event replaces the previous picture). ...

Divergent Bootstrap layouts for different devices

https://i.sstatic.net/g1YeJ.png I am working on creating a layout where I want to display certain content differently based on whether the user is viewing it on a mobile or desktop device. The current approach I have taken involves duplicating some divs ...

Enhance Prime-ng dropdown child elements with customized styling

Currently, I am attempting to apply styles to elements within a PrimeNG dropdown. These particular styles are associated with child elements nested within the p-dropdown tag, and I am unsure of the proper method to target them. Below is a snippet of the co ...

Styles for Tab Alignment

.tabs { position: relative; margin: 20px auto; width: 1500px; } .tabs input { position: absolute; z-index: 1000; width: 120px; height: 40px; left: 0px; top: 0px; opacity: 0; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; filt ...

Ways to interact with popup windows using Selenium

I'm currently using Selenium to automate a web task, and I'm facing an issue with clicking on a popup that appears after searching for an address. The popup displays a message asking, "Did you mean _____ address?" and I want to be able to click o ...

I am seeking guidance on extracting specific data from a JSON file and presenting it in an HTML table using jQuery

I am currently working on extracting specific elements from a JSON file and displaying them in an HTML table. I have successfully retrieved the data from the JSON file, stored it in an array, and then dynamically created an HTML table to display the conten ...

What does the term "the selected font size" mean when defined in em units?

Exploring the query raised in this post: Why em instead of px?, the top voted response sheds light on the nature of em: The unit 'em' is not fixed but rather relative to the current font size. It varies according to the user's chosen font p ...

Mastering the art of manipulating Div elements using CSS for the optimal Print View experience

I have a table with several columns and 1000 rows. When I print the page, it breaks the rows onto different pages, showing the remaining rows on subsequent pages. I want to display the complete record in one go. Below is a sample code with only one row in ...