How to stretch the searchbox in a navbar using bootstrap 5? - html

I'm new to front-end development and I am trying to design a page that also has a searchbox(here's the jsfiddle https://jsfiddle.net/he40tr9z/1/). I copied the navbar component from https://getbootstrap.com/docs/5.0/components/navbar/ and did some minimal modifications to make the searchbox appear before the nav items and to right align the nav items. Now I want the searchbox to stretch from the navbar-brand to the nav items but I can't seem to do that. I've searched the web and all the solutions I've found were for previous versions of Bootstrap which when applied to Bootstrap 5 don't work.
Here's my code snippet:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#"><img src="images/logo.png" alt="Brand image" width="180" height="52"></img></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 ms-auto me-auto">
<input class="form-control" id="searchbox" type="search" placeholder="Search" aria-label="Search"></input>
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
<ul class="navbar-nav ms-auto mb-2 mb-lg-0 d-flex">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Login</a>
</li>
</ul>
</div>
</div>
</nav>
Here is the code from my css file:
#searchbox{
width: 100%;
}
I put width:100% cause I saw it in this solution Bootstrap: stretching navbar input box but it doesn't work for Bootstrap 5.

Just use w-100 on the containing form...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container-fluid">
<a class="navbar-brand" href="#"><img src="//via.placeholder.com/180x52" alt="Brand image" width="180" height="52"></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 ms-auto me-auto border w-100">
<input class="form-control" id="searchbox" 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 d-flex">
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="#">Login</a>
</li>
</ul>
</div>
</div>
</nav>
Codeply

Related

Need help in centering links below navbar-brand using Bootstrap 5

I am practicing how to create a navbar using Bootstrap 5 and can't get the links centered and below the navbar-brand.
I've attach an image of my current navbar.
Screenshot
My script:
<nav class="navbar navbar-expand-lg flex-column align-items-stretch" style="background-color: #FAF8F1; border:3px solid;">
<div class="d-flex">
<a class="navbar-brand mx-sm-auto mr-auto" href=""> Honeybee Babies </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>
<div class="collapse navbar-collapse w-100" id="navbarSupportedContent">
<unli class="navbar-nav">
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Baby Essentials</a>
</li>
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Price Range</a>
</li>
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Brands</a>
</li>
</unli>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn" style="color:#C58940" type="submit">Search</button>
</form>
</div>
</div>
</nav>
I've tried using justify-content-center in
I'm not sure what I'm doing wrong.
Any help/advice would be appreciated.
Thanks!
Add justify-content-center to the navbar-collapse.
See the snippet below.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<nav class="navbar navbar-expand-lg flex-column align-items-stretch" style="background-color: #FAF8F1; border:3px solid;">
<div class="d-flex">
<a class="navbar-brand mx-sm-auto mr-auto" href=""> Honeybee Babies </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>
<div class="collapse navbar-collapse w-100 justify-content-center" id="navbarSupportedContent">
<unli class="navbar-nav">
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Baby Essentials</a>
</li>
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Price Range</a>
</li>
<li class="nav-item rounded-pill" style="background-color:#C58940;">
<a class="nav-link" style="color:#FAF8F1" href="">Brands</a>
</li>
</unli>
<form class="d-flex" role="search">
<input class="form-control me-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn" style="color:#C58940" type="submit">Search</button>
</form>
</div>
</nav>

Bootstrap 4 navbar is always visible on mobile

My bootstrap navbar is always visible on mobile. No matter if navbar-toggler is collapsed or not.
Here is the code.
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="/"><i class="fas fa-blog"></i> Tech Info</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="d-flex flex-row-reverse collapse navbar-collapse" id="navbarSupportedContent1">
<ul class="nav justify-content-end">
<% if(!user) { %>
<li class="nav-item">
<a class="nav-link my-2 my-sm-0" href="/login">Sing In</a>
</li>
<li class="nav-item">
<a class="nav-link btn btn-outline-primary my-2 my-sm-0" href="/register">Sign Up</a>
</li>
<% } %>
</ul>
</div>
</div>
</nav>
The d-flex class is overriding the navbar-collapse behavior. If you want to reverse the flex direction use it on the nav element instead...
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<div class="container">
<a class="navbar-brand" href="/"><i class="fas fa-blog"></i> Tech Info</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
<ul class="nav justify-content-end flex-row-reverse">
<li class="nav-item">
<a class="nav-link my-2 my-sm-0" href="/login">Sing In</a>
</li>
</ul>
</div>
</div>
</nav>
Demo: https://www.codeply.com/p/MgNaErmlWm
The target of the button must be equal to the div id you want to collapse, and remove the d-flex from the div.navbar-collapse
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1"
aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">
...
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
...
</div>
navbar should not be a container generally. Try taking div class="container " out.

Navbar toggler is displayed, but I click on it and it doesnt shows the navbar items

I'm trying to get a navbar with two items (a ngbdropdown and a link) and when I reduce the resolution the toggler is displayed, but I click on it and it doesn't do anything.
This is my html code: (Missing the nav tag at the end, I put it here but doesn't detect it).
<nav class="navbar navbar-expand-lg navbar-light navbar-toggleable-md" role="navigation">
<a class="navbar-brand ml-3 margin-items" href="#"><img src="../assets/GroupeRenault.png" alt="groupeRenault"></a>
<button
class="navbar-toggler collapsed"
type="button"
data-toggle="collapse"
data-target="#navbarNavDropdown"
aria-controls="navbarNavDropdown"
aria-expanded="true"
aria-label="Toggle navigation"
>
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto" >
<li class="nav-item mr-2 padding-items">
<a style="color:#666" href="https://dda-localhost:4200/dashboard" >{{this.userLanguage}}</a>
</li>
<li class="nav-item ">
<div
ngbDropdown
class="d-inline-block"
>
<button
class="bNombre"
id="dropdownForm1"
ngbDropdownToggle
>
{{this.userName}}
</button>
<div ngbDropdownMenu aria-labelledby="dropdownForm1">
<button ngbDropdownItem (click)="this.logout()">Logout</button>
</div>
</div>
</li>
</ul>
</div>
Just make the following changes, ie just add or remove class named show on button click
component.ts
isNavbarMobile: boolean = false;
component.html
<nav class="navbar navbar-expand-lg navbar-light navbar-toggleable-md" role="navigation">
<a class="navbar-brand ml-3 margin-items" href="#"><img src="../assets/GroupeRenault.png" alt="groupeRenault"></a>
<button (click)="isNavbarMobile = !isNavbarMobile" class="navbar-toggler collapsed" type="button" >
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" [ngClass]="{'show': isNavbarMobile}" id="navbarNavDropdown">
<ul class="navbar-nav ml-auto" >
<li class="nav-item mr-2 padding-items">
<a style="color:#666" href="https://dda-localhost:4200/dashboard" >{{this.userLanguage}}</a>
</li>
<li class="nav-item ">
<div ngbDropdown class="d-inline-block" >
<button class="bNombre" id="dropdownForm1" ngbDropdownToggle >
{{this.userName}}
</button>
<div ngbDropdownMenu aria-labelledby="dropdownForm1">
<button ngbDropdownItem (click)="this.logout()">Logout</button>
</div>
</div>
</li>
</ul>
</div>
</nav>

Align Navbar search form and button in the center with Bootstrap 4

Here is the code of the header and i want to align its search bar and button in center
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Aditya Shrivastava</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav class="navbar navbar-light bg-light">
<form class="form-inline">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</nav>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="#">Twitter</a>
<a class="nav-item nav-link" href="#">Resume</a>
<a class="nav-item nav-link" href="#">Blog</a>
</div>
</div>
</nav>
</header>
To align Bootstrap 4 Navbar content use the utility classes
flex-grow-1 on the brand and form so they fill width
mx-lg-auto to center the form on lg and up (navbar-expand-lg breakpoint)
ml-auto on the right navbar-collapse to keep it right aligned
m-* and p-* to adjust margins or padding as desired
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand flex-grow-1" href="#">Aditya Shrivastava</a>
<div class="flex-grow-1 d-flex">
<form class="form-inline flex-nowrap bg-light mx-0 mx-lg-auto rounded p-1">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success" type="submit">Search</button>
</form>
</div>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="#">Twitter</a>
<a class="nav-item nav-link" href="#">Resume</a>
<a class="nav-item nav-link" href="#">Blog</a>
</div>
</div>
</nav>
</header>
https://www.codeply.com/go/NXIwAj610b
Related: Bootstrap NavBar with left, center or right aligned items
You should be give text-align: center; into .form-inline class. Or also use bootstrap default class align="center"
Hope this help.
Let me know further clarification.
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Aditya Shrivastava</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav class="navbar navbar-light bg-light">
<form class="form-inline" style="text-align: center;">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</nav>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="#">Twitter</a>
<a class="nav-item nav-link" href="#">Resume</a>
<a class="nav-item nav-link" href="#">Blog</a>
</div>
</div>
</nav>
</header>
Bootstrap have inbuilt center align u just have to add align="center" beside your form class
<header>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="#">Aditya Shrivastava</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<nav class="navbar navbar-light bg-light">
<form class="form-inline" align="center">
<input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search">
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button>
</form>
</nav>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav ml-auto">
<a class="nav-item nav-link" href="#">Twitter</a>
<a class="nav-item nav-link" href="#">Resume</a>
<a class="nav-item nav-link" href="#">Blog</a>
</div>
</div>
</nav>
</header>

Bootstrap 4 Center searchbar in navbar

I am trying to centre my search bar inside my navbar. Currently, I have it aligned to the left, But I'm trying to get it in the middle of the navbar.
This is the HTML code I have:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-lg navbar-light bg-light navbar-custom">
<a class="navbar-brand" href="#">ICT-Hulp(Waarschijnelijk logo)</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">
<div class="justify-content-md-center">
<form class="form-inline my-2 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Zoeken..." aria-label="Search" style="width: 600px;">
<button class="btn btn-info my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</li>
</ul>
</div>
</nav>
I Can't get the search bar centred. I can't find it on the bootstrap site either because its very vague
Add ml-auto class to navbar-nav along with mr-auto
You can add margin-left: auto by adding mr-auto to navbar to center the searchbar in the navbar. But it won't be at the center of the header, since there is another component beside the navbar.
<ul class="navbar-nav mr-auto ml-auto">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css" rel="stylesheet" />
<nav class="navbar navbar-expand-lg navbar-light bg-light navbar-custom">
<a class="navbar-brand" href="#">ICT-Hulp(Waarschijnelijk logo)</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 ml-auto">
<li class="nav-item">
<div class="justify-content-md-center">
<form class="form-inline my-2 my-lg-0 my-lg-0">
<input class="form-control mr-sm-2" type="text" placeholder="Zoeken..." aria-label="Search" style="width: 600px;">
<button class="btn btn-info my-2 my-sm-0" type="submit">Search</button>
</form>
</div>
</li>
</ul>
</div>
</nav>