Whenever the .navbar-collapse
element is expanded, it is assigned the show
class to facilitate applying appropriate styles. In your scenario, however, you also need to update the class list of the .navbar
element accordingly. This is where the Bootstrap collapse events come in handy as they indicate when the menu is open.
<script>
const navbar = $('nav.navbar');
const navbarCollapse = $('.navbar-collapse');
// Adding classes to indicate the navbar state
navbarCollapse.on('show.bs.collapse', function () {
navbar.addClass('expanded');
});
navbarCollapse.on('hide.bs.collapse', function () {
navbar.removeClass('expanded');
});
</script>
Next step involves defining the necessary styles. Here's a simple demonstration using media query, although the media query might not be essential. The collapsing feature is intended for smaller screens, meaning the event handlers will only add the expanded
class to the nav element on tablet and mobile devices. Essentially, an expanded navbar won't be applicable on Desktop view.
/* The media query and max-width are included for illustrative purposes */
@media only screen and (max-width: 1024px){
.navbar.expanded {
background-color: white;
}
.navbar.expanded .navbar-collapse {
background-color: white;
}
.navbar.expanded .navbar-toggler-icon{
color: black;
}
.navbar.expanded h2{
color: black;
}
.navbar.expanded #navbarToggle>ul>li>a{
color: black;
}
}
Upon examining your code, I noticed that the logo and menu button are SVGs. Depending on the navbar state, you may need to switch images or embed the SVG code directly into the HTML to allow customization with CSS.