Implement the text-overflow: ellipsis style in a <li> element to truncate text after two lines

I need help with creating a list where each item has a dynamic length. The maximum amount of lines for each item should be 2, and if the text overflows those 2 lines, it should end with dots. I know that using "white-space: nowrap;" can prevent wrapping, but how do I specify the 2 line limit? Any suggestions would be greatly appreciated.example

  li {
    line-height: 25px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    width: 100px;
    border: 2px solid #ff0000;
  }
<ul>
  <li>long long name</li>
  <li>long long name long long name</li>
  <li>long long name long long name long long name</li>
</ul>

Answer №1

Feel free to check out my fiddle example.

In my solution, I utilized pseudo elements to strategically place an ellipsis at the end of every other line. To ensure a smooth visual transition, I incorporated a fading gradient background that prevents cutting off complete words.

CSS Styles

.list-item {
    width:100px;
    max-height: 50px;
    line-height: 25px;
    border:2px solid #ff0000;
    position: relative;
    overflow: hidden;
}

.list-item:after {
    content:"...";
    position: absolute;
    top: 25px;
    right: 0px;
    z-index: 2;
    background: linear-gradient(to right, rgba(255,255,255,0) 0%, #fff 30%, #fff 100%);;
}

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

Utilizing CSS Grid to arrange child elements inside their respective parent containers

I am currently working on a grid container that contains multiple divs. Each div houses different elements such as headings, paragraphs, buttons, and logos. I have utilized Flexbox to evenly distribute the logos across the container's width. However, ...

Recently, there has been an issue with the size of SVG images specifically on Android Chrome

UPDATE: I have narrowed down the issue to two SVG images that can be found here: . Strangely, Chrome on Android is displaying the second SVG image underneath the first. Take a look at this screenshot of the incorrect rendering on the Chrome browser for And ...

Ways to incorporate text using css within a textarea?

I want to include an image on my webpage using the following CSS: { background-image: url(../images/icon_new_reset.png); background-repeat: no-repeat; } Is there a way to provide alternative text or overlay text on this background image? ...

I am having trouble getting my callbacks to properly function with Ajax. It appears that the callback is not being waited for, can someone please advise me on what steps I may be missing?

After reading through several posts on the subject like this one and that one, I am still encountering an issue where 'undefined' is displayed as the output almost instantly, indicating that the callback may not be properly executed. I have been ...

Utilizing jQuery to clear out data sent through the $.post

I am currently working on developing a multi-part form that involves executing different scripts for each part. Each script checks for essential data and if this data is missing, it should return a string "false". While part 1 fails correctly by calling h ...

When I click on a tab section to expand it, the carat arrows all point upwards. Only the arrows corresponding to the selected section should

click here for imageIt appears that there are four tabs, each with a click function on the carat icon. When I expand one tab, all carats point upwards instead of only the selected one appearing. accountSelection(account) { if (!this.selectedAccoun ...

What is the best way to change an HTML string into JSON using PHP?

Is there a method to transform an HTML string into JSON using PHP similar to the functionality of toolslick.com's html2json converter? Here is an illustration of the HTML string: <html> <body> <table style="width: 100%"> ...

Python: Utilizing the requests library to handle webpage redirections

Update: While this may not directly answer the question I had initially, I was able to find the necessary information in the API which provided a solution for my software. My current task involves logging into a webpage, navigating to another page, and ex ...

Guide on aligning a division within another division?

Included below is my HTML code: <div id="background_div"> <img id="background_img" src="images/background.jpg" alt="dont matter"> <i class="material-icons">perm_identity</i> <div id="dropdown"> <ul id=" ...

Illuminated container with shading

I'm struggling to style a box with a unique glow effect and shadow at the bottom. I've hit a roadblock in creating this particular effect. The glow effect I am aiming for is shown here: https://i.stack.imgur.com/i2va8.png My attempts to create ...

Use CSS alignment to combine both the profile picture and title on a webpage

Is there a way to seamlessly integrate a profile picture with text (title) on my tumblr theme? My main challenge lies in getting the alignment right. Additionally, I want it to look good in responsive view. If the title becomes too long, it should wrap un ...

Preventing duplicate Google indexing with .htaccess

I have a website that utilizes a RewriteRule in the root directory to direct queries in this format: http://domain.com/foo/parameter to http://domain.com/index.php?args=parameter Visitors only see the clean URL and everything works smoothly. However, ...

I'm looking for a more efficient and graceful way to implement jquery .slideToggle on numerous hidden divs. Any suggestions?

If you would like to see a demonstration of what I am working on, here is a link to a live example: https://jsfiddle.net/yfe7ky3x/ My goal is to create a basic portfolio website that showcases various projects. I want to implement a feature where clicking ...

HTML automatically removes added elements

I have embarked on the journey of developing a task management application. The main functionality I am working on involves appending a new event card to the events div when the submit button of the formlayer is clicked. function reveal() { document.g ...

Retrieve form data (from a standard form submission) with jQuery

Within page A, I have a form containing 2 fields - one for the user and one for the password. The form's action directs to page B. Is there a way to use JavaScript to retrieve the form values from the request header when page B is loaded? Or is there ...

Trigger an immediate Primefaces update using a JavaScript function

Having an issue where: upon page load, I attach a 'keypress' event listener to every input field on the page. The goal is to check for special characters in the input and remove them. Below is the function that executes on page load: function ...

Conceal the browser scrollbars

When I have two DIVs on my webpage that are larger than a browser window, it results in a horizontal scrollbar being displayed. How can I hide this browser scrollbar for the first DIV while still showing it for the second one? For example, if I have two ...

The navigation bar on the website highlights vibrant green arrows next to the

I'm having trouble implementing a bootstrap menu on my website using the code provided by Bootstrap. The menu is not displaying correctly and instead showing small green UL arrows. I have JavaScript and Bootstrap scripts in my project. Is there a way ...

Switch to using addresses instead of latitude and longitude coordinates when utilizing the Google Maps API

I am looking to utilize Google Map Markers to indicate the locations where our services are offered. The code I have created using latitude and longitude is functioning properly, however, for my project I need to display city names instead of lat/long ...

Styling only for Angular 2 Components

How can component scoped styles be used to style elements differently based on their location within the site while still maintaining style scoping? For example, when using a "heart like" item created in this course, how can padding be added specifically i ...