The Bootstrap 4 navbar remains consistently visible on mobile devices

I am experiencing an issue with my bootstrap navbar on mobile devices. It remains visible even when the navbar-toggler is collapsed.

Below is the code snippet:

<nav class="navbar navbar-expand-lg navbar-light bg-light">
 <div class="container">
  <a class="navbar-brand" href="/"><i class="fas fa-blog"></i>  Tech Info</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
   <span class="navbar-toggler-icon"></span>
  </button>
  <div class="d-flex flex-row-reverse collapse navbar-collapse" id="navbarSupportedContent1">
   <ul class="nav justify-content-end">
     <% if(!user) { %>
      <li class="nav-item">
       <a class="nav-link my-2 my-sm-0" href="/login">Sing In</a>
      </li>
      <li class="nav-item">
       <a class="nav-link btn btn-outline-primary my-2 my-sm-0" href="/register">Sign Up</a>
      </li>
     <% } %>
   </ul>
  </div>
 </div>
</nav>

Answer №1

The d-flex class is taking precedence over the navbar-collapse behavior. To change the flex direction back, apply it to the nav element instead...

<nav class="navbar navbar-expand-lg navbar-light bg-light">
    <div class="container">
        <a class="navbar-brand" href="/"><i class="fas fa-blog"></i> Tech Info</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent1">
            <ul class="nav justify-content-end flex-row-reverse">
                <li class="nav-item">
                    <a class="nav-link my-2 my-sm-0" href="/login">Sing In</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

Check out the demo here:

Answer №2

Ensure that the target of the button matches the ID of the div you want to collapse, and make sure to remove the d-flex class from the div.navbar-collapse element.

<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1"
  aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
  ...
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
  ...
</div>

Answer №3

It's best practice for the navbar to not be contained within a general container. Please consider removing the "container" class from the 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

What is the best way to incorporate scripts into the HTML of my Google Apps Script or Google Sheets?

I'm having trouble getting my Apps Script-based HTML to include any scripts. My doGet function for the HtmlService is working fine: function doGet() { return HtmlService.createHtmlOutputFromFile('myhtmlfilename'); } Regardless of whether ...

A sleek and streamlined scrollspy that dynamically updates the active class exclusively for anchor tags

Can anyone help me with this coding problem? I'm currently working on a minimalistic scrollspy code that adds an active class when the content is offset.top. The code works fine, but I want to change the active class to apply to the "a" tag instead of ...

What is the best method for converting a list tag into an array of objects with XPath?

I am attempting to extract the ordered list and generate an array of list tags along with their content. I have experimented with different paths, such as: //li[div/@class="business-info"] //li[div[@class="business-info"]] //li[descendant::div[@class="bu ...

Prevent parent element from triggering double click when double clicking on children element with CSS or jQuery

Check out my project demo here Whenever I double click on a child element, it also triggers the parent element at the same time. I want to prevent this behavior so that only the child element is affected by the double click event. Can anyone provide assis ...

How come my MySQL date is decreasing by one day when using JavaScript?

My todo list is stored in a MySQL database with columns for todoTitle and todoDate. However, when I display the todoDate on my website, it shows the date decremented by one day. For example, if the date in the database is 2016-12-20, it will show as 2016-1 ...

Refreshing Div Element with PHP AJAX POST请求

It's me again. I'm feeling really frustrated with this code, it's starting to get on my nerves. I don't mean to keep bothering you, but I finally figured out where the issue was in the code and now I just need a little help with the fin ...

What is the best way to display an element once an input field with type "date" is populated with a value

Struggling to create a dynamic form that reveals additional fields when a date picker is used? I've searched for solutions, but everything I find involves complex validations and logic that exceed my needs. I've had some success with revealing e ...

Send multiple form submissions using JQuery Ajax without needing to refresh the page

I am facing an issue with my form submission using Ajax. After receiving a response in the form of another form, when I try to submit it again using the submit button, it doesn't go through Ajax and uses the normal method instead. Why is this happenin ...

Ensure that a v-card in Vuetify/Vue 2 is consistently square in shape, yet capable of adjusting its size dynamically

I am facing an issue where I am trying to make sure a v-card always maintains a square shape (or any other aspect ratio set), even if it means there is scrolling or cut-off text/components inside the card. For example, I want this v-card with lorem ipsum ...

Dynamic CSS containers that adjust according to browser and screen dimensions

01) In my attempt to create a row with two content boxes, I encountered an issue. The first box is supposed to display an image and the second box text. However, I am struggling to make it responsive and interactive based on different browser and screen si ...

Modify the layout of columns in Bootstrap dynamically without compromising on responsiveness

On my webpage, I have a section where I display cards of specific sizes (250*250). I want to show a maximum of 4 cards per row, stacking them on smaller viewports. Currently, the layout works fine with 4 or more cards, with the excess automatically moving ...

Design a table featuring button groups using Bootstrap styling

I'm currently using Bootstrap 4 and facing some issues while attempting to create a button group with multiple rows, similar to the design shown in the image provided below. The default button groups in Bootstrap appear to only support arranging butto ...

Receiving an absence of string or a null value from a text box

My goal is to test the functionality of an <input> element by entering text into a textbox and then displaying that data in the console. However, I am encountering issues with retrieving and printing the content. Additionally, I am interested in test ...

sending live video from a Python server to a browser-based client

Is there a way to stream a video from a python server to an HTML page web client (Node.js)? In my Node.js web project, I am currently using Python to write frames and JavaScript to read them. However, I am facing limitations with both frame rate and resol ...

Troubleshooting issue with CSS `gap` property malfunctioning in conjunction with `display: block;` setting

I've run into an issue with the gap property in CSS not working as expected in my layout. After investigating, I found that some of my containers have display: block; set, which seems to be causing the gap property to be ineffective. My goal is to cre ...

The never-ending loading problem at the bottom of the Firefox screen seems

I am facing an issue while trying to open something in Firefox using window.open(). The loading screen seems to never stop, and I cannot pinpoint the reason behind it. Any advice on how to resolve this would be greatly appreciated. View Screenshot: This ...

Modifying selections within a select box generated dynamically using JQuery

Looking for help on how to delegate to a static DOM element in my current situation. I need to create a dynamic select box .userDrop when .addNew is clicked, and then have the user select an option from #secDrop, triggering a change event that calls the da ...

Implementing a custom loader screen in jQuery Mobile instead of displaying an error page while loading

I am in the process of developing a hybrid mobile application using Intel XDK and jQuery Mobile for the user interface. One challenge I encountered is related to an ajax form submission, which involves two logins and the use of tabs (2). Initially, I focus ...

What could be causing the CSS border to not show up on the webpage?

I am trying to add a border to the footer, but it seems like something is blocking it from showing properly. Below are the CSS lines I have used: position: fixed; bottom: 0; right: 0; left: 0; clear: both; line-height: 1.36em; padding: 08px 0px 0px; tex ...

Creating a clickable image in the header of an R Shiny app

I've been attempting to create a hyperlink from the image output, but I'm encountering difficulties despite reviewing similar questions on Stack Overflow. svg with clickable links in shiny - not clickable The tags are not working. server.r li ...