Troubleshooting issues with the Bootstrap4 navbar toggle functionality

Having some trouble toggling the navbar button. It's not working as expected - when I click on it, the toggle background appears and disappears immediately. Here is my code snippet along with a link to the Fiddle file:

https://jsfiddle.net/85uyhbv6/

<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark sticky-top bg-custom">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">Brand</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse text-center" id="navbarSupportedContent">
            <ul class="navbar-nav navbar-center">
                <li class="nav-item active">
                    <a class="nav-link" href="#top-img">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#games-tag">Games</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#team-tag">Team</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About Us</a>
                </li>
            </ul>
        </div>
    </div>
</nav>
.bg-gray{
    background-color: #b8b8b8;
}

.bg-custom{
    background-color: #170c37;
}

.navbar-brand{
    font-family: roboto-thin;
    font-weight: bold;
    font-size: 28px;
}

.navbar-dark .navbar-nav .nav-link{
    font-family: roboto-thin;
    font-weight: 700;
    font-size: 16px;
}

.navbar-dark .navbar-nav .nav-item{
    margin: 0 15px;
}

.navbar-center{
    position: absolute; 
    left: 50%; 
    transform: translatex(-50%)
}

I suspect that the issue might be with the positioning of absolute elements, but I'm struggling to center the items correctly without it. Any help would be greatly appreciated.

Answer №1

If you take out the CSS from .navbar-center, everything will function as expected without impacting the alignment of the menu items, as they are already centered by default.

Alternatively

Simply include min-height: 160px; in the .collapse.show class and it will solve the issue.

Below is a functional example:

.collapse.show {
    min-height: 160px;
}

.bg-gray{
    background-color: #b8b8b8;
}

.bg-custom{
    background-color: #170c37;
}

.navbar-brand{
    font-family: roboto-thin;
    font-weight: bold;
    font-size: 28px;
}

.navbar-dark .navbar-nav .nav-link{
    font-family: roboto-thin;
    font-weight: 700;
    font-size: 16px;
}

.navbar-dark .navbar-nav .nav-item{
    margin: 0 15px;
}

.navbar-center{
    position: absolute; 
    left: 50%; 
    transform: translatex(-50%)
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<!-- Navigation -->
<nav class="navbar navbar-expand-lg navbar-dark sticky-top bg-custom">
    <div class="container-fluid">
        <a class="navbar-brand" href="#">Brand</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>

        <div class="collapse navbar-collapse text-center" id="navbarSupportedContent">
            <ul class="navbar-nav navbar-center">
                <li class="nav-item active">
                    <a class="nav-link" href="#top-img">Home <span class="sr-only">(current)</span></a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#games-tag">Games</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#team-tag">Team</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">About Us</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

Answer №2

Update the CSS of .navbar-center class by replacing position: absolute; with position: relative;

.bg-gray{
    background-color: #b8b8b8;
}

.bg-custom{
    background-color: #170c37;
}

.navbar-brand{
    font-family: roboto-thin;
    font-weight: bold;
    font-size: 28px;
}

.navbar-dark .navbar-nav .nav-link{
    font-family: roboto-thin;
    font-weight: 700;
    font-size: 16px;
}

.navbar-dark .navbar-nav .nav-item{
    margin: 0 15px;
}

.navbar-center{
    /*position: absolute;*/
    position: relative;
    left: 50%; 
    transform: translatex(-50%)
}
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <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.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
    <link href="../static/css/style.css" rel="stylesheet">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>

<body>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark sticky-top bg-custom">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Brand</a>
            <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent"
                aria-controls="SupportedContent" aria-expanded="false" aria-label="Toggle navigation">
                <span class="navbar-toggler-icon"></span>
            </button>

            <div class="ollapse navbar-collapse text-center" id="SupportedContent">
                <ul class="avbar-nav navbar-center">
                    <li class="av-item active">
                        <a class="av-link" href="#top-img">Home <span class="sr-only">(current)</span></a>
                    </li>
                    <li class="av-item">
                        <a class="av-link" href="#games-tag">Games</a>
                    </li>
                    <li class="av-item">
                        <a class="ak" href="#team-tag">Team</a>
                    </li>
                    <li class="av-item">
                        <a class="al" href="#">About Us</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
  
  </body>

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

Why does the video cover extend past its lower boundary in HTML?

I'm facing an issue where a purple cover over the <video> element extends slightly beyond the video's bottom border. Here's the code I'm using: .media-cover { display: inline-block; position: relative; } .media-cover:after ...

Guide to implementing tooltips for disabled <li> elements in AngularJS

I have a list of items that are displayed using the following code: <li class="dropdown-item" data-toggle="tooltip" uib-tooltip="tooltip goes here" data-placement="left" data-ng-repeat="result in items.results.contextValues.rows " ...

What's the best way to update the value of an angular field upon submission?

Could someone please provide instructions on how to update the myName variable when the "submit" button is pressed? Thank you! app.js: app.controller('SomeController', ['$scope', 'emails', function($scope, emails) { emails ...

Custom container width causes animation to crash when scrolling

I'm having trouble with my header. When the containers change with scrolling, an animation takes place. Everything works fine with native Bootstrap CSS, but when I customize the width of the container in my custom CSS (the width set to 1140px), the an ...

Binding values in Vue.js with a prefix and suffixCreating bindings in Vue

I attempted to bind a value to an input by using a variable declared in the data object, but I also need to include a prefix and a suffix. <input id="topnavback", v-bind:value="rgb({{themestopnavback}})", class="jscolor"/> The value "themestopnavba ...

Using Object Values and Subvalues to Assign Element Attributes in jQuery

I have a weekly schedule that I update regularly using a static table layout: <table> <tr class="live gm fsp"> <td>Oct. 7</td> <td>12:30 pm</td> <td class="prog">Show 1</td> <td>Team ...

I'm having trouble getting videos to play in my Angular application. Can anyone provide guidance on resolving this issue

I'm currently working on an Angular application and trying to showcase a list of videos sourced from an Odata service. However, I am facing an issue where the video controls are visible but the video itself isn't displaying - only a black backgro ...

What is the best way to ensure your background image appears proportional?

I'm having an issue with the background image on my login page - it doesn't look like the original photo: Take a look at the screenshot to see the problem with the width of the image: https://i.sstatic.net/qb9u0.jpg Here's the HTML/jsx cod ...

Dynamic binding issue causing audio event to not fire

Currently, I have an <audio> element that is playing audio. When I manually add the event listener onplay in the HTML code, it functions as expected. <audio onplay="alert('t')" .... It's working... However, when I attempt ...

Design elements - Responsive vertical images

I am facing an issue with resizing the slider I have on my website. Currently, it is set at 1200 x 400 pixels, and while the width adjusts correctly when the page is resized, the height remains fixed at 400px. I would like the height to resize proportional ...

The :focus pseudo-class is failing to target the input element

I'm dealing with a simple input field of type text: <input matInput type="text" placeholder="{{placeholder}}" class="input-field" [ngClass]="{'error': hasErrors}" [readonly]="isReadonly&quo ...

The alignment of margins appears as intended in Opera, Chrome, and IE, but unfortunately it does not

I have exhausted all possible options, but I am still struggling to get this page to appear correctly in Firefox. When you click inside the square, a menu with images should display on the right side. It is properly aligned in every browser except for Fire ...

Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component: <Typography variant="body1" sx={bubbleStyle}> ...

transitioning a <div> between dimensions with a snap effect

It seems like it should be straightforward to achieve with bootstrap/grid, but I seem to be overlooking something. I have a container div with two column divs within it. Once the screen size hits a certain point, I want the container to transition directly ...

Challenges encountered when converting Javascript into a Django template

I am facing a challenge with converting an HTML file into a Django template. It appears that the main.js file is not functioning properly, yet there are no errors being displayed in the debug console. I recently transferred the files from my local filesys ...

An interactive top navigation bar paired with three dynamic columns housing scrollable content, all crafted seamlessly with Bootstrap 4

I recently updated my layout using bootstrap but I am facing a glitch. I want the layout to have fixed scrolling only within the columns, without affecting the entire page. Everything works fine until I add some text inside any of the columns, then the who ...

Creating an HTML table from a JSON string with AngularJS

I am completely new to angularjs and have been delving into it for the past few days. I now need to convert a JSON string from a REST endpoint into tabular data. Below is the code I've been working on. $scope.getDataCatalogue = function(){ $ ...

Java - Avoid overwriting files by renaming duplicated files instead of the selected file

Situation - I have a script called TableToCSV that is designed to convert a .html table file to a .csv file. However, it requires the user to input a file with a .html extension through the console. The issue is that the files selected often have a .xls ex ...

What strategies can we implement to ensure consistency in visual styles and templates across our microservices?

Our team is currently working on multiple microservices simultaneously. While traditional microservice architecture discourages code sharing and reuse between services, we are considering the benefits of consistent styling across all services that have Web ...

Changing the background color of radio buttons using Chrome and the power of WebGL<script>canvas</script>

When using the Three.js WebGL renderer, I noticed that radio buttons have white backgrounds, but this issue only seems to occur in Chrome. If you want to see the problem for yourself, check out this fiddle: http://jsfiddle.net/5pL8v/328/ Does anyone know ...