Shorten the text as necessary for the final elements

I am working with Bootstrap 4 and have the following HTML code:

<div class="badge-container">
  <span class="badge badge-pill badge-primary">One</span>
  <span class="badge badge-pill badge-primary">Two</span>
  <span class="badge badge-pill badge-primary">Three</span>
  <span class="badge badge-pill badge-primary">Four</span>
  <span class="badge badge-pill badge-primary">Five</span>
  <span class="badge badge-pill badge-primary">Six</span>
  <span class="badge badge-pill badge-primary">Seven</span>
  <span class="badge badge-pill badge-primary">Eight</span>
  <span class="badge badge-pill badge-primary">Nine</span>
</div>

The badge-container has a fixed width, for example, 150px, and should be displayed on one line. However, all the badge elements are not fitting in within this space (and I don't know in advance how many badges there will be).

I want the first elements to overflow my div and end with an ellipsis (...) while hiding the remaining elements. This is what I'm aiming for:

One Thow Three Fo...

I attempted to use the following CSS:

.badge-container {
  background: red;
  width: 150px;
  display: flex;
}

// Instead of creating a new class, consider using the text-truncate class that Bootstrap provides.
.badge {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

However, this solution is truncating all elements which is not the desired outcome.

How can I achieve the desired result? Is it possible to do this using only CSS?

Answer №1

Let's simplify by removing flexbox:

.badge-container {
  background: red;
  width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space:nowrap;
}
.badge-container .badge {
  display:inline; /*Remove this to have a different rendring*/
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" >
<div class="badge-container">
  <span class="badge badge-pill badge-primary">One</span>
  <span class="badge badge-pill badge-primary">Two</span>
  <span class="badge badge-pill badge-primary">Three</span>
  <span class="badge badge-pill badge-primary">Four</span>
  <span class="badge badge-pill badge-primary">Five</span>
  <span class="badge badge-pill badge-primary">Six</span>
  <span class="badge badge-pill badge-primary">Seven</span>
  <span class="badge badge-pill badge-primary">Eight</span>
  <span class="badge badge-pill badge-primary">Nine</span>
</div>

Answer №2

Everything should function properly:

.container-badge {
  background-color: blue;
  width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap; 
}

// An alternative is to use the .text-truncate class
.my-badge {
  white-space: nowrap;
}

Answer №3

It seems you were on the right track with your solution, but there was a slight error in applying the truncate code. The code should be applied to the container for the desired effect. Here's an example:

.badge-container {
    width: 200px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    background: red;
}
  <div class="badge-container">
        <span class="badge badge-pill badge-primary">One</span>
        <span class="badge badge-pill badge-primary">Two</span>
        <span class="badge badge-pill badge-primary">Three</span>
        <span class="badge badge-pill badge-primary">Four </span>
        <span class="badge badge-pill badge-primary">Five</span>
        <span class="badge badge-pill badge-primary">Six</span>
        <span class="badge badge-pill badge-primary">Seven</span>
        <span class="badge badge-pill badge-primary">Eight</span>
        <span class="badge badge-pill badge-primary">Nine</span>
      </div>

Answer №4

    HTML
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet" >
    <div class="badge-container">
      <span class="badge badge-pill badge-primary">One</span>
      <span class="badge badge-pill badge-primary">Two</span>
      <span class="badge badge-pill badge-primary">Three</span>
      <span class="badge badge-pill badge-primary">Four</span>
      <span class="badge badge-pill badge-primary">Five</span>
      <span class="badge badge-pill badge-primary">Six</span>
      <span class="badge badge-pill badge-primary">Seven</span>
      <span class="badge badge-pill badge-primary">Eight</span>
      <span class="badge badge-pill badge-primary">Nine</span>
    </div>

    CSS
   .badge-container {
  background: red;
  width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space:nowrap;
}
.badge-container .badge {
  display:inline; /*Remove this to have a different rendring*/
}

.badge-container:hover {
   overflow:visible;
    width: 400px;
   }

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 arises with background image scaling and adaptability

Utilizing Magnific Popup for presenting two sections of content side by side has been a bit challenging. The left section features a div with a background image that varies in size, causing issues with responsive styling to fit the containers properly. Cu ...

The flex container cannot contain all of the content due to an

I am looking to implement a flexbox dropdown menu similar to this: https://i.sstatic.net/Cer9D.png After researching online, I found a simple dropdown menu example. However, I want to customize it using flexbox and its properties. The issue I encountered ...

Replacing the left styling using CSS

Struggling to adjust the position of a side panel that pops up when hovering over an option. This is the code I found in Chrome Developer Tools: <nav id= "side_nav" class= "sidebar-bg-color header-side-nav-scroll-show"> <ul ...

Retrieve the outcome of sendkeys operation saved within an element located in href using Selenium with Python

<td><a href="mcc.php" target="blank">Mobile Country Code</a></td> <td> <input type="text" class="other-text" size="15" name="mcc" placeholder="201 ...

What is the most effective way to showcase the value of a field nested within an object?

I am struggling to frame my question correctly and finding the right information on Google. Therefore, I will provide a simple example instead. If you can assist me by answering my question or guiding me in the right direction, I would greatly appreciate i ...

Navigating Between Web Pages

Hello there! I'm curious to learn how I can establish routing between multiple pages. Specifically, I have an index page and I'd like to navigate to other HTML pages within my views folder. Is it possible to achieve this without relying on the An ...

Start the embedded YouTube live stream on autoplay

When it comes to autoplaying a regular YouTube video, there are various solutions available. However, these methods do not always work for livestreams. I attempted to automatically click the embedded link using the following code: <script> var els = ...

Create a border surrounding the matColumn within a sticky matTable

I am currently working with a matTable that has the first two or three columns set as sticky. However, I am facing an issue where the scrolling behavior of the non-sticky columns under the sticky ones looks like a glitch due to existing border styling on t ...

jQuery for Dynamic CSS Class

Is there a way to apply a different style to a link upon page load with jQuery? I would like the link to appear highlighted when the user navigates to a specific page, such as clicking on the About Us menu item. I want to use jQuery to achieve this effect. ...

The align-content property in CSS Flex does not function properly when used alongside an inline height declaration

I encountered an issue with the CSS Flex property align-content. It seems to not work properly in a container where the height is declared inline. However, once I move the inline height declaration to a separate CSS class, everything works fine. Both Chrom ...

The CSS within the WebComponent nesting is not valid

Upon testing, I have found that writing CSS directly using nesting is effective. However, when used within a WebComponent and nested with the :host selector, it becomes invalid. Below is a demo code snippet showcasing this issue. My Chrome version number i ...

Customize the slide-in menu on your WordPress theme by adding jQuery and enhancing it with CSS animations

I'm attempting to create a sliding menu that appears when clicked and disappears when the close button is activated. Unfortunately, I can't seem to get it to slide out properly. jQuery(function($){ $('#menu-open').click(function ...

Tips on customizing the size of tooltips within ui-select-choices

While working on a small website for a homework assignment, I have encountered an issue with styling the size of a tooltip. The tooltip is part of an attribute in a span tag under the ui-select-choices. I only want to customize the style of the tooltip w ...

struggling to align menu items on both sides of a navbar

I am currently facing an issue with positioning elements in my HTML code. I have a File drop-down that I want to display on the left side, while the rest of the drop-downs should appear on the right side. It seems like my CSS files might be causing some in ...

Ways to identify local storage when a user visits the page for the first time using jQuery

As a beginner in the world of local storage in Jquery, I am looking to implement a feature on my Bootstrap 4 accordion. The desired functionality is as follows: If a user is visiting the page for the first time, all accordions should be open by default ...

Exploring Angular Material Design's method for vertically populating a column

Is there a way to fill a column vertically in my current app? https://i.sstatic.net/uu4yO.png I've been struggling for hours trying to make the vertical items span the entire height of the page. I have pored over documentation and other posts, but I ...

horizontal menu consolidation

Web Design <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Online Hangout</title> <meta http-equiv="Content-Type" content="text ...

Directly insert an image into your webpage by uploading it from the input field without the need to go through the server

Can an image be uploaded directly from <input type="file" /> into an HTML page, for example through the use of Javascript, without needing to first save the image to the server? I am aware that AJAX can accomplish this, but if there is a way to ...

What are the steps to programmatically click a JavaScript button with Selenium and Python?

Imagine there is a button on a webpage that looks like this: <input type="submit" name="next_btn" value="Next" onclick="gonext();" id="btnNext"> When manually clicked, it takes approximately 3-6 seconds for the page to reload and show new content. ...

Unable to fetch local file using ajax from a local HTML page

According to Same Origin Policy, the SOP should not be applied to the file:// protocol. However, I'm having trouble with my code. I am running a testing page from my local system, and both when I try to access abc.txt in the same directory as the HTML ...