What is causing the div to expand even though css max-width is applied?

I am currently facing an issue with the navigation on mobile view while using Bootstrap 4. Specifically, the .nav .dropdown-menu and .text-truncate classes are not displaying ellipsis when the content text length is too long.

I have attempted to resolve this by setting max-width: 100% to the .dropdown-menu so that children elements only expand to the maximum width of the parent.

View the screenshot of the actual issue here

Here is the code snippet:

<nav class="navbar navbar-expand-sm navbar-dark bg-dark shadow fixed-top">
    <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#collapse-husr-navbar" aria-expanded="true">
      <span class="navbar-toggler-icon"></span>
    </button>
    <div class="navbar-collapse collapse show" id="collapse-husr-navbar" style="">
      <ul class="navbar-nav" id="navbar-husr-right">
        <div class="d-block d-sm-flex">
          <div class="p-2 text-truncate">
            <span class="d-inline">
              <i class="fas fa-map-marker-alt"></i>
              <small><span id="husr-branch-name">DUMMY BRANCH A</span></small>
            </span>
          </div>
        </div>
        <li class="nav-item dropdown show">
          <a class="nav-link dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="true">
            <span class="d-inline d-sm-none d-md-inline d-lg-inline">Menu</span>
          </a>
<!-- I tried setting max-width: 100% to prevent children elements from expanding the parent, but it did not work. :( -->
          <div class="dropdown-menu dropdown-menu-right show" style="max-width:100%">
            <span class="dropdown-item-text text-nowrap" style="
">
              <i class="fas fa-user"></i>
              <span id="husr-user-name">Staff User</span>
              (<em><span id="husr-user-type">STAFF</span></em>)
            </span>

<!-- I also tried setting max-width: 100% multiple times to no avail. :( -->
            <span class="dropdown-item-text head-email" style="max-width:100%">
              <div class="text-truncate" style="max-width:100%">
                <i class="fas fa-envelope"></i>
                <span id="husr-user-username">i_mean_it_to_be_ellipsis_text_truncated_but_container_div_still_expanded</span>
<!-- The text contains no spaces due to it being an email, which should be non-spaced. -->
              </div>
            </span>
          </div>
        </li>
      </ul>
    </div>
  </nav>

View the expected screenshot here

Answer №1

To enable the text-overflow feature, it is necessary to have a maximum width and also apply overflow: hidden. You can eliminate the text-truncate div and utilize the following CSS:

.head-email {
    max-width: 100%;
    white-space: nowrap;
    overflow: hidden;
}

According to the MDN documentation on text-overflow:

The text-overflow property alone does not trigger an overflow. To make text overflow its container, you need to define other CSS properties: overflow and white-space.

UPDATE:
In this particular scenario, bootstrap does not restrict the size of the navbar itself when under 576px. Adding max-width: 100%; to the navbar-collapse class resolves issues with smaller viewport sizes.

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

Issue with Jquery addClass method in Firefox

I am encountering an issue with the jQuery addClass() function in Firefox where the class is not being added to the current element. Interestingly, this code works perfectly in Chrome and Safari (IE has not been tested). Here is the CSS snippet : $(doc ...

Tips for changing the width of a child element in CSS without impacting the parent's width

Here is the scenario: <div class="parent"> <div class="sizer" /> <div class="child" /> </div> .sizer { width: 200px; } .child { position: relative; width: 400px; } I am facing restrictions - I cannot explicitly set the width ...

The styling of HTML elements is ineffective when using scoped styles

I'm facing an issue where my element styling doesn't seem to work when using the scoped attribute. Upon inspecting the element, it appears that the styling is not being applied when using scoped. The reason I need to use scoped is because I want ...

I am experiencing an issue where my mobile responsive website consistently shows the smallest CSS width view. What could be the root of this

Currently, I am developing a mobile-responsive website specifically catered to iPhone 6 Plus and smaller models (iPhone 6 & 5 included). When I preview the site using "Inspect Element" in the Chrome browser with these device settings, everything appears t ...

Spacing between hidden checkboxes and their labels

I've customized some checkboxes to resemble a select dropdown by hiding the input and using only the label as the visible element. However, I'm struggling to add margin between the elements, which seems to work only when the checkbox is visible. ...

I am encountering issues with the responsiveness of my layout as it is

My goal is to create a responsive layout utilizing code and examples from this website: www.responsivegridsystem.com To achieve this, I have nested a few containers to ensure a content area of 960px width centered within a 1000px container. My plan is th ...

concise stylus CSS selector

Is there a way to condense these stylus selectors for a cleaner look? #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > div:nth-child(1) > button #map > div.mapboxgl-control-container > div.mapboxgl-ctrl-top-left > ...

The issue of React hover style malfunctioning in conjunction with Radium and Material-UI

I am currently utilizing the Radium library for inline styling in my React project. I have encountered an issue where the styling does not apply properly to Material-UI components, specifically when hovering over a Paper element. The color should change ...

What is the best way to modify the color of a button within an AngularJS function by utilizing the same button?

Currently, I have a function assigned to the ng-click event on a button in order to filter a list. My goal is to change the color of the button once it has been clicked and the filtered list is displayed. I am looking for a way to achieve this within the ...

The web element cannot be located, but it becomes visible when manually inspected

https://i.sstatic.net/wQxLk.png I'm facing an issue with accessing this menu in selenium as the web element doesn't show up in the inspector until I do it manually. <a id="cke_3275" class="cke_menubutton cke_menubutton__table c ...

Seamless transition of lightbox fading in and out

Looking to create a smooth fade in and out effect for my lightbox using CSS and a bit of JavaScript. I've managed to achieve the fading in part, but now I'm wondering how to make it fade out as well. Here is the CSS code: @-webkit-keyframes Fad ...

Remove bulleted list from HTML using JavaScript DOM

My task involved deleting the entire "agent" array using the removeChild() function, requiring the id of a <ul> element. However, I faced an issue as I couldn't set the id for the "ul" due to using the createElement() function. //old code < ...

Incorporating an HTML Canvas element within a CSS grid design

I've been attempting to integrate an html canvas that I created into one of the css grid classes specified in my code. The code runs when the "Calculate" button is clicked. Upon clicking the button, the <div> section designated for the canvas r ...

Permitting various valid responses for questions in a multiple-choice test | Utilizing Angular.js and JSON

As a beginner in this realm, I must apologize if my query seems naive. I recently crafted a multiple-choice quiz using HTML, CSS, JavaScript (angular.js), and a JSON data file following a tutorial I stumbled upon. The outcome pleased me, but now I am face ...

What methods can I use to customize the appearance of the acceptance label in a contact form created with

I am currently developing a WordPress website using the Contact Form 7 plugin. However, I am facing an issue with styling the "I accept" button and text when clicking on the "registrace" button. I would like to center them and create some proper margin aro ...

Unable to display Boostrap modal upon clicking href link

Can someone help me troubleshoot why my pop modal is not displaying after a user clicks on a specific link on my webpage? I have attempted the following: <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></s ...

The sequence of HTML5 DragDrop Enter and Leave events occur consecutively

I am encountering a problem with a simple drag & drop effect where the class name is changed when the drag enters or leaves, resulting in continuous entering and leaving. Take a look at this basic HTML code: <div class="box"> <div cla ...

ngAnimate - Animation not activating on recurring custom directive

I am trying to implement ngAnimate on a custom directive that is repeated using ng-repeat in my AngularJS application. The animations function correctly when I use ng-repeat on a simple HTML list-item. However, when I replace the list-item with a custom di ...

Display picture next to content with the help of CSS/Bootstrap and Django's Jinja 2

I'm having trouble displaying an image next to text, it keeps showing up below. Here's the code snippet: <div class="container"> <div class="row"> <div class="col-md-8"> < ...

Wrapping a list vertically with ASP.NET using a repeater for generation

I have a repeater that is creating a list of links in no particular order. I want the list to be displayed as follows: -Item1 -Item4 -Item2 -Item5 -Item3 Most solutions I've found require you to know the items in advance and set specific classes for ...