I have an inline form with a search bar and a search button next to it. How can I force the input-group
div to span across the entire column in Bootstrap 4 and preferably without using custom CSS?
<div class="container"> <div class="row"> <div class="col-md-12"> <form class="form-inline" action="/search" accept-charset="UTF-8" method="get"> <div class="input-group"> <input type="text" name="search" id="search" value="test" placeholder="Search accounts, contracts and transactions" class="form-control"> <span class="input-group-btn"> <input type="submit" name="commit" value="Search" class="btn btn-primary" data-disable-with="Search"> </span> </div> </form> </div> </div> </div>
Choose from a responsive, fixed-width container (meaning its max-width changes at each breakpoint) or fluid-width (meaning it's 100% wide all the time). container-fluid class can be used to get full width container. container-fluid class provides a full-width container which spans the entire width of the viewport.
For default size . form-control is used, for smaller size we can use . form-control class along with . form-control-sm and for larger size we can use .
Additional rule for an inline form: Add class .form-inline to the <form> element.
Set the heights of input elements using classes like .input-lg and .input-sm . Set the widths of elements using grid column classes like .col-lg-* and .col-sm-* .
Updated 2018
Remove the form-inline
..
<div class="container"> <div class="row"> <div class="col-md-12"> <form action="" accept-charset="UTF-8" method="get"> <div class="input-group"> <input type="text" name="search" id="search" value="test" placeholder="Search accounts, contracts and transactions" class="form-control"> <span class="input-group-btn"> <input type="submit" name="commit" value="Search" class="btn btn-primary" data-disable-with="Search"> </span> </div> </form> </div> </div> </div>
Demo: http://www.codeply.com/go/4eu7w6KPkT
Another option - full width input that stacks vertically on xs:
<div class="row"> <div class="col-md-12"> <form class="row"> <div class="col-12 col-sm pr-sm-0"> <input type="text" name="search" id="search" value="test" placeholder="Search accounts, contracts and transactions" class="form-control"> </div> <div class="col-12 col-sm-auto pl-sm-0"> <input type="button" name="commit" value="Search" class="btn btn-primary btn-block"> </div> </form> </div> </div>
Demo
You have three possible ways to implement the following two layout options:
See my Codeply.com demonstrating all solutions detailed below...
Just add class flex-fill
to your <div class="form-group">
.
Please note from your original example code above, Bootstrap 4 has changed from using input-group-btn
to input-group-append
to make the button stick to the keyword field and handle the rounded corners correctly.
Finally, I've also added an aria-label
attribute to satisfy accessibility requirements to your example.
<div class="container"> <div class="row"> <div class="col-md-12"> <form class="form-inline" action="/search" accept-charset="UTF-8" method="get"> <div class="input-group flex-fill"> <input type="search" name="search" id="search" value="" placeholder="Full-width search form with sticky button" class="form-control" aria-label="Search this site"> <div class="input-group-append"> <input type="submit" name="commit" value="Search" class="btn btn-primary" data-disable-with="Search"> </div> </div> </form> </div> </div> </div>
For this solution you have two options, depending on your desired HTML structure and needs of the project.
First, you could remove all the wrapper divs and add classes flex-fill mr-2
to the <input class="form-control">
. This is the easiest option.
<div class="container"> <div class="row"> <div class="col-md-12"> <form class="form-inline" action="/search" accept-charset="UTF-8" method="get"> <input type="search" name="search" id="search" value="" placeholder="Full-width search form with separate button w/o wrapper div" class="flex-fill mr-2 form-control" aria-label="Search this site"> <input type="submit" name="commit" value="Search" class="btn btn-primary" data-disable-with="Search"> </form> </div> </div> </div>
If you have the chance, I would go with this solution as it produces the cleanest HTML ;)
If your form requires some wrapper divs, (eg due to fighting against your CMS) then you have to battle with where you place the classes: First, replace the <div class="input-group">
with <div class="flex-fill mr-2">
, add class w-100
to the <input class="form-control">
, and finally delete the <div class="input-group-append">
wrapper and move the submit button outside the flex-fill wrapper.
<div class="container"> <div class="row"> <div class="col-md-12"> <form class="form-inline" action="/search" accept-charset="UTF-8" method="get"> <div class="flex-fill mr-2"> <input type="search" name="search" id="search" value="" placeholder="Full-width search form with separate button w/ wrapper div" class="form-control w-100" aria-label="Search this site"> </div> <input type="submit" name="commit" value="Search" class="btn btn-primary" data-disable-with="Search"> </form> </div> </div> </div>
Ultimately, your choice may boil down to how you want this to work on mobile. You can of course wrangle the code even further, but it is worth looking at the following breakpoints:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With