Changing the background color of navigation items when the navbar in Bootstrap 4 adjusts for responsiveness

Hi there, I'm having an issue with my Bootstrap 4 navbar. When I test the responsiveness of my page, the background color of my nav-items disappears and only the text is visible. Can someone guide me on how to maintain the navbar color when I click the toggler link? Below is the HTML and CSS code I am using.

I want the navbar to resemble a dropdown menu with the same background color as the navbar.

    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    body{
        font-family: 'Poppins', sans-serif;   
    }
    a{
        text-decoration: none;
        color: #555;
    }
    p{
        color: #555;
    }
    .header{
        background: radial-gradient(#fff,#ffd6d6);
    }
    
    .container{
        max-width: 1300px;
        margin: auto;
        padding-left: 25px;
        padding-right: 25px;
        
    }
    .navbar{
        width: 100%;
        height: 80px;
        min-height:22px;
        
    }
    .navbar.navbar-inverse {
        border: none;
      }
    .navbar .navbar-brand img {
        height: 85px;
        padding-left: 39%;
      }
      .navbar ul.navbar-nav li.nav-item a.nav-link {
        color: #fff;
      }
      #navbarMenu{
        align-items: flex-end;
        padding-right: 5%;
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
    <link rel="stylesheet" href="style.css">
    <link rel="stylesheet" href="css/bootstrap.css">
    <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fede0e0fbfcfbfdeeffcfbaa1bfa1bfa2edeafbeebe">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
    
</head>
<body>

    <div class="header">
        <div class="container">
            <div class="navbar">
                <nav class="navbar fixed-top navbar-toggleable-md navbar-inverse navbar-dark bg-danger navbar-expand-lg navbar-custom ">
                        <a href="#" class="navbar-brand">
                             Only Store
                        </a>
                        <button class="navbar-toggler btn btn-secondary" type="button" data-toggle="collapse" data-target="#navbarMenu" aria-controls="navbarMenu" aria-expanded="false" aria-label="Toggle navigation">
                            <span class="navbar-toggler-icon"></span>
                        </button>
                        
                        <div class="collapse navbar-collapse justify-content-end" id="navbarMenu">
                            <ul class="navbar-nav mr-auto">
                                <li class="nav-item active">
                                    <a href="#" class="nav-link">Home<span class="sr-only"></span> </a> 
                                </li>
                                <li class="nav-item">
                                    <a href="#" class="nav-link">Products</a> 
                                </li>
                                <li class="nav-item">
                                    <a href="#" class="nav-link">About</a> 
                                </li>
                                <li class="nav-item">
                                    <a href="#" class="nav-link">Contact</a> 
                                </li>
                                <li class="nav-item">
                                    <a href="#" class="nav-link">Account</a> 
                                </li>
                                <li class="nav-item">
                                    <a href="#" class="nav-link">
                                        <img src="Images/cart.png" width="30px" height="30px">
                                        <a href="javascript:void(0);" class="icon" onclick="menutoggle"></a>
                                            <i class="fa fa-bars"></i>
                                    </a>
                                </li>
                            </ul>
                            </div>
                        
                    </nav>
        </div>

Answer №1

Have you considered applying the background color to the .collapse element and its nested ul?

Please refer to the example below:

    *{
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }
    body{
        font-family: 'Poppins', sans-serif;   
    }
    a{
        text-decoration: none;
        color: #555;
    }
    p{
        color: #555;
    }
    .header{
        background: radial-gradient(#fff,#ffd6d6);
    }
    .collapse, ul {
        background-color: #121212; 
    }
    .container{
        max-width: 1300px;
        margin: auto;
        padding-left: 25px;
        padding-right: 25px;
        
    }
    .navbar{
        width: 100%;
        height: 80px;
        min-height:22px;
        
    }
    .navbar.navbar-inverse {
        border: none;
      }
    .navbar .navbar-brand img {
        height: 85px;
        padding-left: 39%;
      }
      .navbar ul.navbar-nav li.nav-item a.nav-link {
        color: #fff;
      }
      #navbarMenu{
        align-items: flex-end;
        padding-right: 5%;
    }
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <meta name="viewport" content="width=device-width, initial-scale=1.0>
    <title></title>
    <link rel="stylesheet" href="style.css>
    <link rel="stylesheet" href="css/bootstrap.css>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js></script>
    
</head>
<body>

    <div class="header>
        <div class="container>
            <div class="navbar>
                <nav class="navbar fixed-top navbar-toggleable-md navbar-inverse navbar-dark bg-danger navbar-expand-lg navbar-custom ">
                        <a href="#" class="navbar-brand>
                             Only Store
                        </a>
                        <button class="navbar-toggler btn btn-secondary" type="button" data-toggle="collapse" data-target="#navbarMenu" aria-controls="navbarMenu" aria-expanded="false" aria-label="Toggle navigation>
                            <span class="navbar-toggler-icon></span>
                        </button>
                        
                        <div class="collapse navbar-collapse justify-content-end" id="navbarMenu>
                            <ul class="navbar-nav mr-auto>
                                <li class="nav-item active>
                                    <a href="#" class="nav-link>Home<span class="sr-only></span> </a></li>
                                <li class="nav-item>
                                    <a href="#" class="nav-link>Products</a> </a></li>
                                <li class="nav-item>
                                    <a href="#" class="nav-link>About</a> </a></li>
                                <li class="nav-item>
                                    <a href="#" class="nav-link>Contact</a> </a></li>
                                <li class="nav-item>
                                    <a href="#" class="nav-link>Account</a> </a></li>
                                <li class="nav-item>
                                    <a href="#" class="nav-link>
                                        <img src="Images/cart.png" width="30px" height="30px>
                                        <a href="javascript:void(0);" class="icon" onclick="menutoggle"></a><i class="fa fa-bars"></i></a></li>
                            </ul>
                            </div>
                        
                    </nav>
        </div>

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

Can Chrome Support Bookmarklets?

While attempting to craft a bookmarklet in Chrome using the console, I encountered the following error: Refused to load the script 'https://code.jquery.com/jquery-1.6.1.min.js' because it violates the following Content Security Policy directive: ...

Arranging DIV elements into rows and columns within a table

After working all night to fix a problem, my goal is to create this timetable: https://i.sstatic.net/dAt1J.png Specifically, I'm struggling to find a solution for rows 5, 6, and 7 in the first column. The image is from a program that only runs in IE ...

What is preventing me from retrieving WP custom fields value or post ID using Ajax?

After successfully generating a link based on the visitor's location, I encountered an issue with caching when using full-page caching. To address this problem, I decided to implement AJAX to dynamically load the link. While my code worked well in ret ...

Update the displayed number on the input field as a German-formatted value whenever there is a change in the input, all while maintaining

In German decimal numbers, a decimal comma is used. When formatting, periods are also used for grouping digits. For example, the number 10000.45 would be formatted as 10.000,45. I am looking to create an input field (numeric or text) where users can input ...

Tips for modifying the content of a div within an HTML document after a certain period of time has elapsed, and then repeating the process

Currently, I am in the process of developing an Android app that will function as a Digital Display. One of the challenges I am facing involves displaying an HTML page with multiple regions or boxes, each containing various items that need to switch dynami ...

Ensure Website Accessibility by Implementing Minimum Resolution Requirements

Is it possible to create a website that only opens on screens with a resolution of at least 1024 x 768, and displays an error message on unsupported resolutions? I've tried using JavaScript to achieve this, but haven't had any success. Any assis ...

jQuery blur unless

I would like to implement a feature where a div is hidden when a text box loses focus, except if the user clicks on a specific div. If the user clicks on that particular div, then the action of losing focus should not trigger the hiding of the div with cla ...

Is it possible to caution users before refreshing the page, while still allowing them to

I have a script that displays a warning message to the user when they attempt to refresh the page. It works perfectly for that purpose. However, my issue is that this same alert pops up when a user clicks on a button or a link within the website itself. I ...

Implementing the JQueryAjax function in various locations

I'm facing an issue with my HTML page layout <DIV ID="PI"><div id="selection1" style="display: table-row;"> PI CODE:<input id="ecode" type="text" name="ecode" onblur="loadEname(this.value);" value="<?php echo $code; ...

Activating Dynamic Functionality through JavaScript Input

I am currently working on a form in Apex 4.1 where I have an address lookup field that utilizes JavaScript to connect to an address database and populate separate fields with the address details (address1, address2, town, postcode). On the same page, I ha ...

Achieving Efficiency with Handlebars: Streamlining Remote Template Organization and

I am looking for a way to better organize my HB template by splitting it into different HTML files. In this case, I have created a file called helpers.html. This file contains two script tags: <script id='alert' type='text/template>... ...

Get rid of all the formatting on an HTML button or submit

Is there a method to entirely eliminate the styling of a button in Internet Explorer? I am utilizing a CSS sprite for my button and everything appears fine. However, when I click on the button, it shifts slightly upwards which distorts its appearance. Are ...

Is gulp.dest set to match the current file?

I'm having trouble directing my compiled .css file to the same directory as its original .scss version. gulp.task('sass', function() { return gulp.src([ appDir + '/*.scss', appDir + '/**/*. ...

Unable to access a PHP file within a JavaScript loop

In my HTML document, there is a button with an onclick method inside a JavaScript <script> tag. When this button is clicked, it triggers the deleteRow function, which is responsible for removing a row from a table on the page. Within the deleteRow f ...

What steps can I take to ensure that the elements are in the same row instead of being displayed in three separate rows?

(I'm a beginner in web development and need some help) Is there a way to align elements into the same row instead of stacking them up in separate rows? I'm working on creating a header bar similar to the one on the Naive UI Documentation Website. ...

The issue with data targeting in Bootstrap 3 persists

header.php code <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Strict//EN"> <html lang="EN"> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=UT ...

What methods does Coinbase use to achieve this effect? Is it possible to recreate it in a React application? (Dropdown menu positioned relative to a button on hover or click

Notice the hover effect on the Businesses section, where a prepositioned dropdown menu appears, spanning the full width of the screen. How was this effect achieved and can it be recreated in React if it wasn't originally coded using React? https://i. ...

Combining the value of $(this) to create an identifier name

I am attempting to create a hover effect on an h1 element that triggers the glowing effect on a span element with an id that corresponds to the value of the h1. While I have successfully set up a glowing effect for a sentence, I am struggling to replicate ...

What is the best way to transfer JSON address records to a list of Address objects in C#?

My query pertains to a table containing HTML text inputs where users can add up to 10 rows to input customer addresses. I am utilizing the ASP.NET MVP pattern to develop the application. I am seeking guidance on how to pass a JSON object to the properties ...

What is the best way to center a div horizontally when the width is unknown?

Check out the full screen result here: http://jsfiddle.net/anik786/UYkSf/4/embedded/result/ Here's where you can find the HTML/CSS: http://jsfiddle.net/anik786/UYkSf/4/ This is the HTML code: <div class="gallery_container"> <div class="pic ...