Text width that adjusts dynamically

Consider this HTML structure;

<div>
  <span>Text one</span>
  <span class="sticky">ABC</span>
</div>
<div>
  <span>Some other text</span>
  <span class="sticky">DEF</span>
</div>
<div>
  <span>Lorem dolor sit amet lorem ipsum dolor</span>
  <span class="sticky">GHI</span>
</div>

When styled with CSS, it produces a layout as shown below.

An issue arises when the content in the first span exceeds the width of the div. This usually results in the second span wrapping to a new line.

The desired outcome is to have both spans remain on a single line. One way to achieve this is by using;

white-space: nowrap;
overflow: hidden;

However, applying these properties causes the overflow text to be hidden, including the second span.

To address this, the goal is to implement an ellipsis (...) for the overflowing text and push the second span all the way to the right edge of the div (considering padding).

This essentially requires setting the maximum width for the first span equal to the total div width minus padding and the variable width of span.sticky.

While unsure if max-width is the ideal solution, it serves as a reference point for the intended behavior.

Is there a CSS-only approach to achieve this without JavaScript?

Answer №1

It is highly recommended to implement this in CSS. Simply apply a fixed width and height on the initial span to prevent it from expanding to multiple lines. Subsequently, position the .sticky span accordingly. To add flexibility, you can utilize percentages or ems for a fluid design. Take into account that if 'ABC' and 'DEF' are purely visual and not structural components, they may be better applied through a :before or :after pseudo class.

Below is a brief example:

div { 
    width: 200px; /* adjust as needed */
}
span:not(.sticky) {
    display: inline-block;
    max-width: 80%;
    height: 1em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
span.sticky {
    display: inline-block;
    width: 20%;
}

If you wish to observe this implementation in action, visit moolta.com where I incorporated this design. Navigate to 'make a challenge' and select a friend from the modal. The list items inside the dropdown exemplify the concept of truncating text with an ellipsis while maintaining a button alongside them.

Answer №2

<html>
  <head>
    <style>
      .left {
        max-width: 200px;
        display: inline-block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }

      .right {
        float: right;
      }
    </style>
  </head>
  <body>
    <div class="left">
      <div class="right">BAR</div>
      Ipsum
    </div>
    <br />
    <div class="left">
      <div class="right">BAR</div>
      Ipsum lorem
    </div>
    <br />
    <div class="left">
      <div class="right">BAR</div>
      Ipsum lorem dolor sit amet, consectetur adipisicing elit
    </div>
  </body>
</html>

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

Problem: Needing an additional click after selecting a menu item in Jquery

After countless days of seeking help on this platform, I am finally making my first post here to express my gratitude for all the support I have received. I am diving into the world of mobile app development with my very first project - a calculator/conve ...

Positioning borders in an Outlook table

While experimenting with Mjml, I encountered an issue where I struggled to place the border exactly where it needed to be in order for my table to function correctly. It was a bit of a do-it-yourself solution, but it was the only way I managed to make it w ...

Tips for emphasizing a row of various list boxes when hovering in asp.net

I am facing an issue with two listboxes on my website. Currently, I can only highlight one item at a time by mousing over the listbox. However, I would like to be able to highlight items in both listboxes simultaneously when I mouseover either listbox1 or ...

Certain rows appear to be missing even though they are present in both the webpage and its corresponding source code

I am facing a CSS issue that I am unable to resolve. I have 20 results that should be displayed on my website, but only 14 of them are appearing. The other ones are visible in the source code when inspected. When I move the position of the menu up or down, ...

What is the best way to ensure my buttons hide or display correctly when clicked?

I have been working on setting up an edit button that toggles the visibility of save and cancel buttons. However, I encountered a problem where all the buttons disappear when I click on the edit button. This issue did not occur with my other buttons, and t ...

Assign a background image to a button using an element that is already present on the page

Is there a way to set the background-image of a button without using an image URL? I am hoping to use an element already in the DOM as the background-image to avoid fetching it again when the button is clicked. For example, caching a loading gif within the ...

Incorporating background icons or images into an HTML table cell

I want to create a golf leaderboard design similar to the one on golfchannel.com, with circles representing birdies and squares for bogeys. https://i.sstatic.net/mDD8o.png This project utilizes Bootstrap for styling and PHP for database management and lo ...

Optimizing window height to 100% on mobile Chrome

My website is designed to adjust based on the size of the browser window, with the first div filling up the entire height using height: 100%. However, I've encountered an issue with certain mobile browsers like Chrome hiding the address bar on devices ...

Building a Dynamic Image Carousel with jQuery

Is there a way to create a jQuery Slider similar to the one showcased in this example? I have searched high and low for tutorials or plugins that can help me achieve the same design without any luck. ...

JavaScript code to always keep the element on top of the page: "Make

I have developed a straightforward chrome extension tool that displays a small text message (a div with z-index 999999) on the webpage that the user is currently viewing. However, I am facing a problem where the div sometimes gets hidden beneath the existi ...

The jQuery fade speed effect has a limitation where it can only be utilized

When a specific link is clicked, I want a div to display with a fadeIn effect and then fadeOut when the link is clicked again. However, after the first click following an HTTP request, the fadeIn works but the fadeOut does not (the div closes though). More ...

Troubleshooting Problem with CSS and Javascript Dropdown Menu

Greetings, fellow web designers! I am relatively new to the world of web design and currently, I am immersed in the process of creating a website. My current project involves implementing a dropdown menu using CSS and Javascript. While I have made signific ...

Confirming the authenticity of a newly added user input with the help of jQuery

Within my current project, I have implemented validation features for text boxes. When a user clicks on a text box, the border is highlighted. If they click elsewhere without entering any value, the border turns red and an alert pop-up appears. In additio ...

Sections stacked with Bootstrap cards are located beneath a different section

Objective The primary aim is to ensure that all sections display correctly on the mobile version of the website. Challenge An obstacle I am currently facing is: Full width (Looking good) https://i.sstatic.net/EDm3N.jpg Mobile version (Issue) There i ...

Is it possible to incorporate a label within a dropdown menu (<select>)?

How can I add a label inside a select box using Bootstrap? Here is an example: Before selecting an option After selecting an option, the label should appear in a small size like this: After selecting an option : <div class="form-group"> & ...

The burger icon in the navigation bar is malfunctioning, showing just a single horizontal line instead

I've been working on creating a responsive layout button that resembles the following design: https://i.sstatic.net/v5vDd.png However, I've encountered an issue where I can't seem to generate all three horizontal lines, only one of them. T ...

Displaying React components using Bootstrap in the navigation bar

I'm feeling a little stuck here. I'm currently working with React Bootstrap and the Navigation component. One of the routes in my application is the dashboard, which is only accessible after logging in. When a user navigates to the dashboard, I ...

Center elements both vertically and horizontally by utilizing Bootstrap flexbox

Having delved into the documentation of Bootstrap 4 Flex, I am facing difficulties in making the "align-item-center" property function as desired. Despite trying various examples found online, I am unable to alter the vertical alignment of elements. This s ...

Material UI - Clear all designs from the slate

Is it possible to completely override all default material-ui styles and implement your own custom style guide for components? This would involve removing all material-ui styles and starting fresh with customized html skeletons. Creating a new theme with m ...

Several Glyphicons are missing from Bootstrap

When attempting to utilize Glyphicons in Bootstrap, I encountered the following error: > http://address/fonts/glyphicons-halflings-regular.woff2 > http://address/fonts/glyphicons-halflings-regular.woff GET > http://address/fonts/glyphicons-halfl ...