Delay in background color for hamburger menu in Bootstrap 4

I recently delved into the world of coding and I am just putting the finishing touches on my basic portfolio site.

The one issue that has been bothering me is the hamburger menu background. While I have successfully applied a background color to the menu using CSS, there seems to be a slight delay in the color change when clicking on it. It's a small detail, but it's been causing some frustration!

I apologize if my code is a bit messy. Any feedback or constructive criticism would be greatly appreciated!

Link: https://codepen.io/MikeyT24/pen/xrQvxe

HTML

<nav class="navbar fixed-top navbar-inverse navbar-toggleable-sm opaque-navbar">
  <div class="container containernav">
    <button class="navbar-toggler " type="button" data-toggle="collapse" data-target="#myContent" aria-controls="myContent" aria-expanded="false" aria-label="toggle navigation">
    <span class="navbar-toggler-icon"></span>
    </button>
    <h1 class="navbar-brand m-0 ">Michael Treeves</h1>
  <div class="collapse navbar-collapse" id="myContent">
  <div class="navbar-nav ml-auto">
    <a class="nav-item nav-link" href="#about">About</a>
    <a class="nav-item nav-link" href="#portfolio">Portfolio</a>
    <a class="nav-item nav-link" href="#contact">Contact</a>
  </div> <!--navbar-nav-->
  </div> <!--collapse-->
  </div> <!--container-->
</nav>

CSS

/***********************************************************
  OPAQUE NAVBAR SECTION
**********************************************************/
.opaque-navbar {
    background-color: rgba(0,0,0,0);
    height: 50px;
    border-bottom: 0px;
    transition: background-color 0.5s ease 0s;
}

.opaque-navbar.opaque {
    background-color: rgba(100,100,150,0.9);
    height: 50px;
    transition: background-color 0.5s ease 0s;
}

@media (max-width: 992px) {
  .opaque-navbar {
    height: 50px;
    transition: background-color .5s ease 0s;
}

@media (max-width: 768px){
.collapse {
    background-color: rgba(100,100,150,0.9);
}

Script (not sure if relevant)

  * OPAQUE NAVBAR SCRIPT
  **********************************************************
  */

  // Toggle tranparent navbar when the user scrolls the page

  $(window).scroll(function() {
    if($(this).scrollTop() > 650)  /*height in pixels when the navbar becomes non opaque*/
    {
        $('.opaque-navbar').addClass('opaque');
    } else {
        $('.opaque-navbar').removeClass('opaque');
    }
});

Thank you for your help and feedback!

Sincerely, Mikey

Answer №1

When you open the navbar, Bootstrap switches the .collapse class to .collapsing first, causing a delay before applying the background color change.

Try using .navbar-collapse instead.

.navbar-collapse {
    background-color: rgba(100,100,150,0.9);
}

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

How do I use CSS to organize data by row and column within a table?

I'm looking to format the table data in a column-row layout for mobile devices. Can anyone provide an example or guidance on how to achieve this? I've browsed through some discussions but haven't found a solution that works with tables. The ...

The HTML code may fade away, but the JavaScript is still up and running behind the

Switching between different div elements in my HTML document is a challenge. Here's the code I currently have: <div id="screen1" class="current"> <div id="press_any_key_to_continue"> <font style="font-family: verdana" color="yellow ...

the child div within a Bootstrap 3 column is not being properly styled

Experiencing a strange issue with a relative div within a column in Bootstrap 3. Here's my code: <div class="col-lg-6"> <div class="large" style="background: url(img/bg.jpg);"> <h1 class="grid-header">Content 1</h1> ...

sliding to the right gets jammed

I am encountering a peculiar issue with Multi-page slide functionality. Whenever I attempt to switch pages, the new page gets stuck on the left before sliding out into position. Feel free to test this problem using my JSFiddle This is the JQuery code that ...

Link not being properly contained within div element

Is there a way to ensure that the link stays contained within the div element and does not extend beyond it? It seems like the padding is causing an issue where the div considers the link to be as tall as the text. Any solutions or workarounds? Check out ...

The Street View feature on Google Maps API is now showcasing a cool,

I have been attempting to showcase the interior of various businesses using the Google Maps API street view feature. Initially, I was able to retrieve the place_id and then use it to access any available street view panoramas. However, this functionality s ...

Tips for displaying a variable that contains HTML in PHP

I'm facing an issue with a variable that holds an HTML iframe obtained from a third-party package. When I try to display it using <?php echo $variable ?>, the page shows the raw HTML with tags instead of rendering it properly. Can someone guide ...

Tips for showcasing an image partially with primefaces/jsf

Looking to display just a single icon from a sprite image? Here is how you can do it using jsf/primefaces: If you tried the code below but ended up displaying the complete image, try adjusting the background-position values: <p:graphicImage value="/re ...

Wagtail one-page website

Recently, I created a one-page site in static HTML featuring shiny divs that span the height of the screen, along with a smooth scrolling function in the navigation bar. The site was designed to include a mix of plain text, images, and a card-deck. Everyth ...

Enhancing an Image Gallery using CSS

Currently in the process of building a website for an upcoming event, and naturally, I need to create an event calendar. For inspiration, I have been referencing this example for its gallery layout and hover effects. However, I am hoping to customize thi ...

Issue with CSS not appearing in Google Chrome's coverage tab

As I delve into analyzing the extent of unused CSS on my webpage, I encounter a challenge. The webpage is crafted in Angular 7, with CSS incorporated through the angular.json build configuration. The css appears to be added to the head of the html file, e ...

List items in a horizontal arrangement are not occupying the full height

<ul class="navBar"> <li><a href="#"><i class="icon-home"></i></a></li> <li><a href="#">Blog</a></li> <li><a href="#">Elements</a></li> <li><a ...

What exactly is HTML cloud storage all about?

As I work on developing an app through phonegap, one question that comes to mind is the possibility of storing information online. For instance, if there's a number variable that increases when a button is pressed, can this value be saved somewhere an ...

I'm having trouble getting the font to display properly on Safari (mac) similar to how it appears on

Recently, I completed a website for a client at . The owner requested the footer text to be styled similarly to the footer text on . However, upon review, it seems that the font in the lists on desiringgod appears thinner compared to the site I built. Thi ...

JavaScript fade effects toggle

Do you have a question? In Vue.js, achieving the fade in / fade out effect can be easily done by specifying it through the component. new Vue({ el: '#demo', data: { show: true } }) .fade-enter-active, .fade-leave-active { ...

What is the best way to upload a canvas image from a GUI to the back-end using an AJAX call and setting the content-type as "image/jpeg"?

What is the best method for saving a canvas image from GUI to back-end using an AJAX call that accepts content type "image/jpeg" and avoids the error "jquery.js: 8453 Uncaught TypeError: Illegal invocation?" HTML <canvas id="myImage"></canvas> ...

Receiving "Illegal Invocation" error when attempting to submit form using ajax

I am attempting to submit a form using ajax, and here is the form code: <form class="form-vertical" method="POST" id="request-form" action="/post_handler?request=add_data" enctype="multipart/form-data"> <div class="form-group"> <label ...

How to position a <div> in the center of a popup <div>

I've encountered a problem trying to center a <div> within another <div> that pops up when a button is clicked. It's not my code, but it's similar. Here is the HTML: <div class="popup"> <div class="options"> ...

Verify whether the object is inside the CSS border or CSS wrapper

As a newcomer to the programming world, I am venturing into my first jQuery project for my ICT school assignment. Here is an overview of the project: I have various draggable objects (images) within #wrapper, which is defined in my style.css file. ...

After sending an HTML Gmail using Nodemailer, some rules may be missing from the CSS

I am attempting to send HTML content via Nodemailer to my Gmail account. Initially, I sent the email as plain text to ensure everything was working properly. const email = { from: emailAddress, to: sendTo, subject: ...