Change bootstrap navbar collapse breakpoint without using LESS

前端 未结 19 2231
囚心锁ツ
囚心锁ツ 2020-11-22 07:55

Currently when the browser width drops below 768px, the navbar changes to collapsed mode. I want to change this width to 1000px so when the browser is below 1000px the navba

相关标签:
19条回答
  • 2020-11-22 08:41

    This worked for me Bootstrap3

    @media (min-width: 768px) {
        .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;
          }
    }
    

    Took a while to figure these two out:

    .navbar-collapse.collapse {
       display: none!important;
    }
    .navbar-collapse.collapse.in {
       display: block!important;
    }
    
    0 讨论(0)
  • 2020-11-22 08:43

    Finally worked out how to change the collapse width by fiddling with '@grid-float-breakpoint' at http://getbootstrap.com/customize/.

    Go to line 2923 in bootstrap.css(also min version) and change @media screen and (min-width: 768px) { to @media screen and (min-width: 1000px) {

    So the code will end up being:

    @media screen and (min-width: 1000px) {
      .navbar-brand {
        float: left;
        margin-right: 5px;
        margin-left: -15px;
      }
      .navbar-nav {
        float: left;
        margin-top: 0;
        margin-bottom: 0;
      }
      .navbar-nav > li {
        float: left;
      }
      .navbar-nav > li > a {
        border-radius: 0;
      }
      .navbar-nav.pull-right {
        float: right;
        width: auto;
      }
      .navbar-toggle {
        position: relative;
        top: auto;
        left: auto;
        display: none;
      }
      .nav-collapse.collapse {
        display: block !important;
        height: auto !important;
        overflow: visible !important;
      }
    }
    
    0 讨论(0)
  • 2020-11-22 08:45

    2018 UPDATE

    Bootstrap 4

    Changing the navbar breakpoint is easier in Bootstrap 4 using the navbar-expand-* classes:

    <nav class="navbar fixed-top navbar-expand-sm">..</nav>

    • navbar-expand-sm = mobile menu on xs screens <576px
    • navbar-expand-md = mobile menu on sm screens <768px
    • navbar-expand-lg = mobile menu on md screens <992px
    • navbar-expand-xl = mobile menu on lg screens <1200px
    • navbar-expand = never use mobile menu
    • (no expand class) = always use mobile menu

    If you exclude navbar-expand-* the mobile menu will be used at all widths. Here's a demo of all 6 navbar states: Bootstrap 4 Navbar Example

    You can also use a custom breakpoint (???px) by adding a little CSS. For example, here's 1300px..

    @media (min-width: 1300px){
        .navbar-expand-custom {
            flex-direction: row;
            flex-wrap: nowrap;
            justify-content: flex-start;
        }
        .navbar-expand-custom .navbar-nav {
            flex-direction: row;
        }
        .navbar-expand-custom .navbar-nav .nav-link {
            padding-right: .5rem;
            padding-left: .5rem;
        }
        .navbar-expand-custom .navbar-collapse {
            display: flex!important;
        }
        .navbar-expand-custom .navbar-toggler {
            display: none;
        }
    }
    

    Bootstrap 4 Custom Navbar Breakpoint
    Bootstrap 4 Navbar Breakpoint Examples


    Bootstrap 3

    For Bootstrap 3.3.x, here is the working CSS to override the navbar breakpoint. Change 991px to the pixel dimension of the point at which you want the navbar to collapse...

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

    Working example for 991px: http://www.bootply.com/j7XJuaE5v6
    Working example for 1200px: https://www.codeply.com/go/VsYaOLzfb4 (with search form)

    Note: The above works for anything over 768px. If you need to change it to less than 768px the example of less than 768px is here.


    0 讨论(0)
  • 2020-11-22 08:46

    I just did this successfully by using the following CSS code.

    @media(max-width:1000px)  {
    
        .navbar-collapse.collapse {
            display: none !important;
        }
    
        .navbar-collapse {
            overflow-x: visible !important;
        }
    
        .navbar-collapse.in {
          overflow-y: auto !important;
        }
    
        .collapse.in {
          display: block !important;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 08:47

    Here my working code using in React with Bootstrap

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u"
        crossorigin="anonymous">
    <style>
      @media (min-width: 768px) and (max-width: 1000px) {
       .navbar-collapse.collapse {
           display: none !important;
       }
       .navbar-toggle{
            display: block !important;
       }
       .navbar-header{
            float: none;
       }
    }
      </style>
    
    0 讨论(0)
  • 2020-11-22 08:48

    This is the working code for me. (Also the dropdown navbar links works)

    @media (max-width: 1199px) {
      .navbar-nav {
        margin: 7.5px -15px;
     }
      .ul-links {
        display: block;
      }
      .navbar-nav>li>a {
        padding-top: 10px;
        padding-bottom: 10px;
        line-height: 20px;
      }  
      .navbar-header {
          float: none;
      }
      .navbar-left,.navbar-right {
          float: none !important;
      }
      .navbar-toggle {
          display: block;
      }
    
      .navbar-collapse.collapse {
          display: none!important;
          max-height: none;
      }
      .navbar-nav {
          float: none!important; 
      }
      .navbar-nav>li {
          float: none;
      } 
      .collapse.in{
          display:block !important;
      }
     .navbar-nav .open .dropdown-menu {
         position: static;
         float: none;
         width: auto;
         margin-top: 0;
         background-color: transparent;
         border: 0;
         -webkit-box-shadow: none;
         box-shadow: none;
      }
    }
    
    0 讨论(0)
提交回复
热议问题