The Bootstrap 5 navigation bar fails to expand properly on mobile devices

I am currently developing a website using Bootstrap 5, but I am facing an issue with the navbar. The problem occurs when viewing the website on mobile devices as the navbar does not expand properly.

@import url('https://fonts.googleapis.com/css2?family=Poppins:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}
/* style - header */
.navbar {
    background-color: #131313;
    padding: 1rem 8rem;
    z-index: 1000;

}

.navbar .navbar-brand {

    font-size: 1.4rem;
    font-weight: 800;
}

#navbarSupportedContent > ul > li:nth-child(n) > a {
color: #fff;
font-size: 1.1rem;
padding: 0 0.8rem;
}

#navbarSupportedContent > ul > li:nth-child(n) > a:hover {
color: #FF3633;
}

#navbarSupportedContent button {
background-color: #FF3633;
font-weight: 600;
padding: 0.4rem 1.4rem;
border-radius: 30px;
outline: none;
}

#navbarSupportedContent button:hover {
background-color: #CC3036;
}

Answer №1

The latest version of Bootstrap, Bootstrap 5, has uniquely identified all data attribute names with the prefix data-bs-*. This means that existing code needs to be updated accordingly.

data-target="#navbarSupportedContent"

should now be changed to:

data-bs-target="#navbarSupportedContent"

Answer №2

If you're experiencing issues with collapsing the navbar using a button, it could be due to missing main attribute tags in your code. The correct attributes to use are

data-toggle="collapse" data-target=".nav-collapse"
. Additionally, make sure you have added the necessary links or bundles from https://getbootstrap.com/docs/5.0/getting-started/introduction/

<script src="https://cdn.jsdelivr.net/npm/@popperjs/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="284b475a4d681a061e0618">[email protected]</a>/dist/umd/popper.min.js" integrity="sha384-KsvD1yqQ1/1+IA7gi3P0tyJcT3vR+NdBTt13hSJ2lnve8agRGXTTyNaBYmCR/Nwi" crossorigin="anonymous"></script>

I hope this information proves helpful for you.

Answer №3

Incorporating JavaScript codes from an external file, I successfully resolved the issue before the closure of the body tag.

Here is the JavaScript code snippet that helped me tackle and resolve the problem:

$(document).ready(function(){

  //--Navbar_Toggle_icon--//
  NavItemsStatus = '';
  $('.navbar-toggler-icon').mousedown(function(){
    //---Check if the NavItems are opened, if so then close them, otherwise open them--//
    if((NavItemsStatus == "")|| (NavItemsStatus == undefined)){
      NavItemsStatus = 'on';
      //--Do nothing here as the nav items are sliding down after clicked for the first time--// 
    }else if((NavItemsStatus == "off")){
      NavItemsStatus = 'on';
      $("#navbarNavAltMarkup").show(600); //the id which was called on data-bs-target
    }else{
      NavItemsStatus = 'off';
      $("#navbarNavAltMarkup").hide();
      $("#navbarNavAltMarkup").css('height','0px');
    }
  })

});

For your situation, make sure to use the ID #navbarSupportedContent.

Answer №4

In order for the navigation bar to function properly, it is necessary to incorporate some Javascript code, even if you do not intend to use it. The links provided in Bootstrap's "get started" section for JS may not work as expected.

To simplify the process, here is the header section where you can insert your desired title:

<head>
  <meta charset="utf-8">
  <title>TinDog</title>
  <link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="6e0c01011a1d1a1a1c0f1e2e5b405e405e430c0b1a0f5d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6" crossorigin="anonymous">
  <link rel="stylesheet" href="css/styles.css">


  <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>
</head>

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

Is there a way to locate the source or URL linked to a button on a form or webpage?

Currently, I am extracting data feeds from a webpage by clicking a button that is similar to the 'Login' button displayed on the following link: However, this button acts as a 'Download' request that initiates the download of a csv rep ...

Why isn't my heading showing up as expected in my document?

Can anyone help me understand why my title appears so low on certain screens? I've previously tried removing the negative margin when the title was hidden, but now it's positioned too far down. Here is the link to my webpage: ...

Pictures are loading fine on a Chrome tab, but are showing a 403 error in the React code

I've been working on a React project that includes images using the img tag. Some of these images are failing to load with a 403 error: https://i.sstatic.net/AmgkF.png Interestingly, these same images load perfectly fine in another tab on Chrome: h ...

The controller is failing to effectively showcase the ng-show functionality

My webpage consists of three elements: a search bar, a search result area, and a form. The use case scenario involves showing the search bar and form div by default, while hiding the search result div. When I enter keywords into the search bar, client data ...

Center elements both vertically and horizontally by utilizing Bootstrap flexbox

Having delved into the documentation of Bootstrap 4 Flex, I am facing difficulties in making the "align-item-center" property function as desired. Despite trying various examples found online, I am unable to alter the vertical alignment of elements. This s ...

Is it necessary to have height:0 and width:0 when display:none is already applied?

Is it necessary to include height:0 and width:0 when applying display:none to a class? I've noticed these properties being used in certain instances, could they potentially resolve any issues with compatibility in older browsers? ...

At what point does IE7 recalculate styles? It seems to have difficulty functioning consistently when a class is applied to the body

Currently facing a peculiar issue. I'm utilizing a class on the element as a toggle switch to control various layout behaviors on my website. When the class is active, specific actions occur; when it's inactive, these actions do not take place. ...

Modifying the CSS to Image Attribute within PHP DOM: HTML Editing

Working with my WYSIWYG editor (Summernote) has brought me so close to being able to send html-encoded emails with images. The only issue is that it uses CSS widths and heights rather than img width and height attributes. <?php $html = '<p>& ...

Insert metadata tag into the head element of the parent iframe

I am looking to insert a meta tag element into the head element of an Iframe parent. I attempted window.parent.$('head').append('sometext'); This method worked when the source file and iframe file were in the same folder, however it d ...

"Access denied: Resteasy and ajax do not support this method (error

Encountering a login problem, although registration is functioning smoothly. Tech stack - HTML/AJAX/JQuery->Resteasy (Wildfly 10.1.0 Final) The URL - https://www.test.com/login.html - (actual URL name altered) Upon submitting the form with method type ...

Verify whether the labels are blank, remain as they are, or transfer the data to the database

Currently, I am utilizing PHP, HTML5, and JavaScript for my project. The task at hand involves creating a webpage that will be connected to a database. However, I have encountered an issue wherein the page proceeds to the next step and sends data even when ...

The .media object in Bootstrap 3 does not experience overflow issues with .dropdown elements

Dropdown menu not displaying fully https://i.sstatic.net/G38cL.png I have a dropdown menu but it seems to cut off halfway through displaying its content. <div class="media-body"> <h4 class="panel-heading" style="margin:0;"> <div ...

Unable to click on anchor tag in Internet Explorer 11

Hey everyone, I've been searching high and low for a solution to this problem with no luck. Below is the specific line of code I'm referencing: '<a id="auto_refinance_loan_documents_save_link" ' + ((scope.styleAsBtn) ? 'class=" ...

Issue with the read more button inoperative for two specific paragraphs

Upon trying to implement 2 or more "Read More" buttons on my website for users to view snippets of content before downloading, I encountered an issue with the functionality only working on the first paragraph and not the subsequent ones. Despite ...

Numerous text boxes sharing identical IDs

Here is the code snippet that I am working with: <%for (int index = 1; index < 7; ++index) {%> <tr> <td> <div class="indicacao_gdp"> ...

choosing a date from the UICalendar

Recently, I've started exploring Angular and I'm trying to incorporate a calendar feature using ui-calendar. So far, I've managed to display a basic calendar with some events on it. Now, my goal is to allow users to click on a specific day ...

Looking to update a component in Vue 3 and Laravel 9 without the need to reload the entire webpage

Looking for a solution to refresh the header component upon clicking the logout button, in order to display the login and register options without refreshing the entire page. Any effective suggestions on how to achieve this are greatly appreciated. The l ...

What is the code in CodeIgniter to retrieve the string 'Sugar & Jaggery, Salt' using <a>?

Can someone please help me? I am a beginner in CodeIgniter and I am having trouble passing a URL with a string. The controller is not accepting the string as expected. How can I fix this issue? //Below is the HTML code for passing a string value index.ph ...

What is the best way to implement a calendar view for selecting time periods on an HTML page?

Is there a way to implement a calendar view when clicking on the input box within an HTML page? I am looking to create a function where users can select a time period to generate a file. How can this be accomplished? If possible, could you share some samp ...

Drop and drag action is preventing the onchange event from being triggered in the form

I have set up a form where users can either 'drag and drop' a file or 'click to upload' it. I want an action to be triggered whenever either of these actions takes place. To achieve this, I am using the onchange event to trigger an act ...