How to extend the Bootstrap 4 navbar search box to fill the entire available space

I am currently designing a navigation bar using Bootstrap 4, with the following elements arranged from left to right:

  1. The Logo (brand)
  2. A search bar
  3. The website's navigation

My aim is to have the search box stretch across the entire space available between the logo and navigation. Here is my code snippet so far:

.navbar {
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.25);
  background: #191919;
}
.navbar-inverse .navbar-nav a {
  text-transform: uppercase;
}
.navbar-inverse .navbar-nav .nav-link {
  color: #fff;
}
.navbar-inverse .navbar-nav .dropdown-menu {
  padding: 0;
  border-radius: 0;
  top: 47px;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.12);
}
.navbar-inverse .navbar-nav .dropdown-menu a {
  padding: 11px 15px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.12);
  color: #757575;
}
.navbar-inverse .navbar-nav .dropdown-menu a:hover {
  color: #555;
}
.navbar-inverse .navbar-nav .dropdown-menu a:last-child {
  border-bottom: none;
}

.navbar-inverse .form-inline {
  display: block;
  width: auto;
}
.navbar-inverse .form-inline .bg-white {
  background: #fff;
  border: none;
  padding-top: 0;
  padding-bottom: 0;
  margin-top: 1px;
  height: 37px;
}
.navbar-inverse .btn-outline-secondary {
  border: 1px solid #fff;
  color: #fff;
}
.navbar-inverse .btn-outline-secondary:hover {
  background: #fff;
  color: #0275D8;
}
.container {
  margin-top: 56px;
}
<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">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


<nav class="navbar fixed-top navbar-toggleable-sm navbar-inverse bg-primary">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNav">
    <form class="form-inline mx-auto">
      <div class="input-group">
        <input class="form-control border-right-0" placeholder="Search here...">
        <span class="input-group-addon bg-white border-left-0"><i class="fa fa-search"></i></span>
      </div>
    </form>
    <ul class="navbar-nav ml-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><i class="fa fa-folder-open" aria-hidden="true"></i>
 Categories</a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Sports</a>
          <a class="dropdown-item" href="#">Hot</a>
          <a class="dropdown-item" href="#">Mad Science</a>
        </div>
      </li>
      <li>
        <a href="#" class="btn btn-outline-secondary"><i class="fa fa-user"></i> My account</a>
      </li>
    </ul>
  </div>
</nav>
<div class="container">
  <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Doloribus mollitia, minima aliquam soluta ducimus sint obcaecati rerum inventore, ipsa nobis dignissimos, reiciendis, numquam cum sit cumque suscipit voluptatibus a est?</p>
</div>

Upon testing on wide screens, it appears that the search box is too small. I would like its left side to be positioned 15px away from the logo, while its right side should be situated 15px away from the navigation. What adjustments do I need to make?

Answer №1

It appears that you were utilizing Bootstrap 4 alpha, which is now considered completely outdated.

Additionally, if you desire the form input to occupy the entire width, avoid using the form-inline class. This particular class is specifically designed to restrict form inputs from spanning the full width.

Below is a functional code snippet that achieves your desired outcome (colors may need adjusting):

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">

<nav class="navbar navbar-dark sticky-top bg-dark flex-wrap2 flex-md-nowrap p-0">
<a class="navbar-brand col-auto mr-0" href="#">Brand</a>

<button class="navbar-toggler d-md-none mt-1 mr-2" 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="input-group py-1 px-2 px-md-0">
<input class="form-control form-control-dark" type="text" placeholder="Search here..." aria-label="Search">
<div class="input-group-append">
<button class="btn btn-outline-success" type="submit"><i class="fa fa-search"></i></button>
</div>
</div>

<ul class="navbar-nav navbar-expand-md pl-2 pr-3">
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto text-nowrap">
<li class="nav-item my-1 active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item dropdown my-1">
<a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<i class="fa fa-folder-open" aria-hidden="true"></i>
Categories
</a>
<div class="dropdown-menu" aria-labelledby="navbarDropdown">
<a class="dropdown-item" href="#">Sports</a>
<a class="dropdown-item" href="#">Hot</a>
<div class="dropdown-divider"></div>
<a class="dropdown-item" href="#">Mad Science</a>
</div>
</li>
<li class="nav-item my-1">
<a href="#" class="nav-link btn btn-outline-secondary"><i class="fa fa-user"></i> My account</a>
</li>
</ul>
</div>
</ul>
</nav>

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

To enclose everything in a container or container-fluid, simply uncomment the corresponding div.

Note the utilization of responsive padding and margin classes for controlling spacing. For instance, my-1 imparts vertical margin of 1 unit to the navbar items, while px-2 px-md-0 offers horizontal padding of 2 units to the input-group on small screens and eliminates horizontal padding on medium (md) screens and above.

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

Tips for making the text input smaller in a datepicker

https://i.sstatic.net/fuECu.png Is there a way to reduce the width of a datepicker while using Bootstrap? It's taking up too much space in my browser window. Below is the code snippet I am using for this functionality. <!---DatePicker ----> ...

Changing datepicker into a datetimepicker

I have successfully implemented a datepicker in my website using Bootstrap 4. However, I now need to incorporate time selection as well. Despite trying various datetime picker options and ensuring proper CDN file placement, I am facing conflicts that preve ...

What is the best way to incorporate a dynamic background image into a panel heading using Twitter Bootstrap 3 for a responsive design?

Hey everyone! I'm having trouble adding an image as a background to my panel heading and also including an h3 inside it. Below is the code I am using: <div class="row col-wrap"> <div class="col-lg-4 col"> < ...

What is causing the overflow issue with my <ul> element in Bootstrap 5?

My interface contains a <div id=content> element with a large number of elements. When the list extends beyond the height of the screen, a scrollbar should appear within the list. The element is set to fill the remaining height space in the right col ...

The CSS drop-down menu on the website is not operating as intended

Whenever I attempt to use the menus by hovering over them, the sub menu briefly appears but disappears before I can access it fully. I've experimented with adding margins and adjusting the z-index in different locations, yet I am still unable to pinpo ...

Stop elements from overextending within their parent container

I am facing an issue with two divs that are wrapped inside a container div. The bottom div contains a dynamically filled table, which determines the overall width of all the divs. On the top div, I want to display several small red blocks that need to tak ...

The hover feature on my website is making the picture flicker

I am experiencing an issue with a map on my website that contains four colored squares. When I hover over one of the squares, the image of the map changes to show the route corresponding to that color. However, this causes the image to shift position and i ...

Importing D3 data from CSV files using the "%" symbol

I am trying to import a CSV file with the following data: Month, Ratio January, 0.19% February, 0.19% March, 0.19% April, 0.18% The current code snippet I'm using is as follows: d3.csv("month_ct.csv", function(d) { return { month: d ...

The inclusion of the <div> tag disrupts the functionality of the Material UI Grid

While attempting to enclose my element within a <div> that is nested inside the <Grid>, I realized that this was causing the <Grid> layout to break! Surprisingly, when I tried the same approach with Bootstrap grid system, this issue did n ...

Avoiding a line break between two <form> tags is essential for maintaining the structure of your webpage

I am looking for a way to ensure that there are no line breaks between two forms that I have on my website. Here is the code snippet: <form action="..."> <input type="submit" /> </form> LINE BREAK HERE <form action="..."> <inpu ...

Customize specific styles for individual divs one at a time (without the use of jQuery)

Can you assist me with this issue? I am trying to display the <span class="badge badge-light"><i class="fa fa-check-circle"></i></span> tag (initially set to "visibility: hidden") when clicking on a div with the class "role-box". He ...

Ways to display a loading indicator while an AJAX request is being sent

I have an AJAX function that retrieves data. How can I display a Bootstrap progress loader (circle loader) while waiting for the response? Here is the AJAX Function JS: $('#category_modal').click(function(event) { var url = $(this).data(&a ...

Fuzzy division following a CSS transformation/animation

I have been working on a content slider, using the guidelines from this demonstration However, I have encountered an issue where my content, which consists of a few divs, appears slightly blurry after the CSS transition completes. This problem only seems ...

GZIP compression causes a total page layout malfunction

Thank you for taking the time to visit. I've been attempting to tackle this issue on my own, but it seems to be a bit overwhelming for me once again. THE SCENARIO... I have my website live on a shared hosting platform. As I delved into compressing my ...

Video background is malfunctioning on Firefox and Internet Explorer

Currently, I am facing some issues with the JQuery plugin 'videoBG'. The problem is that when I view the video offline, it works perfectly in all browsers. However, once I go online, the video stops working in FireFox and IE. It seems like the sc ...

Is there a way in Angular Material to consistently display both the step values and a personalized description for each step?

Is there a way to display both numerical step values and corresponding custom text in Angular Material? I prefer having the number at the top and the descriptive text at the bottom. Check out this image for reference: https://i.stack.imgur.com/LGMIO.png ...

Is there a way to manually change the field color upon approval in AngularJS?

Within my application that utilizes the MEAN stack, I am using AngularJS as the front-end technology. How can I manually change the color representation of a tolerance value to 159.06 in a table? Check out my Plunker for more details. Please refer to m ...

Colorful shifting text effect

Looking for a cool menu hover animation for my website project. Thinking about sliding the text color from left to right... Starting color: White Ending color: Black Duration: 0.5 seconds The goal is to animate only the text color, not the background co ...

Another ajax function implemented for a different form is experiencing technical issues

I have two forms with different IDs (form_sign and form_login) and I am using AJAX to submit them. The form_sign is working fine, but the other form is not functioning properly. When I tried to display an alert after submitting the form_login function, it ...

Transform JavaScript code into HTML using jQuery.`

I have a piece of HTML and JavaScript code. The JavaScript code is currently in jQuery, but I want to convert it to vanilla JavaScript: // Vanilla JavaScript code document.querySelectorAll('.hello[type="checkbox"]').forEach(item => { item ...