Center the search box in responsive mode

I am utilizing a bootstrap navigation bar as shown in this source. My aim is to ensure that on mobile devices, users do not have to click on the menu icon to search on the page. I want to display the search box immediately after the Brand in responsive mode.

In a normal desktop browser, Bootstrap appears like this:

https://i.sstatic.net/dqKlJ.png

However, in responsive mode it looks like this:

https://i.sstatic.net/Du6qM.png

I need to reposition the search box and login option to be centered after the Brand on mobile devices. I attempted adjusting the CSS to move the search box out of the navigation bar, but it ended up affecting my entire HTML structure.

Can someone provide guidance on how to achieve this? Here is the

[JSFiddle Link][4] - https://jsfiddle.net/mzy1sak3/1/

Answer №1

When the toggle button on the navigation bar is clicked in a mobile device, the collapsible menu will appear. The default navigation bar includes a search option that cannot be changed without altering the bootstrap default behavior. However, there is a workaround available. You can hide it by adding an additional class called "hidden-xs" and then insert another div with the class "visible-xs" to place the form specifically for mobile devices.

Below is a sample HTML structure:

<!-- Navbar -->
    <nav class="navbar navbar-default" role="navigation">
        <div class="container-fluid">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>

                <a class="navbar-brand" href="#">Title</a>

                <div class="visible-xs">
                    <form class="navbar-search-sm" role="search">
                        <div class="form-group">
                            <input type="text" class="form-control" placeholder="Search">
                        </div>
                    </form>

                    <ul class="list-inline navbar-links-sm">
                        <li><a href="#">Login</a></li>
                    </ul>

                </div>

            </div>

            <div class="collapse navbar-collapse navbar-ex1-collapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="#">Link</a></li>
                    <li><a href="#">Link</a></li>
                </ul>
                <form class="navbar-form navbar-left hidden-xs" role="search">
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Search">
                    </div>
                </form>
                <ul class="nav navbar-nav navbar-right">
                    <li><a href="#">Link</a></li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown">Dropdown <b class="caret"></b></a>
                        <ul class="dropdown-menu">
                            <li><a href="#">Action</a></li>
                            <li><a href="#">Another action</a></li>
                            <li><a href="#">Something else here</a></li>
                            <li><a href="#">Separated link</a></li>
                        </ul>
                    </li>
                </ul>
            </div><!-- /.navbar-collapse -->
        </div>
    </nav>
    <!-- End Navbar -->

You can also customize the CSS accordingly:

.navbar-search-sm{ display: inline-block; margin-top: 8px; }
.navbar-search-sm .form-group{ width: 300px; margin-bottom: 0; }
.navbar-links-sm{ display: inline-block; margin-left: 15px; }

Answer №2

Here is a different method Utilizing the screen resolution of iPhone 5 and 5S

Enclose the ul input tag and login div within a new div with the id form_login and apply the following style below:

@media screen and (min-width: 320px) and (max-width: 568px) {
  #form_login {
    position: absolute;
    top: 0;
    transform: translate(0, 50%);
  }
  #navbarCollapse {
    padding-top: 120px;
  }
}

View the demonstration on fiddle here (resize window width)

Additional media rules will be necessary for other devices

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 it necessary to bring in CSS for the following page?

Should CSS be imported and used across multiple pages in a web application? Is the page structure outlined below considered acceptable, or is there a standard practice to follow? /pages /home index.js index.module.css /cart ...

What is causing the continuous appearance of null in the console log?

As part of my JavaScript code, I am creating an input element that will be inserted into a div with the id "scripts" in the HTML. Initially, I add a value to this input field using JavaScript, and later I try to retrieve this value in another JavaScript fu ...

What steps can be taken to ensure that the design of this page flows seamlessly?

Interested in a design similar to this https://i.sstatic.net/wbXo2.jpg <!DOCTYPE html> <html lang="en> <head> <meta charset="UTF-8> <title>Title</title> <style> html, body { height: 100%; margin: 0; ...

AngularJS Toggle Directive tutorial: Building a toggle directive in Angular

I'm attempting to achieve a similar effect as demonstrated in this Stack Overflow post, but within the context of AngularJS. The goal is to trigger a 180-degree rotation animation on a button when it's clicked – counterclockwise if active and c ...

Issue with table not remaining inside the div on the content page of WordPress site

I am having trouble with a div on my website. When I include a table in one div it resizes perfectly, but when I do the same in another div using a different template, it doesn't work as expected. To see the issue, please visit: Home page, bottom lef ...

Move your cursor over the image to activate the effect, then hover over it again to make the effect disappear

Looking to enhance my images with hover effects. Currently, all the images are in grayscale and I'd like to change that so that when you hover over an image, it reverts to full color and remains that way until hovered over again. I've noticed so ...

`Why are my overflow-x and flex-wrap not functioning as expected in React JS?`

Having trouble aligning all the movie posters in a single line? It appears that your overflow-x and flex-wrap properties aren't functioning as expected in your App.css. Here's a snippet of your App.css: body { background: #141414; color: ...

What methods can I use to ensure that content is not obstructed by the Android virtual keyboard? Leveraging Bootstrap

I am currently tackling an issue with my 'navbar' layout that looks like this: <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <button class="navbar-toggler" type="button" data-toggle="collaps ...

Using Bootstrap SCSS in ReactJS without applying Bootstrap CSS classes

After successfully installing bootstrap using the following command: npm install <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d9bbb6b6adaaadabb8a999edf7e9f7e9f4b8b5a9b1b8f7ef">[email protected]</a> --save my d ...

Is it better to use multiple canvases or stick to a single canvas?

Can someone explain why some people choose to use multiple canvases instead of just one? Does it offer better performance or is there another reason behind it? I personally haven't noticed much of a difference. ...

Choosing "grandoffspring"

I have a complex inquiry! Let's examine three potential scenarios Situation 1 <div class="entry-content"> <p><a href="#"><img src="#"></a></p> </div> Situation 2 <div class="entry-content"> &l ...

The custom CSS styling is triggered only when the page is resized in Bootstrap

Currently, I am utilizing bootstrap alongside my custom CSS. The issue I am facing is that some of the styling from my custom CSS only takes effect after resizing my browser window to a smaller size. My custom CSS seems to be applied when the website adapt ...

Displaying tooltips with ngx-charts in Angular

Currently, I am working on developing a unique legend component that features individual material progress bars for each data entry. My goal is to display the pie chart tooltip when hovering over any of the entries within this custom legend. Below is a sn ...

What are some ways to enhance the loading time of my PHP-powered website?

Having troubles with my PHP-driven website that showcases real-time stock market data due to slow loading speeds. The site utilizes PHP for scraping financial information from external sources and presenting it on the front end. However, the performance is ...

Issue with Element's Response to JQuery Click Event

After pulling elements from the database in PHP, I utilized Jquery to add Elements to the page. Each button has two classes; one for controlling the GUI and another for handling the click event of that specific button. Here is the code snippet: echo " ...

Email Coding Problem

This email serves as a confirmation from a MailJet subscription widget. The issue at hand is that the body of the email appears too wide in Outlook, but the header is responsive and displays correctly. I have made several attempts such as setting fixed wid ...

The divs contained are misaligned and not properly centered on the page

My container div contains 6 inner divs, but they are not properly centered on the page. Although the divs are equally spaced apart, they seem to be shifted to the left. I tried using 'justify-content: center' without success, and I suspect it mi ...

Customizing nested tables in your HTML email

When creating an HTML email template with nested tables for maximum email client support, the question arises about using inline styling. Should the style always be applied to the lowest level td, or is it possible to apply it to a parent table or td? For ...

The CSS code designed to limit the display to only a two-line sentence is failing to work properly in Internet Explorer

I've implemented the following CSS code to ensure that the display sentence doesn't exceed 2 lines, with the remaining text being replaced by "...". .bell-notification-max-words-display { overflow: hidden; display: -webkit-box; ...

Using Angular 2 alongside Twitter Bootstrap

What is the recommended approach for integrating the Twitter Bootstrap library with Angular 2 framework? There are typically two main methods: Begin by structuring the HTML using Bootstrap containers and then embed Angular components within these contain ...