Bootstrap 3 Navbar Collapse

后端 未结 12 2054
长发绾君心
长发绾君心 2020-11-22 09:27

Is there any way to increase the point at which the bootstrap 3 navbar collapses (i.e. so that it collapses into a drop down on portrait tablets)?

These two were app

相关标签:
12条回答
  • 2020-11-22 10:20

    Thanks to Seb33300 I got this working. However, an important part seems to be missing. At least in Bootstrap version 3.1.1.

    My problem was that the navbar collapsed accordingly at the correct width, but the menu button didn't work. I couldn't expand and collapse the menu.

    This is because the collapse.in class is overrided by the !important in .navbar-collapse.collapse, and can be solved by also adding the "collapse.in". Seb33300's example completed below:

    @media (max-width: 991px) {
        .navbar-header {
            float: none;
        }
        .navbar-toggle {
            display: block;
        }
        .navbar-collapse {
            border-top: 1px solid transparent;
            box-shadow: inset 0 1px 0 rgba(255,255,255,0.1);
        }
        .navbar-collapse.collapse {
            display: none!important;
        }
        .navbar-collapse.collapse.in {
            display: block!important;
        }
        .navbar-nav {
            float: none!important;
            margin: 7.5px -15px;
        }
        .navbar-nav>li {
            float: none;
        }
        .navbar-nav>li>a {
            padding-top: 10px;
            padding-bottom: 10px;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 10:20

    I wanted to upvote and comment on jmbertucci's answer, but I'm not yet to be trusted with the controls. I had the same issue and resolved it as he said. If you are using Bootstrap 3.0, edit the LESS variables in variables.less The responsive break points are set around line 200:

    @screen-xs:                  480px;
    @screen-phone:               @screen-xs;
    
    // Small screen / tablet
    @screen-sm:                  768px;
    @screen-tablet:              @screen-sm;
    
    // Medium screen / desktop
    @screen-md:                  992px;
    @screen-desktop:             @screen-md;
    
    // Large screen / wide desktop
    @screen-lg:                  1200px;
    @screen-lg-desktop:          @screen-lg;
    
    // So media queries don't overlap when required, provide a maximum
    @screen-xs-max:              (@screen-sm - 1);
    @screen-sm-max:              (@screen-md - 1);
    @screen-md-max:              (@screen-lg - 1);
    

    Then set the collapse value for the navbar using the @grid-float-breakpoint variable at about line 232. By default it's set to @screen-tablet . If you want you can use a pixel value, but I prefer to use the LESS variables.

    0 讨论(0)
  • 2020-11-22 10:20

    I think I found a simple solution to changing the collapse breakpoint, only through css.

    I hope others can confirm it since I didn't test it thoroughly and I'm not sure if there are side effects to this solution.

    You have to change the media query values for the following class definitions:

    @media (min-width: BREAKPOINT px ){
        .navbar-toggle{display:none}
    }
    
    @media (min-width: BREAKPOINT px){
        .navbar-collapse{
            width:auto;
            border-top:0;box-shadow:none
        }
        .navbar-collapse.collapse{
            display:block!important;height:auto!important;padding-bottom:0;overflow:visible!important
        }
        .navbar-collapse.in{
            overflow-y:visible
       }
       .navbar-fixed-top .navbar-collapse,.navbar-static-top .navbar-collapse,.navbar-fixed-bottom .navbar-collapse{
            padding-left:0;padding-right:0
        }
    }
    

    This is what worked for me on my current project, but I still need to change some css definitions to arrange the menu properly for all screen sizes.

    Hope this helps.

    0 讨论(0)
  • 2020-11-22 10:24

    And for those who want to collapse at a width less than the standard 768px (expand at a width less than 768px), this is the css needed:

    @media (min-width: 600px) {
    .navbar-header {
            float: left;
    }
    .navbar-toggle {
        display: none;
    }
    .navbar-collapse {
        border-top: 0 none;
        box-shadow: none;
        width: auto;
    }
    .navbar-collapse.collapse {
        display: block !important;
        height: auto !important;
        padding-bottom: 0;
        overflow: visible !important;
    }
    .navbar-nav {
        float: left !important;
        margin: 0;
    }
    .navbar-nav>li {
        float: left;
    }
    .navbar-nav>li>a {
        padding-top: 15px;
        padding-bottom: 15px;
    }
    }
    
    0 讨论(0)
  • 2020-11-22 10:25

    I did the CSS way with my install of Bootstrap 3.0.0, since I am not that familiar with LESS. Here is the code that I added to my custom css file (which I load after bootstrap.css) which allowed me to control so the menu was always working like an accordion.
    Note: I wrapped my whole navbar inside a div with the class sidebar to separate out the behavior I wanted so it did not affect other navbars on my site, like the main menu. Adjust along your needs, hope it is of help.

    /* Sidebar Menu Fix */
    
    .sidebar .navbar-nav {
      margin: 7.5px -15px;
    }
    .sidebar .navbar-nav > li > a {
      padding-top: 10px;
      padding-bottom: 10px;
      line-height: 20px;
    }
    .sidebar .navbar-collapse {
      max-height: 500px;
    }
    .sidebar .navbar-nav .open .dropdown-menu {
      position: static;
      float: none;
      width: auto;
      margin-top: 0;
      background-color: transparent;
      border: 0;
      box-shadow: none;
    }
    .sidebar .dropdown-menu > li > a {
      color: #777777;
    }
    .sidebar .navbar-default .navbar-nav .open .dropdown-menu > li > a:hover,
    .sidebar .navbar-default .navbar-nav .open .dropdown-menu > li > a:focus {
      color: #333333;
      background-color: transparent;
    }
    .sidebar .navbar-default .navbar-nav .open .dropdown-menu > .active > a,
    .sidebar .navbar-default .navbar-nav .open .dropdown-menu > .active > a:hover,
    .sidebar .navbar-default .navbar-nav .open .dropdown-menu > .active > a:focus {
      color: #555555;
      background-color: #e7e7e7;
    }
    .sidebar .navbar-nav {
      float: none;
    }
    .sidebar .navbar-nav > li {
      float: none;
    }
    

    Additional note: I also added a change to the toggle of the main menu navbar (since the code above on my site is used for a "submenu" on the side.

    I changed:

    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
    

    into:

    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse-topmenu">
    

    And then also adjusted:

    <div class="navbar-collapse collapse navbar-collapse">
    

    into:

    <div class="navbar-collapse collapse navbar-collapse-topmenu">
    

    If you will only have one navbar I guess you will not need to worry about this, but it helped me in my case.

    0 讨论(0)
  • 2020-11-22 10:26

    Easiest way is to customize bootstrap

    find variable:

     @grid-float-breakpoint
    

    which is set to @screen-sm, you can change it according to your needs. Hope it helps!

    0 讨论(0)
提交回复
热议问题