Bootstrap: align input with button

前端 未结 12 577
栀梦
栀梦 2020-11-28 17:21

Why don\'t buttons and inputs align well in Bootstrap?

I tried something simple like:

相关标签:
12条回答
  • 2020-11-28 18:02

    you may use the input-group button property to apply the button direct to the input-field.

    Bootstrap 3 & 4

    <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 Input-Group doc for many more examples.

    example for bs3 input group button

    0 讨论(0)
  • 2020-11-28 18:04

    Not directly related, unless you have similar code but I just noticed a strange behaviour to form-inline, bootstrap 3.3.7

    I did not use row and cells for this as it is a desktop project, if the opening tag was below table (in this case):

    <table class="form-inline"> 
    <form> 
    <tr>
    

    Form elements would stack.

    Switch and it properly lined up.

    <form>
    <table class="form-inline">
    <tr>
    

    Safari 10.0.2 and latest Chrome did not make any differences. Maybe my layout was wrong but it is not very forgiving.

    0 讨论(0)
  • 2020-11-28 18:05

    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">
      <div class="input-group-append">
        <button class="btn btn-outline-secondary" type="button">Button</button>
      </div>
      <input type="text" class="form-control">
    </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.

    0 讨论(0)
  • 2020-11-28 18:06

    I was also struggling with same issue. The bootstrap classes I use are not working for me. I came up with an workaround, as below:

    <form action='...' method='POST' class='form-group'>
      <div class="form-horizontal">
        <input type="text" name="..." 
            class="form-control" 
            placeholder="Search People..."
            style="width:280px;max-width:280px;display:inline-block"/>
    
        <button type="submit" 
            class="btn btn-primary" 
            style="margin-left:-8px;margin-top:-2px;min-height:36px;">
          <i class="glyphicon glyphicon-search"></i>
         </button>
      </div>
    </form>
    

    Basically, I overrode the display property of class "form-control", and used other style properties for correcting the size and position.

    Following is the result:

    0 讨论(0)
  • 2020-11-28 18:09

    Just the heads up, there seems to be special CSS class for this called form-horizontal

    input-append has another side effect, that it drops font-size to zero

    0 讨论(0)
  • 2020-11-28 18:12

    I tried all the above codes and none of them fixed my issues. Here is what worked for me. I used input-group-addon.

    <div class = "input-group">
      <span class = "input-group-addon">Go</span>
      <input type = "text" class = "form-control" placeholder="you are the man!">
    </div>
    
    0 讨论(0)
提交回复
热议问题