How to hide and replace navbar-toggler-icon in Boo

2019-05-23 15:03发布

问题:

How do you hide and replace the navbar-toggler-icon in Bootstrap 4? Right now the code below just puts the 'X' below the hamburger menu.

<nav class="navbar navbar-toggleable-md">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" 
  data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" 
  aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>

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

     <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" 
  data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" 
  aria-label="Toggle navigation">
    <span>X</span>
  </button>
</nav>

回答1:

The code you have now has 2 togglers, but I'm not sure why. You just need to replace the normal navbar-toggler-icon with an X..

<nav class="navbar navbar-faded navbar-toggleable-md">
    <button class="navbar-toggler navbar-toggler-right py-2" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault" aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
       <span>X</span>
    </button>
    <a href="" class="navbar-brand">Brand</a>
    <div class="collapse navbar-collapse" id="navbarsExampleDefault">
        <ul class="navbar-nav">
            <li><a href="">Link</a></li>
        </ul>
    </div>
</nav>

EDIT To toggle between the hamburger toggler icon and a close X, you'd need have both the X and hamburger icon inside the navbar-toggler. Add some CSS to handle the collapsed state of the navbar.

.navbar-toggler>.close {
    display:inline;
}
.navbar-toggler.collapsed>.close, .navbar-toggler:not(.collapsed)>.navbar-toggler-icon {
    display:none;
}

http://www.codeply.com/go/fgHv33Q6UM


Update Bootstrap 4.2

The original question/answer is for Bootstrap 4 alpha. Once Bootstrap 4 was in beta, the navbar-toggleable-* classes changed to navbar-expand-* which effect the navbar breakpoint. Here is an updated example for Bootstrap 4.2...

https://www.codeply.com/go/9AQ4AsYUph