Scrollable Menu with Bootstrap - Menu expanding its container when it should not

后端 未结 6 1090
野性不改
野性不改 2020-11-29 15:39

I tried this method (their fiddle) to enable scrollable menu with Bootstrap, but with that approach, the scrollable menu expands its container -- fiddle -- the non-scrollabl

相关标签:
6条回答
  • 2020-11-29 16:18

    I just fix this problem in my project-

    CSS code

    .scroll-menu{
       min-width: 220px;
       max-height: 90vh;
       overflow: auto;
     }
    

    HTML code

    <ul class="dropdown-menu scroll-menu" role="menu">
       <li><a href="#">Action</a></li>
       <li><a href="#">Another action</a></li>
       <li><a href="#">Something else here</a></li>
       <li><a href="#">Action</a></li>
       ..
       <li><a href="#">Action</a></li>
       <li><a href="#">Another action</a></li>
    </ul>
    
    0 讨论(0)
  • 2020-11-29 16:29

    You can use the built-in CSS class pre-scrollable in bootstrap 3 inside the span element of the dropdown and it works immediately without implementing custom css.

     <ul class="dropdown-menu pre-scrollable">
                    <li>item 1 </li>
                    <li>item 2 </li>
    
     </ul>
    
    0 讨论(0)
  • 2020-11-29 16:32

    For CSS, I found that max height of 180 is better for mobile phones landscape 320 when showing browser chrome.

    .scrollable-menu {
        height: auto;
        max-height: 180px;
        overflow-x: hidden;
    }
    

    Also, to add visible scrollbars, this CSS should do the trick:

    .scrollable-menu::-webkit-scrollbar {
        -webkit-appearance: none;
        width: 4px;        
    }    
    .scrollable-menu::-webkit-scrollbar-thumb {
        border-radius: 3px;
        background-color: lightgray;
        -webkit-box-shadow: 0 0 1px rgba(255,255,255,.75);        
    }
    

    The changes are reflected here: https://www.bootply.com/BhkCKFEELL

    0 讨论(0)
  • 2020-11-29 16:36

    Do everything in the inline of UL tag

    <ul class="dropdown-menu scrollable-menu" role="menu" style="height: auto;max-height: 200px; overflow-x: hidden;">
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                    <li><a href="#">Something else here</a></li>
                    <li><a href="#">Action</a></li>
                    ..
                    <li><a href="#">Action</a></li>
                    <li><a href="#">Another action</a></li>
                </ul>
    
    0 讨论(0)
  • 2020-11-29 16:44

    I think you can simplify this by just adding the necessary CSS properties to your special scrollable menu class..

    CSS:

    .scrollable-menu {
        height: auto;
        max-height: 200px;
        overflow-x: hidden;
    }
    

    HTML

           <ul class="dropdown-menu scrollable-menu" role="menu">
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
              <li><a href="#">Something else here</a></li>
              <li><a href="#">Action</a></li>
                ..
              <li><a href="#">Action</a></li>
              <li><a href="#">Another action</a></li>
           </ul>
    

    Working example: https://www.bootply.com/86116

    Bootstrap 4

    Another example for Bootstrap 4 using flexbox

    0 讨论(0)
  • 2020-11-29 16:45

    i hope this code is work well,try this.

    add css file.

    .scrollbar {
        height: auto;
        max-height: 180px;
        overflow-x: hidden;
    }
    

    HTML code:

    <div class="col-sm-2  scrollable-menu" role="menu">
        <div>
       <ul>
      <li><a class="active" href="#home">Tutorials</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
      <li><a href="#news">News</a></li>
      <li><a href="#contact">Contact</a></li>
      <li><a href="#about">About</a></li>
    
        </ul>
       </div>
       </div>
    
    0 讨论(0)
提交回复
热议问题