Change Hamburger-Icon

I would like to change the hamburger icon (toggler icon), but I can’t get it to work cleanly. I would like to have something like this:

Can someone help me with a code snippet that fits for bootstrap? And if possible, does anyone know a site where there are more Bootstrap toggler examples with code?

And last but not least. How do I have to do it if I want to put a SVG graphic for “normal” and “hover” instead of SVG code there?

There’s no simple tip or trick to creating something like this. It’s involved. Here’s a 20 minute video which should help you.

1 Like

Try adding this css:

 .navbar-toggler-icon {
  background-image: none!important;
  background-color: var(--bs-gray-800);
  height: 3px;
  width: 25px;
  margin: 10px 0;
  position: relative;
  transition: all 0.35s ease-out;
  transform-origin: center;
}

.navbar-toggler-icon::before {
  display: block;
  background-color: var(--bs-gray-800);
  height: 3px;
  content: "";
  position: relative;
  top: -7px;
  transition: all 0.15s ease-out;/*taken down to hide quicker*/
  transform-origin: center;
}

.navbar-toggler-icon::after {
  display: block;
  background-color: var(--bs-gray-800);
  height: 3px;
  content: "";
  position: relative;
  top: 4px;
  transition: all 0.35s ease-out;
  transform-origin: center;
}

.navbar-dark .navbar-toggler-icon,
.navbar-dark .navbar-toggler-icon::before,
.navbar-dark .navbar-toggler-icon::after {
  background-color: var(--bs-gray-100);
}

.navbar-toggler:not(.collapsed) .navbar-toggler-icon {
  transform: rotate(45deg);
}

.navbar-toggler:not(.collapsed) .navbar-toggler-icon::before {
  opacity: 0;
}

.navbar-toggler:not(.collapsed) .navbar-toggler-icon::after {
  transform: rotate(-90deg) translateX(7px);
}

Then make sure to add the collapsed class to the Navbar Toggle as this:

I might try improving it later to see if I can add more animation

1 Like

Excellent @richards

One small improvement could be add .navbar-light to the classes .navbar-toggler-icon and then dubblicate the classes and change the .navbar-light to .navbar-dark, and change the background-color to a light color on the .navbar-dark .navbar-toggler-icon classes , then your CSS will work for both dark and light navbar when one change the text-color in the options panel for the navbar

1 Like

Thank you and good idea @kuligaposten

I have updated the css just adding this:


.navbar-dark .navbar-toggler-icon,
.navbar-dark .navbar-toggler-icon::before,
.navbar-dark .navbar-toggler-icon::after {
  background-color: var(--bs-gray-100);
}
1 Like

@richards

If you remove the transition on the middle bar the one you have top -7px then the animation looks a little smoother

Funnily enough I have just been tweeking it a bit to this:

.navbar-toggler:not(.collapsed) .navbar-toggler-icon {
  transform: rotate(45deg);
}

.navbar-toggler:not(.collapsed) .navbar-toggler-icon::before {
  opacity: 0;
}

.navbar-toggler:not(.collapsed) .navbar-toggler-icon::after {
  transform: rotate(-90deg) translateX(7px) ;
}

Edit: and changing the transform-origin to center rather than center left

@kuligaposten @richards

Wow, great, that works out beautifully. Thank you so much.

Can you guys help me out again? How do I make it so that when I click on the toggle, the background of the whole browser window is black, and the menu items are centered in height?

@rumede

.navbar-collapse {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

You’re welcome. I’ve just tidied up the css and updated in the original post

I have added this to the online library as Animated Toggle NavBar BS5

2 Likes

Thanks to @kuligaposten for cleaning up my css to make the dropdown transition a lot smoother, Version 2 is now available on the online library: Animated Toggle NavBar BS5 V2

1 Like

@richards Thank you so much. Very nice…

One little point: When i will Expand the navbar on -lg and above, the background is black and the navbar is horizontal centered too. Is there a possibility to combine e solution vor expanded and not exanded?

You can just add a media query to the navbar collapse

Edit: courtesy of @kuligaposten :slight_smile:

@media (max-width: 991px) {
  .navbar-collapse {
    --navbar-height: 56px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: calc(100vh - var(--navbar-height));
    transition: all 320ms ease-in-out;
  }
}

.navbar-collapse.collapsing {
  transition-property: height, visibility;
  transition-duration: 0s;
}

.navbar-toggler, .navbar-toggler:active, .navbar-toggler:focus {
  box-shadow: none;
  border: none;
  outline: none;
}