Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap: align input with button

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.

enter image description here

like image 286
pguardiario Avatar asked May 16 '12 09:05

pguardiario


People also ask

How do you add a button to an input field?

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 do I center a button in Bootstrap?

How to: Bootstrap button center. Add class . text-center to the parent div to align any item inside to the center.

How do you align input boxes in HTML?

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.


2 Answers

Twitter Bootstrap 4

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)

Group button on the left side (prepend)

<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> 

Group button on the right side (append)

<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> 

Twitter Bootstrap 3

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)

Group button on the left side

<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> 

Group button on the right side

<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.


Twitter Bootstrap 2

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 Differences

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.

like image 179
My Head Hurts Avatar answered Oct 15 '22 07:10

My Head Hurts


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.

example for bs3 input group button

like image 31
Karl Adler Avatar answered Oct 15 '22 07:10

Karl Adler