How to maintain HTML list items in a single line while overlaying text

I'm seeking assistance with understanding how to solve a particular issue. I have multiple list elements with varying lengths of text content, each flanked by small images/icons on either side. Below is an example of the HTML structure:

.truncate>img {
  width: 25px;
  height: 25px;
}
.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
<div class="container-fluid">
  <div class="row">
    <div class="col-sm-4">
      Content
    </div>

    <div class="col-sm-4">
      <ul id="myList" class="list-group">
        <li class="list-group-item" value="">
          <div class="truncate">
            <img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
            <img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
          </div>
        </li>
        <li class="list-group-item" value="">
          <div class="truncate">
            <img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
            <img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
          </div>
        </li>
        <li class="list-group-item" value="">
          <div class="truncate">
            <img src="https://cdn3.iconfinder.com/data/icons/softwaredemo/PNG/128x128/Close_Box_Red.png">A long text string that is about this length
            <img src="http://www.clker.com/cliparts/1/5/4/b/11949892282132520602led_circle_green.svg.med.png">
          </div>
        </li>
      </ul>
    </div>

    <div class="col-sm-4">
      Content
    </div>
  </div>
</div>

View Example on JS Fiddle.

My goal is to have the text truncated with an ellipsis at the end if it exceeds the available width. Despite using text-overflow: ellipsis, the text gets cut off before the right image. Here are some methods I've attempted:

  • Enclosing the entire li element in a div and utilizing display: inline-block

  • Placing the image tags and text within a single div with inline-block which yielded closer results.

  • Putting the text in a span element and applying different CSS styles.

However, the right image continues to drop below the text each time.

Desired Outcome:

Actual Outcome:

Feel free to let me know if additional information is required. Thank you in advance for any suggestions or advice.

Answer №1

One technique you can use is to manually adjust the placement of the images on the left and right side, then enclose the text within an absolutely-positioned div:

.left { position: absolute;
        left: 0px;
}

.right { position: absolute;
         right: 0px;
}

.text { position: absolute;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        left: 24px;
        right: 24px;
}

.container { display: inline-block;
             width: 100%;
             position: relative;
             height: 24px;
}
    <ul>
      <li>
        <div class="container">
          <img class="left" src="http://raksy.dyndns.org/cross.png">
          <div class="text">
            This is a text
          </div>
          <img class="right" src="http://raksy.dyndns.org/disc.png">
        </div>
      </li>
      <li>
        <div class="container">
          <img class="left" src="http://raksy.dyndns.org/cross.png">
          <div class="text">
            This is a longer text that may need to be truncated
          </div>
          <img class="right" src="http://raksy.dyndns.org/disc.png">
        </div>
      </li>
      <li>
        <div class="container">
          <img class="left" src="http://raksy.dyndns.org/cross.png">
          <div class="text">
            This is a somewhat lengthy text
          </div>
          <img class="right" src="http://raksy.dyndns.org/disc.png">
        </div>
      </li>
    </ul>

Answer №2

It might not be exactly what you're looking for, but this solution could help you reach your desired outcome: Check out the code snippet here

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;    
  display: inline-block;
  width: 50%;
}

.list-group-item{
    background-color:transparent;
    border:none;
    padding:0px 15px;
    cursor: pointer;
  white-space: nowrap;
}

Answer №3

After experimenting, I found success with a div container that has the style display: inline-block:

http://jsfiddle.net/yourusername/example123/

To make it work perfectly:

  • Start by creating a div container.
    • Set the style to display: inline-block;
    • Specify a width for the container.

Answer №4

Using floats and absolute positioning can produce the desired effect:

.truncate>img {
  width: 30px;
  height: 30px;
  position: absolute;
  left: 10px;
}

.truncate img:first-child {
  right: 10px;
  left: auto;
}

.truncate {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 40px;
}

<li class="list-item-item" value="">
  <div class="truncate">
    <img src="...">
    <img src="..."> This is a longer text string that demonstrates how this works
  </div>
</li>

See it in action here

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

Removing the 'div' tag using jQuery in CodeIgniter

I need assistance in removing the added item. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form ...

Guide to integrating various HTML files into a single HTML file with the help of Vue.js

Although I am familiar with using require_once() in PHP, unfortunately, I am unable to use PHP in my current project. I attempted to use w3-include from W3Schools as an alternative, but encountered issues with loading my scripts. Even utilizing JavaScript ...

How to emphasize a clicked hyperlink in AngularJS

Here's the HTML code I've been working on: <div id="Navigation" class="md-dialog-full"> <div class="products-options"> <a ng-click='addType("Physical")' href="javascript:void(0);"> < ...

Place CSS elements in relation to the underlying element

This is an example of my HTML structure: <nav id="menu"> <ul> <li><a href="">Products</a></li> <li><a href="">Offers</a></li> <li><a href="">References</a>& ...

How to set the element in the render method in Backbone?

Currently, I am in the process of developing a web page utilizing BackboneJS. The structure of the HTML page consists of two divs acting as columns where each item is supposed to be displayed in either the first or second column. However, I am facing an is ...

Update the displayed number on the input field as a German-formatted value whenever there is a change in the input, all while maintaining

In German decimal numbers, a decimal comma is used. When formatting, periods are also used for grouping digits. For example, the number 10000.45 would be formatted as 10.000,45. I am looking to create an input field (numeric or text) where users can input ...

javascript highchart image carousel

I am currently working on a visual chart project using the JavaScript library highchart. After setting up my chart with some dummy data, I am looking to incorporate functionality that allows for triggering an image slide based on the chart data. Specific ...

Having trouble getting the on:dblclick event to trigger on a row within a list using S

I am currently using Sveltestrap and I am in need of a double click handler for a row: <ListGroupItem on:dblclick={handler(params)}> <Row> <Col><Icon name=........</Col> ... </Row> </ListGroupItem> Int ...

Stop Scrolling on Web Pages with HTML5

I need assistance in preventing all forms of page scrolling within my HTML5 application. Despite searching on popular platforms like SO and Google, I have been unable to find a comprehensive solution for disabling all scrolling mechanisms entirely. Existin ...

CSS animation: Final position once the vertical text has finished scrolling

I have designed a CSS-animated headline with a leading word and three vertically-scrolling/fading statements to complete the sentence, inspired by the style used on careers.google.com. The animation runs through three cycles and stops, leaving the third st ...

Having trouble accessing the information stored in the Firebase Database?

As a newcomer to Firebase and JS, I am attempting to showcase user information on a webpage that is stored within the Firebase database. The data format resembles the image linked here I have written this Javascript code based on various tutorials. Howev ...

When I include scroll-snap-type: y; in the body tag, the scroll-snapping feature does not function properly

Hey there! I've been trying to implement scroll-snapping on my project but unfortunately, I couldn't get it to work. I tested it out on both Chrome and Firefox, but no luck so far. Here's the code snippet I've been working with, would a ...

jQuery - easily adjust wrapping and unwrapping elements for responsive design. Perfect for quickly undo

Within the WordPress PHP permalinks and Fancybox plugin, there are elements enclosed in an "a" tag like so: <a href="<?php the_permalink(); ?>" class="example" id="exampleid" data-fancybox-type="iframe"> <div class="exampledivclass"> ...

Streamlining programming by utilizing localStorage

Is there a more efficient way to streamline this process without hard-coding the entire structure? While attempting to store user inputs into localStorage with a for loop in my JavaScript, I encountered an error message: CreateEvent.js:72 Uncaught TypeErr ...

What is the method to trigger a function upon opening an anchor tag?

When a user opens a link in my React app, I need to send a post request with a payload to my server. My current strategy involves using the onClick and onAuxClick callbacks to handle the link click event. However, I have to filter out right-clicks because ...

populating a HTML image tag 'src' attribute with data extracted from API javascript code

Hey there! I'm currently working on integrating an image element from the openweatherAPI that corresponds to the weather icon retrieved from the JSON data when a user inputs a city. This means displaying images like scattered clouds or clear skies bas ...

Verify whether the value is in the negative range and implement CSS styling in React JS

I have been developing a ReactJS website where I utilize Axios to fetch prices from an API. After successfully implementing this feature, I am now looking for a way to visually indicate if the 24-hour change percentage is positive by showing it in green, a ...

How can I use HTML and jQuery to send a button click event to a .py file using AJAX and cgi in web development?

I have encountered a challenge with posting data from button clicks on an HTML page to Python CGI for insertion into a PostgreSQL database. My script seems to be struggling with this task. Here is the structure of my HTML, ajax, and javascript: '&ap ...

What steps can be taken to override the property overflow:hidden specifically for a custom tooltip design

I am facing a challenge with overriding the property overflow:hidden in the parent td element. https://i.stack.imgur.com/HKcvn.png This issue is related to my tooltip, which ideally should be displayed beneath the td element: Code snippet for the toolti ...

Unlocking fashion with $refs: A step-by-step guide

After using console.log(this.$refs['block' + row + column]);, I confirmed that I can successfully access the HTML element it refers to. https://i.stack.imgur.com/7KYqB.png However, when attempting to access the element's style using variou ...