TechQA.

How to place the search form in the middle of navbar and expand it in Bootstrap 5

974 views Asked by Peter Amo At 2021-10-26T07:47:57+00:00 26 October 2021 at 07:47 2025-10-17T11:49:35+00:00

I'm using Bootstrap v5.1.3 and I have added a navbar like this:

navbar

And here is the code for that:

        <div class="container">
            <nav class="navbar navbar-expand-lg navbar-light bg-light">
                <div class="container-fluid">
                    <a class="navbar-brand" href="#">Navbar</a>
                    <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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">
                        <form class="d-flex">
                            <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                            <button class="btn btn-outline-success" type="submit">Search</button>
                        </form>
                        <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                            <li class="nav-item dropdown">
                                <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Cart</a>
                                <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                    <li><a class="dropdown-item" href="#">Action</a></li>
                                    <li><a class="dropdown-item" href="#">Another action</a></li>
                                    <li><hr class="dropdown-divider" /></li>
                                    <li><a class="dropdown-item" href="#">Something else here</a></li>
                                </ul>
                            </li>
                        </ul>
                    </div>
                </div>
            </nav>
        </div>

But I need to exapnd the search form and place it at the center of navbar, so it would be looked like this:

expected search form example

So I tried adding id of to the form:

<form class="d-flex" id="search">

And added this:

#search{
   text-align:center;
}

But it does not still work out and shows the search form at the left.

So how to place this search form in the middle of navbar and expand it also a little bit in Bootstrap 5 ?

css twitter-bootstrap navbar bootstrap-5 search-form
Original Q&A
1

There are 1 answers

0
Carol Skelly Carol Skelly On 2021-10-26T11:03:58+00:00 26 October 2021 at 11:03 BEST ANSWER

Use flex-grow-1 to make it expand...

<div class="container">
    <nav class="navbar navbar-expand-lg navbar-light bg-light">
        <div class="container-fluid">
            <a class="navbar-brand" href="#">Navbar</a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-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">
                <form class="d-flex flex-grow-1">
                    <input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
                    <button class="btn btn-outline-success" type="submit">Search</button>
                </form>
                <ul class="navbar-nav ms-auto mb-2 mb-lg-0">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" id="navbarDropdown" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">Cart</a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                            <li><a class="dropdown-item" href="#">Action</a></li>
                            <li><a class="dropdown-item" href="#">Another action</a></li>
                            <li>
                                <hr class="dropdown-divider" />
                            </li>
                            <li><a class="dropdown-item" href="#">Something else here</a></li>
                        </ul>
                    </li>
                </ul>
            </div>
        </div>
    </nav>
</div>

https://codeply.com/p/zL7fRRq6gK

Related Questions in CSS

  • How to use custom font during html to pdf conversion?
  • Storing the preferred font-size in localStorage
  • mp4 embedded videos within github pages website not loading
  • Is there any way to glow this bulb image like a real light bulb
  • What can I do to improve my coding on both html and css
  • Uncaught TypeError: google.maps.LatLng is not a constructor at init (script.js:7:13)
  • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
  • How to increase quality of mathjax output?
  • Hover animation resetting( seemingly reverting back to original CSS and then again to hover)when moving mouse horizontaly accross a part of an element
  • Storing selected language in localStorage
  • How to clip grid cell and provide scroll as well?
  • KeyboardAvoidingView makes a messy the flexbox
  • Rotate an object around another object in javascript
  • Understanding Scroll Anchoring Behavoir
  • how to use only block layout in this css code?

Related Questions in TWITTER-BOOTSTRAP

  • General questions about creating a custom theme Moodle CMS
  • How to fix - The modal dissapear below 992px.?
  • Bootstrap horizontal gutters issue
  • How can I add a list of icons to the right of a text area
  • Why isn't my Bootstrap background color class working as expected?
  • Edit button in each row of table using same Bootstrap modal as add button
  • Change Opacity of Bootstrap Button
  • bootstrap 5 multiple tab dropdown with tab pane
  • Bootstrap v5.3 navbar drop-downs not working. Why?
  • Should i learn Bootstrap in 2024
  • How to turn on Bootstrap Intellisense in .NET 8 Blazor
  • Bootstrap 5.3 burger menu not responsive in Underscore WordPress template
  • Bootstrap 5 columns not displaying inline
  • Bootstrap scaffolding not showing good on mobile
  • How to Prevent Select2 Dropdown from Moving with Scroll inside a Bootstrap Vue Modal?

Related Questions in NAVBAR

  • Dropdown menu in a fixed navbar
  • How to write HTML CSS code for a header navbar that too responsive
  • navbar not appear in the result?
  • Bootstrap cards
  • Bootstrap 5.3 burger menu not responsive in Underscore WordPress template
  • microsoft from 2.0 controler textbox to retrieve the hindi content in vba
  • How do I make navigation bar buttons show and hide divs/sections on click without javascript or JQuery?
  • i want codepen to angular component for this template
  • Trying to create a responsive nav bar for smaller device, but after clicking the button everything disappears
  • Wordpress navbar and slider
  • Change highlight colour on scroll in React js
  • Image not displaying under Nav bar
  • CSS Styling is applied to the submenu border on live website but why it is not working
  • Blazor Navigation Menu - add picture and a Text Line
  • Weird behaviour of Google Chrome's responsive website checker

Related Questions in BOOTSTRAP-5

  • Bootstrap modal not showing at the desired position on a web page when the screen size is smaller
  • automatically compare prices with ready-made codes
  • Bootstrap card not collapsing with other cards and merging with cards
  • Bootstrap component does not want to render in Datatables function
  • Bootstrap 5 tooltips not working in Laravel 9.x application
  • How to fix - The modal dissapear below 992px.?
  • Bootstrap horizontal gutters issue
  • What could be the cause of the Bootstrap menu not working?
  • Edit button in each row of table using same Bootstrap modal as add button
  • Vite Serving Libraries without CDN
  • Why can't i click the "register-selection" in bootstrap and JS?
  • .NET MVC Bootstrap Modal not showing up
  • Are there any ways to add Bootstrap Carousel in CKEditor (django-ckeditor)
  • Bootstrap v5.3 navbar drop-downs not working. Why?
  • Inline menu items

Related Questions in SEARCH-FORM

  • How would I go about using a string value, such as '100pF 50vdc' and query multiple different fields as part of a search?
  • search form in new custom page template not working
  • Looking for a better approach to keeping long parameter queries organized in MS Access
  • Hide fields from SearchForm depending on the request user groups
  • Wordpress not validating page when get_search_form() is used more than once
  • If you can't place the search bar inside the <header>, only inside the <main> - Which tag can you place it into?
  • How to properly sanitize user input in WordPress search form: get_search_query()
  • Excluding out-of-stock products from Wordpress search results not working when using custom statuses
  • flutter Text field appears when button is clicked icons buttons
  • WordPress Search using multiple select options and no text search
  • Flutter How to show a message when flutter search filter result no result
  • How to override a function in the wp_includes folder(blocks/search.php)
  • Disable the enter key when the input form is unfilled
  • Limiting Wordpress Search to specific blog category not working
  • How to place the search form in the middle of navbar and expand it in Bootstrap 5

Popular Questions

  • How do I undo the most recent local commits in Git?
  • How can I remove a specific item from an array in JavaScript?
  • How do I delete a Git branch locally and remotely?
  • Find all files containing a specific text (string) on Linux?
  • How do I revert a Git repository to a previous commit?
  • How do I create an HTML button that acts like a link?
  • How do I check out a remote Git branch?
  • How do I force "git pull" to overwrite local files?
  • How do I list all files of a directory?
  • How to check whether a string contains a substring in JavaScript?
  • How do I redirect to another webpage?
  • How can I iterate over rows in a Pandas DataFrame?
  • How do I convert a String to an int in Java?
  • Does Python have a string 'contains' substring method?
  • How do I check if a string contains a specific word?

Trending Questions

  • UIImageView Frame Doesn't Reflect Constraints
  • Is it possible to use adb commands to click on a view by finding its ID?
  • How to create a new web character symbol recognizable by html/javascript?
  • Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
  • Heap Gives Page Fault
  • Connect ffmpeg to Visual Studio 2008
  • Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
  • How to avoid default initialization of objects in std::vector?
  • second argument of the command line arguments in a format other than char** argv or char* argv[]
  • How to improve efficiency of algorithm which generates next lexicographic permutation?
  • Navigating to the another actvity app getting crash in android
  • How to read the particular message format in android and store in sqlite database?
  • Resetting inventory status after order is cancelled
  • Efficiently compute powers of X in SSE/AVX
  • Insert into an external database using ajax and php : POST 500 (Internal Server Error)
  • Privacy
  • Terms
  • Cookies
  • Homegardensmart
  • Math
  • Aftereffectstemplates