Trouble with my dropright Bootstrap menu getting cropped

I am currently working on creating a scrollable sidebar that includes a bootstrap dropright function. The goal is for the menu to appear on the right-hand side when a user clicks on an item in the sidebar. I have successfully implemented the scrollable feature, but I am running into issues with the dropright menu being cut off when the text is too long. I attempted adjusting the z-index to bring the dropright menu to the front, but this did not solve the problem. If I remove overflow:auto from #sidebar, everything works correctly, but I want to maintain the scrollable functionality so overflow is necessary. Codepen Link

Answer №1

Take a look at this code snippet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8> 
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">

    <!-- jQuery library -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

    <!-- Popper JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js "></script>

    <!-- Latest compiled JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
    <style type="text/css">
        html {
            height: 100%;
        }

        body {
            margin: 0;
            height: 100%;
        }

        #sidebar {
            display: inline-block;
            vertical-align: top;
            width: 18%;
            background-color : red;
        }

        #sidebar-content {
            display: inline-block;
            vertical-align: top;
            width: 100%;
            overflow-y: auto;
        }

        #content {
            position : relative;
            z-index : 10;
            display: inline-block;
            vertical-align: top;
            width: 82%;
            overflow: auto;
            background-color : green
        }
    </style>
    
</head>
<body>
    <div id="container" class="h-100">
        <div id="sidebar" class="h-100" >
            <div class="container" id="sidebar-content-header">
                <div class="btn-group dropright">
                    <button  type="button" class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        Dropright
                    </button>
                    <div class="dropdown-menu" x-placement="right-start" style="position: absolute; transform: translate3d(111px, 0px, 0px); top: 0px; left: 0px; will-change: transform;">
                        <a class="dropdown-item" href="#">Action</a>
                        <a class="dropdown-item" href="#">Some long text which should just appear on top of content area</a>
                    </div>
                </div>
            </div>

            <div class="container h-100" id="sidebar-content">
                <ul>
                    <li>Sidebar list 1</li>
                    <li>Sidebar list 1</li>
                    ...
                    <li>Sidebar list 1</li>
                    <li>Sidebar list 1</li>
                </ul>
            </div>
    </div><!--
--><div id="content" class="h-100">
    <div id="main-content">
        Lorem ipsum dolor sit amet... [truncated for brevity]
    </div>
</div>
</body>
</html>

Here are the steps I have implemented:

  1. Moved drop-down button into its own section within sidebar for better control.
  2. Added h-100 class to container, sidebar, and content to make them full height of viewport.
  3. Set overflow-y: auto for sidebar to show vertical scroll bar only when necessary, similar to content section.

Extra Tip: Consider adding padding classes like p-1/p-2/p-3/p-4 to sidebar and content for better appearance.

Answer №2

If you're struggling with a similar problem, I came across an insightful article that provided me with some guidance: Check it out here!

The article explained the concept of not being able to have visible horizontal overflow when vertical overflow is invisible, and vice versa.

$(function() {
  // Function to handle menu item hover
  $('li.parent').on('mouseover', function() {
    var $menuItem = $(this),
        $submenuWrapper = $('> .wrapper', $menuItem);
    
    // Get position of the menu item relative to its parent
    var menuItemPos = $menuItem.position();
    
    // Set submenu position relative to the menu item
    $submenuWrapper.css({
      top: menuItemPos.top,
      left: menuItemPos.left + Math.round($menuItem.outerWidth() * 0.75)
    });
  });
});
.wrapper {
     position: relative;
}
 ul {
     width: 200px;
     max-height: 250px;
     overflow-x: hidden;
     overflow-y: auto;
}
 li {
     position: static;
}
 li .wrapper {
     position: absolute;
     z-index: 10;
     display: none;
}
 li:hover > .wrapper {
     display: block;
}
 ul {
     margin: 1em;
     color: white;
     font-family: sans-serif;
     font-size: 16px;
}
 li {
     padding: 1em;
}
 li ul {
     margin: 0;
}
 li .wrapper {
     cursor: auto;
}
 li .wrapper li {
     padding: 0.5em;
}
 li:nth-child(2n) {
     background: #0e8ce0;
}
 li:nth-child(2n+1) {
     background: #0064b3;
}
 li.parent {
     background: #00b99b;
     cursor: pointer;
}
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrapper">
  <ul>
    // Structure for nested lists...
  </ul>
</div>

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

Divs appearing consistently across both mobile and desktop platforms

I'm having trouble getting two divs to display in the same row on desktop and stacked on top of each other in the mobile view. However, they are currently appearing in the same row on both desktop and mobile views. Additionally, when I try to use col- ...

My CSS horizontal drop-down menu is causing the sub-navigation items to overlap each other

I'm having an issue where my sub navigation menu items are stacking on top of each other instead of displaying properly. Here is the code snippet causing the problem: /* STYLING FOR NAVIGATION MENU */ .nav-bar { width: 100%; height: 80px; ...

How to make a HTML div background fill the entire page

Is there a way to extend the background in a div so that it adjusts to fit the screen resolution? I also want to include a navigation bar at the top right corner of this div for the intro page. There will be additional content in other divs positioned be ...

What is the process to replace the image in these templates?

I recently downloaded a template from the internet and am in the process of developing a website. Can someone please guide me on how to change the icons highlighted in the black and red boxes as shown in the image? I am using Visual Studio and ASP.NET fo ...

Utilize CSS to selectively target specific sections of a repetitive pattern within a list

I have come across a list fragment that is generated automatically and I only have the ability to adjust the CSS for it. <ul class="menu"> <li class=" menuItem1 first parent"></li> <li class=" menuItem2 parent"></li> ...

Ways to avoid scrolling on a fixed element

Here is the HTML setup I have... body .top .content The issue I am facing is that when scrolling reaches the end of the ul in the .top element, the background starts to scroll. This can be quite disorienting and makes the site slow on tablets. Even ...

What is causing my HTML script tag to not recognize my JS file in Next.js?

Just starting out with Next.js and trying to access the web cam for a project I'm working on, but encountering an issue when writing in the "script" tag. Below is the simple code for page.js: export default function Live(){ return( <html> ...

Manipulating div positions using JQuery and CSS

Whenever I hover over the #HoverMe div, a hidden #hidden div appears, and when I unhover it, the hidden div disappears again. The #hidden div contains a sub-div that functions like a dropdown list. The #hidden div is styled with position: absolute;. How ca ...

WordPress footer widgets appearing misaligned

I have made a child theme for Twenty Thirteen and am in the process of making some updates to it. My development site is essentially a replica of the live site. The only modification I made in the footer section was to change the widget title styles, but I ...

How to place an image in the bottom right corner using CSS

Does anyone know how to position an image in the bottom right corner of a div? I have tried using margin-right and padding-right with 0px on the img tag, but it does not seem to work. The black lines indicate that I do not need that space Here is my Cod ...

Add a filter and set a background color on the rows in jqGrid

After analyzing the code and output in firebug, I can filter the td and assign a different background color based on certain conditions. Here is the code snippet: loadComplete: function() { var i, names=this.p.groupingView.sortnames[0], l = n ...

What is the contrast between specifying a MIME type and omitting it?

If I were to write a stylesheet in an HTML document, it's worth noting that since the introduction of HTML5, the type attribute is no longer necessary as text/css has become the default and only possible value: <style type='text/css'> ...

Is there a notable distinction when utilizing http for referencing assets?

As I work on creating a new website, the question of displaying images has come up. I plan on using an img tag, but I'm unsure if it matters how I structure the source link: <img src="img/mypic.png"> or like this: <img src="http://www.mysi ...

I am experiencing an issue with Bootstrap's Jumbotron where the bottom border is not visible even after setting it. I am quite curious as to why

I'm new to using bootstrap and I've encountered an issue with the Jumbotron class. Specifically, I have set a border for the Jumbotron and it appears on the top, left, and right sides, but for some reason, the bottom border is missing. After goi ...

What makes text-decoration a crucial element of the foreground design?

<span>Educating children on unlocking their creative and intellectual potential through the world of Computer Science.</span> Here is the HTML code provided above along with the corresponding CSS: span { text-decoration: underline; color: red ...

Implementing a modal component into a React JS page upon loading

I've been working on a webpage using React JS. The site's structure follows MainContent.js --> Main.js --> ReactDOM render. I recently tried to implement a modal popup that worked perfectly in its own project, but ran into issues when impor ...

Unnecessary additional spacing caused by inline blocks in Firefox

On my webpage, I have organized my div elements using inline-block properties. Strangely, Firefox is adding extra spacing between two specific divs, while Safari and Chrome display the design consistently. Check out this example here. #main { display ...

Having difficulties adjusting the margin for a JSX element directly within the code

I'm struggling with giving a margin style inline to a JSX element. Here's the code snippet that I'm having trouble with: <Nunito20 style={{color: frenchGray, margin: 20 0 3 0;}}>Full Name</Nunito20> Unfortunately, this is throw ...

Bootstrap 5.5.2 facing transition issues on bundle.js

Recently, I attempted to incorporate zoom.js into my project to enable image zoom functionality similar to that of the medium website. After linking both the CSS and JS files, it seemed to be working properly. However, I encountered an issue where the tra ...

What could be the reason for the event not being triggered multiple times?

Currently, I am in the process of designing follow/unfollow buttons for various members within a system. However, I am encountering an issue where the jQuery event does not trigger after the HTML is re-rendered within the same div containing the buttons. T ...