Ensure that breadcrumb links remain on a single line by using text truncation in Bootstrap 5

For creating breadcrumbs in Bootstrap 5, the documentation suggests the following code:

<nav aria-label="breadcrumb">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">Home</a></li>
    <li class="breadcrumb-item"><a href="#">Library</a></li>
    <li class="breadcrumb-item active" aria-current="page">Data</li>
  </ol>
</nav>

However, there seems to be an issue when trying to use the .text-truncate class to auto truncate text with ellipsis in breadcrumbs. Is there a workaround to achieve one-line breadcrumbs in BS5?

Answer №1

.text-truncate is designed to work only on elements with display: block or inline-block properties. However, the .breadcrumb class in Bootstrap uses flex. For more information, refer to: Text truncation

One possible solution is to modify your ol.breadcrumb element to have a display property of block by utilizing the Bootstrap class .d-block, then applying the .text-truncate class to it. Additionally, apply a display: inline-block style to all li elements using the .d-inline-block class.

<nav aria-label="breadcrumb">
  <ol class="breadcrumb d-block text-truncate">
    <li class="breadcrumb-item d-inline-block"><a href="#">Home</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 1 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 2 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 3 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block active" aria-current="page">Data of a long named page</li>
  </ol>
</nav>

In addition to the above solution, here's a creative suggestion for you to consider:

/*JUST A SUGGESTION*/
.sugg ol.breadcrumb {
  flex-wrap: unset;
  white-space: nowrap;
}

.sugg ol.breadcrumb > li:not(:first-child):not(:last-child){
  overflow: hidden;
  text-overflow: ellipsis;
}
/* breakdown to hide it on small devices */
@media (max-width: 576px) { 
  .sugg ol.breadcrumb > li:not(:first-child):not(:last-child){
    visibility: hidden;
    width: 0;
    padding: 0;
    
  }
  .sugg ol.breadcrumb > li:last-child:before {
    content: var(--bs-breadcrumb-divider, "/ ... /");
  }
   .sugg ol.breadcrumb > li:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d3f3232292e292f3c2d1d68736d736c">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

Solution
<nav aria-label="breadcrumb">
  <ol class="breadcrumb d-block text-truncate">
    <li class="breadcrumb-item d-inline-block"><a href="#">Home</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 1 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 2 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block"><a href="#">Child 3 Page Name</a></li>
    <li class="breadcrumb-item d-inline-block active" aria-current="page">Data of a long named page</li>
  </ol>
</nav>

Different Solution (suggestion)
<nav aria-label="breadcrumb" class="sugg">
  <ol class="breadcrumb">
    <li class="breadcrumb-item"><a href="#">Home</a></li>
    <li class="breadcrumb-item"><a href="#">Child 1 Page Name</a></li>
    <li class="breadcrumb-item"><a href="#">Child 2 Page Name</a></li>
    <li class="breadcrumb-item"><a href="#">Child 3 Page Name</a></li>
    <li class="breadcrumb-item active" aria-current="page">Data of a long named page</li>
  </ol>
</nav>

Answer №2

Utilizing Bootstrap breadcrumbs with the display:flex property allows for greater control. By incorporating flex-nowrap and text-truncate on the items, you can ensure the desired layout...

    <nav>
        <ol class="breadcrumb flex-nowrap">
            <li class="breadcrumb-item text-truncate"><a href="#">Home</a></li>
            <li class="breadcrumb-item text-truncate"><a href="#">Library</a></li>
            <li class="breadcrumb-item text-truncate"><a href="#">Longer text</a></li>
            <li class="breadcrumb-item text-truncate active" aria-current="page">Data</li>
        </ol>
    </nav>

Check out the demo 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

Issues with modals functionality in Bootstrap on Rails 7

I'm currently implementing the Bootstrap gem within my Rails 7 project. My main focus right now is to successfully set up a basic Bootstrap modal. The dropdowns in my application are functioning properly, leading me to believe that the JavaScript co ...

What should be done if an image is not wide enough to stretch it to match the width of the window?

When the image is not full screen, it looks fine but when it's viewed in full screen, there's a white area on the right side which is likely due to the image not being large enough. Is there a way to automatically stretch the image so that its wi ...

Having trouble with applying a class in Gtk3 css?

MY ATTEMPT AND EXPLANATION: In my application, I have the following layout structure: GtkWindow GtkOverlay GtkBox GtkGrid GtkButton Even after trying to apply this CSS style: GtkButton{ background-color:blue; } T ...

How come my image is not aligned to the right of the header in my HTML code?

My attempt to align an image to the right side of my heading using code was unsuccessful. Here is the HTML snippet I used: /* Aligning with CSS */ #colorlib-logo img { display: flex; align-items: center; width: 30px; height: 30px; } #colorli ...

Why is my bootstrap-enhanced website appearing unattractive on MSIE 8?

Check out my website at My website displays perfectly in browsers like Firefox, Chrome, Opera, and the Android Browser. However, when I viewed it on Internet Explorer 8 at my parent's house, it looked all messed up. How can I improve the compatibilit ...

Show Struts2 error message on an HTML webpage

I have included error and message handling in my Struts2 action class using the addActionMessage and addActionError methods. I am now looking for code that will allow me to display these messages and errors on the HTML page. Is it feasible to do so? ...

The issue of background-attachment: fixed not functioning properly on Safari

Currently, I have the following code implemented on an element that extends 100% of the browser: #section_white { background-attachment:fixed; background-image:url(image_url_here.jpg); background-position:100% 100%; background-repeat:no-repeat no- ...

The input field will be in a read-only state if it contains a value from the database. However, it will be editable if the value

Hello everyone, I am a newcomer to this community and would greatly appreciate your help. I am encountering an issue with the following lines of code: <input type="text" class="form-control" id="fines" name="fines&quo ...

Prevent a click event from triggering on one div when another div is clicked using jQuery

I am in the process of designing a website that includes two clickable divs, "#coloursettings" and "#powersettings". Both of these divs successfully unbind the menu ("#tab a"), but I am struggling to make them deactivate each other upon clicking one, then ...

What are the steps to insert a plotly graph into a webpage so that it responds to different

I have been working on integrating a plotly pie chart into my website. In order to make the iframe responsive, I decided to utilize this specific tool to generate the necessary html/css. While the chart appears satisfactory on a laptop screen, it is barely ...

What is the best way to ensure that my text always aligns perfectly to the end of the line?

I've been struggling to create a column layout where the text perfectly aligns with the end of the line, but so far, I haven't had any success. Despite my efforts to find a solution online, I have come up empty-handed. What I'm aiming for i ...

Steps for dynamically loading the correct pane without having to refresh the header, footer, or left navigation

Looking to create a web application using HTML, Bootstrap, and jQuery that includes a header, footer, left navigation, and right pane. The content of the right pane will be loaded from a separate HTML page based on what is clicked in the left navigation. ...

Can someone share tips on creating a stylish vertical-loading progress bar with a circular design?

Currently, I am working on developing a unique progress bar that resembles a glass orb filling up with liquid. However, due to its rounded shape, the traditional approach of adjusting the height does not produce the desired result (as illustrated in this f ...

Step-by-step guide on downloading an image in HTML with the click of a button

I have a photo gallery on my website created using PHP and HTML. I am looking to add a functionality where users can click on a button to download the image that is currently set as a background of a specific div element in the gallery. The download button ...

Error in Angular 2 component when loading background images using relative URLs from an external CSS skin

In my angular2 component, I am utilizing a third-party JavaScript library. The skin CSS of the component attempts to load images using relative URL paths. Since I am following a component-based architecture, I prefer to have all component dependencies enca ...

Troubleshooting Angular 2 ng-if issues

Is there a way to dynamically apply CSS styles to an element that has an ng-if condition, even if the condition fails? It currently works fine when the condition is true, but I'm looking for a solution that allows me to modify the element regardless o ...

When a div element overlaps with other elements, everything below it continues to be displayed on top of

I have a DIV that is displaying correctly. However, content that is supposed to be below the DIV is showing on top of it. I need help fixing this issue. I have tried the solutions provided in other similar questions but none of them have resolved my proble ...

How can I ensure that an absolutely positioned element [created with React] snaps to the edge of its parent div when the page loads

I am currently developing a unique two-thumb slider bar component using React. Each thumb in the slider snaps to the closest discrete tick so that users can easily select values along a number line visually. One challenge I'm facing is that the thumbs ...

Is there a way to potentially utilize window.open() to open a specific section of a webpage?

My HTML page consists of 2 div blocks and 2 links. I want to open the content of the first div in a new window when the first link is clicked, and the content of the second div in another new window when the second link is clicked. You can find the code h ...