Solutions for missing background in Bootstrap hamburger menu upon clicking

Is there a way to change the background color to white when the hamburger menu is activated in Tablet or Mobile view?

I want to customize the appearance by switching the Logo, Title, Nav-links, and Menu button colors to black with a white background instead of the current translucent style upon pressing the hamburger menu.

The only instance where I prefer everything in the Navbar to be white is in Desktop view.

Thank you!

Answer №1

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.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating a persistent footer in a modal: A step-by-step guide

I'm currently working on a react-modal that slides in from the bottom of the screen with an animated effect. The challenge I am facing is getting the footer of the modal to stay fixed at the bottom of the browser window. Despite using the standard CSS ...

Tips on how to align several divs within another div

I've been attempting to center multiple div blocks within another div or HTML document, but I haven't had any success with the methods I've found on StOv. Here's an image of what I'm aiming for: https://i.stack.imgur.com/DB7uQ.jp ...

Using CSS to select a specific class within an attribute

Having trouble targeting a specific navigation item in a div using an attribute value in CSS. Here's the code I have so far: My goal is to target the navDepts class. HTML <div class="primary-nav" data-name="about"> <div class="sub ...

Tips for creating a navigation bar that bypasses body padding in HTML and CSS

I have set padding in my 'body' tag, but I want my top navigation bar to cover the entire page without being affected by it. I attempted to fix this by setting the width to 100% and using negative padding and margin values, but it did not work a ...

The standard React Favicon appears in PDF view instead of the custom one intended

I am encountering an issue with the file structure in my React App's public folder. Here is a glimpse of the structure: https://i.sstatic.net/FjT0z.png Although I have successfully added a custom favicon to replace the default React one, it displays ...

Giant Slide - navigate directly to a particular slide using a link

Hey there, I am currently working on incorporating the Superslide slider for fullscreen images in my website. My goal is to have a mostly text-free site where users can navigate through the images using the main menu or jump to a specific image within the ...

Angular Script Linking

Hello there! I am currently attempting to add an HTML tag to my response message, but for some reason it isn't working as expected. Here is a snippet of my controller code (in case the API indicates that the account doesn't exist - see this scr ...

Transforming the DOM using jQuery

I am looking to manipulate the DOM using jQuery. This is the initial source code: <td class="name"><a href="position_details.php?x=-109&amp;y=95">21</a> </td> <td class="name"><a href="position_details.php?x=-109& ...

Tips for creating a dynamic text that adjusts to different screen sizes within a bootstrap button

When utilizing the w3.css and bootstrap libraries to add a link button, an issue arises when resizing the screen as the text inside the button does not respond accordingly. How can we ensure that the text fits within the button based on the screen resoluti ...

Tips for adding a CSS marker to the Videogular timeline at a designated time

My HTML player application allows users to search for a term and then displays the results along with the time when those words appear. By clicking on a specific sentence, the HTML player will start playing from that location. However, I would like to enha ...

Guide on navigating through various HTML pages with distinct parameters using Node.js (Express server)

Seeking assistance with a Node.js server that receives an ID as a query parameter. Each time a client connects with different parameters, I aim to serve them a unique HTML page containing a simple UI with 2 dynamic arrays. Everything seems to be working co ...

How to Use jQuery to Detect Audio Position in HTML

Is it possible to use the HTML5 Audio tag with an eventListener or action using Dom/jQuery to trigger an event during playback or at the end? This answer mentions currentTime but lacks examples. Pseudo code provided below. <!DOCTYPE html> <html& ...

Div alignment issue caused by 'Position:absolute' in Internet Explorer

When using position: absolute on a div, it causes misalignment in Internet Explorer but works correctly in Mozilla Firefox and Chrome. Does anyone have a suggested workaround for this issue? Code http://jsbin.com/uxerus/15/edit ...

What is the best way to create a personalized image as the background in WordPress using CSS for a client?

I have this in my style.css .showcase{ background: url("x.jpg") no-repeat 0; } My website is built on WordPress and I have implemented advanced custom fields for the client to edit text. However, I am struggling to find a way for them to change the ...

Eliminating HTML element within CKEditor

I'm in the process of developing a CKEditor plugin and one feature I'd like to include is the ability to delete specific HTML elements from the editor's content. For example, removing an <img id="remove-me" />. I understand that I can ...

Title vanishes when gradient is added to a div

I have encountered an issue with a recent update on my webpage. Previously, I had multiple divs with the title attribute that displayed information when users hovered over them. However, after adding a gradient background, the titles stopped appearing. Th ...

Is it possible to run quirks mode in one frame while running standards mode in another?

Back in the IE6 days, I developed an old application that relies on frames instead of iframes and runs in quirks mode. Now, I'm wondering if it's possible to have one frame remain in quirks mode while another operates in standards mode when using ...

Identify the CSS Framework being used in conjunction with Selenium

I have developed a program that crawls through various web pages and conducts tests using Selenium. My current task is to identify which CSS Frameworks are utilized on these websites for statistical analysis. Currently, I am using the FireFox Webdriver to ...

The slider class in the div is not showing the image, but it is visible in the elements

Is the sequence of loading .css and .js files in the head section important for displaying images with the .slider class on MaterializeCSS? <!DOCTYPE html> <html> <head> <!--Import Google Icon Font--> <link href="htt ...

What is the functionality of the CSS switch button?

I'm curious about how the cool turn on/off switch button actually works. Take a look at for reference. I'm interested in implementing a prevention check to confirm whether the user truly wants to switch the button. Here's a snippet of the ...