Centering the searchbar in the Bootstrap 4 navbar

I'm finding it difficult to center my search bar inside my navigation. I've experimented with different options like mr-auto and ml-auto or mx-auto, but the element just won't budge. I want to avoid using specific pixel values as it may affect the responsiveness on mobile devices. Here is a simple visualization of where the search bar should be and where it currently is located: https://i.sstatic.net/qDE7d.jpg

Here is the code snippet for my navigation bar:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="nl">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <script src="https://use.fontawesome.com/0ba2bca7b4.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
</head>

<body>
  <nav class="navbar navbar-expand-lg navbar-light bg-light navbar-custom z-depth-half">
    <div class="container">

      <a class="navbar-brand" href="#">
        <i class="fa fa-skype"></i>
      </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" id="navbarSupportedContent">

        <ul class="navbar-nav mr-auto">
          <li class="nav-item">
            <a href="#" class="nav-link">Questions</a>
          </li>
          <li class="nav-item">
            <a href="#" class="nav-link">Providers</a>
          </li>
        </ul>


        <ul class="navbar-nav">
          <li class="nav-item" style="border-right: 0.1rem solid lightgrey">
            <form class="form-inline">
              <input class="form-control mr-sm-2 search-input" type="search" placeholder="Search..." name="search" id="search">
              <button class="btn btn-search my-2 my-sm-0 " type="submit">
                 <i class="fa fa-search"></i>
              </button>
            </form>

          </li>



          <li class="nav-item">
            <a class="nav-link" href="">Sign In</a>
          </li>
          <li class="nav-item">
            <a class="nav-link btn nav-register-btn" href="">Register</a>
          </li>

      
        </ul>
      </div>
    </div>
  </nav>
</body>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
</html>

Appreciate any help in advance!

Answer №1

To adjust your HTML structure, consider moving the form outside of the UL element.

Modified HTML:

...

</ul>

<form class="form-inline">
   <input class="form-control mr-sm-2 search-input" type="search" placeholder="Search..." name="search" id="search">
   <button class="btn btn-search my-2 my-sm-0 " type="submit">
      <i class="fa fa-search"></i>
   </button>
</form>


<ul class="navbar-nav">

...

CSS:

.form-inline{
  width: 100%;
  display: flex;
  justify-content: center;
}

.form-inline #search{
  width: 50%;
}

View JsFiddle example

Answer №2

To solve this issue, you can set a minimum width for your .form-inline class and allow the .search-input to expand using flex-box:

.form-inline {
  min-width: 500px;
}
.search-input {
  flex-grow: 3;
}

Check out the codepen example here

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

When using multiple select tags with *ngFor in Angular, modifying the value of the first select tag will have an

<table id="DataTable" border="1" ALIGN="center"> <tr ALIGN="center"> <th>name</th> <th>address</th> <th>number</th> <th>type</th> </tr> <tr class="tcat" *ngFor ...

Conceal a single column on mobile devices using Bootstrap 4

Take a look at this code snippet: <div class="row"> <div class="col-lg-3"> <div class="ava-block"> </div> </div> <div class="col-lg-6 ...

Set the background-color of each <td> element to be equal to a value in the array, with each group of three elements having the same

I am trying to color every <td> element in a row of three columns with the same color using the following code: for (var i = 0; itr < $("td").length; i++) { $("td").eq(i).css("background-color", Colors[i]); } However, this code colors each i ...

The absence of PHP GET variable being set

I am encountering an issue with my registration php class. It is designed to display a form and upon clicking the registration button, it triggers a function in a login javascript file. This JavaScript file utilizes ajax to send data to an index.php file. ...

Struggling to make the fgetcsv file function seamlessly with my MySQL database

I have been attempting to use the fgetcsv function to import all rows of a CSV file into my database. This functionality should be straightforward, but for some reason it is not working as expected. Below is the code I am using: <?php $file = $_FILES[ ...

How can I modify the appearance of a slider's range?

I'm struggling with customizing the style of a price slider input range. No matter what I do, the changes don't seem to take effect. Can anyone pinpoint where I may be going wrong? Appreciate any guidance on this! Thank you! <div class=" ...

Constantly centered div

I'm dealing with a situation like this: .center_position_div{ width:100%; background-color:green; display: flex; flex-wrap: wrap; } .show_running_exam { width: 22%; background-color:aliceblue; margin-left: 1%; margi ...

Stop the form submission until validation is complete

I'm currently working on a form and encountering some validation issues. HTML: <form id="regForm" class="form-group" method="POST" action="signup.php"> <div class="col-md-12"> <h2>Job Pocket</h2> </div> <di ...

The lengthy email exceeds its limits

https://i.sstatic.net/juD2P.png https://i.sstatic.net/WQo8o.png Take a look at this code snippet: <div className='mt-[3vh] flex h-1/3 min-h-fit w-[80%] min-w-[300px] flex-col content-center items-center justify-center rounded-2xl bg-white shadow ...

Selenium struggles to update dropdown menu choice

Here is the HTML code snippet in isolation: <span style="position: relative; width: 100%; display: inline-block;"> <select id="0ac88542d16d6200fb983d094f655c76_select" class="form-control"> <option value="display_value">Numbe ...

Animating Elements with CSS and HTML

Struggling with my website project, specifically creating a login page. The issue arises when resizing the browser, causing elements to look misaligned and chaotic. Before: view image here After: view image here /* Bordered form */ form { margin-l ...

Is there a way to verify if a checkbox has been checked?

Having trouble with this code: if($('#primayins').prop('checked')) { $("#tx_nm0x0_pricingplan").val(1); } else { $("#tx_nm0x0_pricingplan").val(2); } It keeps returning false consistently ...

"An issue was found on line 75, with column 16 reporting an error stating: AttValue expected either a double or single quote

I can't seem to figure out what's wrong with the first line: <a xlink:href=“#dccomics” xlink:title=“DC Comics”> <rect x="1023.509" y="-612.968" fill="none" stroke="#000000" stroke-width="0.5" stroke-miterlimit="10" width ...

Arranging titles on the top of the page in a column format, resembling an index

Has anyone figured out how to make the title of the content stick at the top, one below the other, as the user scrolls? I've been using Bootstrap's Scrollspy, which works fine, but I need a few additional features. You can check out the codepen l ...

Discovering the art of incorporating various color palettes within a single stylesheet using HTML

I'm curious about incorporating multiple color schemes into a single stylesheet that can be activated by clicking, similar to the functionality found in platforms like WordPress and Magento. These platforms offer themes with various color options avai ...

Saving HTML data into the WordPress database with the help of Ajax

After developing a WordPress plugin, I encountered an issue while working with div elements. My goal is to retrieve the content of a specific div using the following code snippet: var archive = j( "div.page2" ).html(); console.log(archive); Everything wo ...

Importing ReactDOM alone does not result in the rendering of anything

Having just started delving into the world of React, I've been grappling with getting a React app up and running. Despite my efforts, all I can manage to see is a blank page. Can anyone offer some assistance? HTML Markup (index.html) <html> & ...

What is the best way to center-align the label of a TextField in React Material-UI horizontally?

I'm trying to center-align the label in this TextField: import React from "react"; import { withStyles } from "@material-ui/core/styles"; import TextField from "@material-ui/core/TextField"; export ...

Enhance your CouchDB experience by customizing templates to include unique headers and footers

I am currently using CouchDB and CouchApp to host a web application. I have experience with Django and Jinja2, where I can extend templates in two different ways: {% include "header.html" %} or {% extends "base.html" %} <<<---- my preferred me ...

Creating dynamic HTML elements with bound JSON data

When a JSON array list of objects is retrieved from the database, it is sent to the client side upon page load of the current user control. var json = new JavaScriptSerializer(); var jsonList = json.Serialize(mylList); Page.Client ...