Why don't buttons and inputs align well in Bootstrap?
I tried something simple like:
<input type="text"/><button class="btn">button</button>
The button is about 5px
lower than the input in chrome/firefox.
In order to put the button inside the input field, we shall use CSS. Let us see the HTML first. Creating structure: In this section, we will create the basic structure for the input field. HTML Code: By using the HTML, we will place the input field where we will add a responsive button to clear the field.
How to: Bootstrap button center. Add class . text-center to the parent div to align any item inside to the center.
HTML | <input> align Attribute left: It sets the alignment of image to the left. it is a default value. right: It sets the alignment of image to the right. middle: It sets the alignment of image to the middle.
In Twitter Bootstrap 4, inputs and buttons can be aligned using the input-group-prepend
and input-group-append
classes (see https://getbootstrap.com/docs/4.0/components/input-group/#button-addons)
<div class="input-group mb-3"> <div class="input-group-prepend"> <button class="btn btn-outline-secondary" type="button">Button</button> </div> <input type="text" class="form-control"> </div>
<div class="input-group mb-3"> <input type="text" class="form-control"> <div class="input-group-append"> <button class="btn btn-outline-secondary" type="button">Button</button> </div> </div>
As shown in the answer by @abimelex, inputs and buttons can be aligned by using the .input-group
classes (see http://getbootstrap.com/components/#input-groups-buttons)
<div class="input-group"> <span class="input-group-btn"> <button class="btn btn-default" type="button">Go!</button> </span> <input type="text" class="form-control"> </div>
<div class="input-group"> <input type="text" class="form-control"> <span class="input-group-btn"> <button class="btn btn-default" type="button">Go!</button> </span> </div>
This solution has been added to keep my answer up to date, but please stick your up-vote on the answer provided by @abimelex.
Bootstrap offers an .input-append
class, which works as a wrapper element and corrects this for you:
<div class="input-append"> <input name="search" id="search"/> <button class="btn">button</button> </div>
As pointed out by @OleksiyKhilkevich in his answer, there is a second way to align input
and button
by using the .form-horizontal
class:
<div class="form-horizontal"> <input name="search" id="search"/> <button class="btn">button</button> </div>
The difference between these two classes is that .input-append
will place the button
up against the input
element (so they look like they are attached), where .form-horizontal
will place a space between them.
-- Note --
To allow the input
and button
elements to be next to each other without spacing, the font-size
has been set to 0
in the .input-append
class (this removes the white spacing between the inline-block
elements). This may have an adverse effect on font-sizes in the input
element if you want to override the defaults using em
or %
measurements.
Bootstrap 5
<div class="input-group"> <input type="text" class="form-control"> <button class="btn btn-outline-secondary" type="button">Go</button> </div>
Bootstrap 3 & 4
you may use the input-group button property to apply the button direct to the input-field.
<div class="input-group"> <input type="text" class="form-control"> <span class="input-group-btn"> <button class="btn btn-default" type="button">Go!</button> </span> </div><!-- /input-group -->
Take a look at BS4 or BS5 Input-Group doc for many more examples.
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