The issue with the mobile display of the search navigation bar in Bootstrap 4

On PC, the search navigation bar with links appears to the right of the search bar. However, this layout is not working properly on Android or iPhone devices.

I've tried solutions from different sources like using .navbar-header and enclosing the links within the .collapse class, but it didn't produce the desired result. Instead, everything shifted onto a new line, causing the search bar to span across the screen and not fit into the page properly. The full explanation can be found at: .

            <th:block sec:authorize="isAuthenticated()">
            <form th:action="@{/logout}" method="POST" th:hidden="true" name="logoutForm">
                <input type="submit" value="logout">
            </form>
            </th:block>
            <nav class="navbar navbar-expand-sm bg-light">
             <div class="navbar-header">
                <div class="collapse navbar-collapse" id="searchNavbar">
                        </div>
                    <form class="form-inline my-2 my-lg-0" th:action="@{/search}" method="GET">
                        <input type="search" id="item-search" name="keyword" size="50" th:value="${keyword}" class"form-control mr-sm-2"
                            placeholder="What are you looking for " required />
                            &nbsp;
                        <input type="submit" value="Search" class="btn btn-outline-success my-2 my-sm-0"/>
                    </form>
                    <ul class="navbar-nav">
                        <th:block sec:authorize="isAuthenticated()">
                        <li class="nav-item">
                            <a class="nav-link" th:href="@{/customer}"> <b>[[${#request.userPrincipal.name}]]</b></a>
                        </li>
                        </th:block>
                        <li class="nav-item">
                            <a class="nav-link" id="cart-link" th:href="@{/cart}">Cart</a>
                        </li>
                        
                        <th:block sec:authorize="isAuthenticated()">
                        <li class="nav-item">
                            <a class="nav-link" href="javascript: document.logoutForm.submit()">Logout</a>
                        </li> 
                        </th:block>
                    </ul>
                </div>
             </div>
            </nav>
        </div>

While the toggle suggestion seems to function correctly, the issue persists with the search bar extending beyond the div, resulting in scrollability. For reference, please view the attached photo here: https://i.sstatic.net/xzZ0G.png

Is there a way to make the search bar collapsible while ensuring that the links appear above it without requiring selection of the navbar-toggle button? On mobile, the idea is for the links to show above the search bar, and on desktop, below the black line as depicted in the provided image, positioned before the "Toggle" button.

Answer №1

Your question lacks clarity regarding your end goal, but it seems that you need to address the fact that content within .collapse.navbar-collapse will be hidden on mobile devices unless there is a toggling mechanism in place. I have included a button with the text "Toggle" and a

data-target="#searchNavbar"
to enable toggling functionality on mobile.

While the current setup may not look visually appealing, you can enhance the design by adding appropriate layout classes.

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.bundle.min.js" integrity="sha384-LtrjvnR4Twt/qOuYxE721u19sVFLVSA4hf/rRt6PrZTmiPltdZcI7q7PXQBYTKyf" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">


<th:block sec:authorize="isAuthenticated()">
  <form th:action="@{/logout}" method="POST" th:hidden="true" name="logoutForm">
    <input type="submit" value="logout">
  </form>
</th:block>
<nav class="navbar navbar-expand-sm bg-light">
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#searchNavbar" aria-controls="searchNavbar" aria-expanded="false" aria-label="Toggle navigation">Toggle
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="navbar-header">
    <form class="form-inline my-2 my-lg-0" th:action="@{/search}" method="GET">
      <input type="search" id="item-search" name="keyword" size="50" th:value="${keyword}" class "form-control mr-sm-2" placeholder="What are you looking for " required /> &nbsp;
      <input type="submit" value="Search" class="btn btn-outline-success my-2 my-sm-0" />
    </form>
  </div>
  <div class="collapse navbar-collapse" id="searchNavbar">
    <ul class="navbar-nav">
      <th:block sec:authorize="isAuthenticated()">
        <li class="nav-item">
          <a class="nav-link" th:href="@{/customer}"> <b>[[${#request.userPrincipal.name}]]</b></a>
        </li>
      </th:block>
      <li class="nav-item">
        <a class="nav-link" id="cart-link" th:href="@{/cart}">Cart</a>
      </li>

      <th:block sec:authorize="isAuthenticated()">
        <li class="nav-item">
          <a class="nav-link" href="javascript: document.logoutForm.submit()">Logout</a>
        </li>
      </th:block>
    </ul>
  </div>
</nav>

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

Generate a HTML image that is centred using mpld3

I've been experimenting with the mpld3 Python library to convert static matplotlib figures into interactive images in HTML format. Here's a simple example: # create a static matplotlib figure import matplotlib.pyplot as plt import numpy as np fi ...

HTML5, canvas covering every element

Below is the code that I am working with: <html> <head> <title>canvas</title> </head> <body> <canvas width="1745" height="569" style="width: 1730px; height: 1680px; position: absolute; z-index: 1"></canvas> ...

I'm having trouble getting my footer to stay at the bottom of the page

Struggling to get my footer to stay at the bottom on my blog pages, no matter what I try: Every time the social media section appears, the footer slides up... Can't figure out why... Appreciate any assistance! ...

What is the best way to eliminate the gap between spans in Bootstrap?

Imagine you have the following code: <div class="row-fluid"> <div class="span6"></div><!--span6 END--> <div class="span6"></div><!--span6 END--> </div><!--row END--> Now envision two red b ...

Unsure why the React button is disappearing specifically on Safari iOS. Any thoughts on how CSS

Hey there! I created a small hamburger button for my website, but for some reason, it doesn't show up on Safari iOS. Interestingly, it works perfectly fine on Windows Chrome, Windows Firefox, and my Android device. I've been trying to troubleshoo ...

React - A guide on accessing the scroll position of a div using JavaScript

Within my side navigation bar, there is a title and other information. The title is fixed in place (sticky) and the sidebar has a height of 100vh. I am looking to add a box-shadow effect to the title div (sticky-title) only when the user scrolls down past ...

During the loading process, CSS display inline items may momentarily appear as list-items on the page

I am experiencing an issue with my Carousel (jcarousel) displaying items inline. It seems that when the carousel loads, the items first appear vertically in a list format before switching to their normal inline position. This is happening on multiple webs ...

Display the input text to explore and filter through selected options

Imagine having a select tag with numerous options, making it difficult to search through them all. So, when you click on the select and the options appear, you can type a letter and the select will jump to the option starting with that letter. For instance ...

Which is quicker, generating a gradient using CSS or directly loading an image?

I am curious about the potential benefits of the new CSS3 styling techniques. However, I am unsure if the effort is worth it! (Question: Can jQuery be used to apply a vignetting effect to a webpage?) Appreciate any insights! ...

Obtain the contenteditable span's value

I'm facing an issue with a content editable span - I want to extract its value when the post button is clicked, but it's not working as expected. Please note that I am unable to convert the span to a textbox or div, I specifically need the value ...

I am interested in extracting information from a website by utilizing PHP

Can data be scrapped from a website using PHP without the use of any external tools? I attempted with the following code, but it proved to be insufficient: <?php $url = 'http://www.example.com'; $output = file_get_contents($url); echo $output ...

What is the reason behind certain vanilla JavaScript libraries necessitating NPM?

I am a beginner in JavaScript and I want to learn more about it. Recently, I discovered a library called bounce.js which is an animation library. However, it requires NPM for installation, but I am hesitant to use NPM or any packet Manager because they hav ...

Remove old photo when uploading new one in Django

I'm currently working on a user profile feature that allows users to upload profile pictures. The uploading process is functioning correctly, but I'm encountering an issue where the old photo is not replaced when a new one is uploaded. I'm u ...

Instructions on sending search fields by pressing the enter key

Developing my application in React.tsx, I have a menu window that consists of numerous div elements with input fields, checkboxes, and select elements. Upon clicking the submit button, a list of results with user-selected filters appears. As an additional ...

Letters are frolicking in varying sizes and designs

Currently, I am encountering a strange issue with the text on my webpage. Some fonts are displaying with completely different shapes and sizes despite using <meta charset="UTF-8"> to ensure font consistency. Unfortunately, this did not resolve the pr ...

Requirements for ajax submission

I am encountering an issue with my HTML form and AJAX script. The script sends the form without refreshing the page, and I have a check in place to ensure that two identical post dates do not get into the database. However, despite this check, when I click ...

Produce an additional page while remaining on the present one

I have a PHP-based invoice system that displays all customer invoices. I am interested in adding a feature that allows users to print an invoice directly from the list page without navigating away. While I am familiar with window.print() and media="print" ...

How to center multiple divs in a row using HTML and CSS

I've been attempting to achieve a pretty basic task, but unfortunately my efforts have fallen short thus far. I have an image and a title that need to be centered within their parent div element. However, I'm struggling to find the proper way to ...

Is your Facebook send button appearing strangely? Find out how to fix it here!

I recently integrated the Facebook send button on my website, allowing users to easily share information about each drive listed. However, a new issue has arisen where clicking on the send button opens a popup inside a table, negatively affecting the page& ...

What is the best way to retrieve the values from the labels for two separate buttons?

I designed a pair of buttons with a single label below. Both buttons acted as standalone entities. <label for="buttons" class ="val">0</label> <input class="btn btn-primary button1" type="button" ...