Bigger Search Form with Drop-down in Navigation Bar

Websites that use Bootstrap 4 framework, sometimes need a bigger search form in the navigation bar. These websites might also need a drop-down or select input to provide better search. In such case, the fixed top navigation bar consists of a bigger search form along with the logo and menus.

Previously, we have done the same for Bootstrap version 3 as explained in this linked article. However, version 4 is the successor and more powerful than the previous one. Obviously, those techniques won’t work in Bootstrap 4 for a bigger search form with a drop-down in the navigation bar.

Bigger Search Form with Drop-down in Navigation Bar

Navbar HTML for Bigger Search Form with Drop-down

First of all, we are writing the HTML markup to create the navbar. Here, Bootstrap version 4.1 is in use. Place the drop-down to the left of the search input. Correspondingly, put the button to the right. We’re using Bootstrap 4 ‘Input group’ addon for this purpose.  

We have moved the form outside the collapse part and after the toggle button. Additionally, we have used two classes, navbar-form to the form itself and input-group-search to its first direct child. We will use these classes later to make the search form bigger and responsive in the navigation bar.

Also, this bigger search form with drop-down forces other menu items to wrap on the next line at white-spaces. We’re adding text-nowrap class to anchor elements inside the collapsible div to overcome this issue. That class is a utility class available in Bootstrap 4.

Bootstrap 4 CSS for Search Form in Navigation Bar

We have used a couple of CSS rules to let the search form occupy remaining space in the navigation bar. Use the rules below in the stylesheet file created for your Bootstrap 4 project.

First of all, note that the unordered list of menus in the collapsible div has no margin or padding. Second, we expended the form till full available width in the navbar. The flex-basis sub-property of CSS3 Flexbox did this job for us.

Further, we did the same with the search container, consisting the drop-down, the search input and the button. This search container has the class input-group-search. By default, this search input group or container expands to 100% of its parent. In this case, there will be the full-width bigger search form in a new line below the brand and toggle button.

However, that rule applies till the device width is less than the breakpoint specified. As soon as the specified breakpoint reaches, the respective media query takes precedence. Correspondingly, the applicable CSS rule limits the width of the search box.

Further, the search input group has Bootstrap 4 mx-auto class applied as well. This utility class applies equal margins to the left and the right of the element. Thus, it keeps the search group in the horizontal centre of its parent. As per the flex-basic CSS rule, the form will always have the full width, minus occupied by the rest of the components.

Bigger Search Form – Grow/Shrink Width

Furthermore, you can grow or shrink the width of the search box till the form width in the navigation bar. Since it really depends on the number of menu items in the navbar. Play with the percentage width in the media queries if you want to either grow or shrink it.

Also, don’t forget to replace verticle margin class (my-lg-0) in the form with respect to the breakpoint class added in the nav element (navbar-expand-lg).  For example, if you use the navbar-expand-md class then the form should have my-md-0 class. This will keep a decent margin between the search form and the menu in the collapsed state.

Prevent Overlapping of Fixed Top Navigation Bar

If the navbar is fixed to the top then it requires to prevent overlap with other elements. You can read more here about this problem. We have added top padding to the body at two places. First is when the navigation bar is in full width and another is in collapsed status.

There is the bigger search form with a drop-down which shifts below in collapsed condition. Hence, we need to double the top padding at this breakpoint. We’ve commented out this rule at the -lg breakpoint. you can uncomment that as well as shift inside another breakpoint.

Bigger Search Form with Drop-down – Changing Selected Text

The drop-down menu at the left of the search input doesn’t change the text automatically upon selection. So you need a little jQuery to let is work as expected.

That’s all. Place this jQuery script in your JS file or in line. Preferably after necessary JS files as per Bootstrap 4 documentation. Finally, the best way to implement this solution is to copy the HTML and CSS and modify as per the project needs later.

Hope this solution gave you a beautiful, better and bigger search form with a drop-down in the Bootstrap 4 navigation bar. Don’t forget to update us with your feedback regarding the implementation.

You Might Interested In


Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.