Change bootstrap navbar collapse breakpoint without using LESS

前端 未结 19 2230
囚心锁ツ
囚心锁ツ 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:29

    Your best bet would be to use a port of the CSS processor you use.

    I'm a big fan of SASS so I currently use https://github.com/thomas-mcdonald/bootstrap-sass

    It looks like there's a fork for Stylus here: https://github.com/Acquisio/bootstrap-stylus

    Otherwise, Search & Replace is your best friend right in the css version...

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

    In bootstrap 4, if you want to over-ride when navbar-expand-*, expands and collapses and shows and hides the hamburger (navbar-toggler) you have to find that style/definition in bootstrap.css, and redefine it in your own customstyle.css (or directly in bootstrap.css if you are so inclined).

    Eg. I wanted the navbar-expand-lg to collapses and shows the navbar-toggler at 950px. In bootstrap.css I find:

    @media (max-width: 991.98px) {
      .navbar-expand-lg > .container,
      .navbar-expand-lg > .container-fluid {
        padding-right: 0;
        padding-left: 0;
      }
    }
    

    And below that ...

    @media (min-width:992px) { 
        ... lots of styling ...
    }
    

    I copied both @media queries and stuck them in my style.css, then modified the size to fit my needs. I my case I wanted it to collapse at 950px. The @media queries must need to be different sizes (I'm guessing), so I set container max-width to 949.98px and used the 950px for the other @media query and so the following code was appended to my style.css. This was not easy to detangle from twisted solutions I found on Stackoverflow and elsewhere. Hope this helps.

    @media (max-width: 949.98px) {
        .navbar-expand-lg > .container,
        .navbar-expand-lg > .container-fluid {
            padding-right: 0;
            padding-left: 0;
        }
    }
    
    @media (min-width: 950px) {
        .navbar-expand-lg {
            -webkit-box-orient: horizontal;
            -webkit-box-direction: normal;
            -ms-flex-flow: row nowrap;
            flex-flow: row nowrap;
            -webkit-box-pack: start;
            -ms-flex-pack: start;
            justify-content: flex-start;
        }
        .navbar-expand-lg .navbar-nav {
            -webkit-box-orient: horizontal;
            -webkit-box-direction: normal;
            -ms-flex-direction: row;
            flex-direction: row;
        }
        .navbar-expand-lg .navbar-nav .dropdown-menu {
            position: absolute;
        }
        .navbar-expand-lg .navbar-nav .dropdown-menu-right {
            right: 0;
            left: auto;
        }
        .navbar-expand-lg .navbar-nav .nav-link {
            padding-right: 0.5rem;
            padding-left: 0.5rem;
        }
        .navbar-expand-lg > .container,
        .navbar-expand-lg > .container-fluid {
            -ms-flex-wrap: nowrap;
            flex-wrap: nowrap;
        }
        .navbar-expand-lg .navbar-collapse {
            display: -webkit-box !important;
            display: -ms-flexbox !important;
            display: flex !important;
            -ms-flex-preferred-size: auto;
            flex-basis: auto;
        }
        .navbar-expand-lg .navbar-toggler {
            display: none;
        }
        .navbar-expand-lg .dropup .dropdown-menu {
            top: auto;
            bottom: 100%;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 08:32

    You have to write a specific media query for this, from your question, below 768px, the navbar will collapse, so apply it above 768px and below 1000px, just like that:

    @media (min-width: 768px) and (max-width: 1000px) {
       .collapse {
           display: none !important;
       }
    }
    

    This will hide the navbar collapse until the default occurrence of the bootstrap unit. As the collapse class flips the inner assets inside navbar collapse will be automatically hidden, like wise you have to set your css as you desired design.

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

    Navbars can utilize .navbar-toggler, .navbar-collapse, and .navbar-expand{-sm|-md|-lg|-xl} classes to change when their content collapses behind a button. In combination with other utilities, you can easily choose when to show or hide particular elements.

    For navbars that never collapse, add the .navbar-expand class on the navbar. For navbars that always collapse, don’t add any .navbar-expand class.

    For example :

    <nav class="navbar navbar-expand-lg"></nav>
    

    Mobile menu is showing in large screen.

    Reference : https://getbootstrap.com/docs/4.0/components/navbar/

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

    It's now supported on Bootstrap 4.4

    navbar-expand-sm 
    
    0 讨论(0)
  • 2020-11-22 08:38

    This is what did the trick for me:

    @media only screen and (min-width:769px) and (max-width:1000px){
    .navbar-collapse.collapse {
        display: none !important;
    }
    .navbar-collapse.collapse.in {
        display: block !important;
    }
    .navbar-header .collapse, .navbar-toggle {
        display:block !important;
    }
    
    0 讨论(0)
提交回复
热议问题