Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bootstrap, align input with label to button without label, vertical form / not horizontal

I want to align input with label, to button without label, using bootstrap v3 with default form (vertical, not horizontal layout of form).

Desired effect: http://screencast.com/t/b2uwBopW9rW

I managed to solve this by creating label with non-breaking space inside label, but I wonder if there is more clean, better way to achieve this effect.

Example on jsfiddle: https://jsfiddle.net/r6o5hysk/1/

<div class='container' style='width:200px; margin: 50px;'>
    <div class="row">
      <div class="col-xs-10">
        <div class="form-group">
            <label>
                Name
                <input type="text" class="form-control" />
            </label>
        </div>
      </div>
      <div class="col-xs-2">
        <div class="form-group">
          <label>
              &nbsp;
              <div class="btn btn-danger">Remove</div>
          </label>
        </div>
      </div>
    </div>
</div>

Thank you for your time.

like image 879
LPodolski Avatar asked Jul 03 '15 21:07

LPodolski


People also ask

How do I center align a label in bootstrap?

Add text-align: center in your CSS or text-center class to your parent element (probably to a row or container ).

How do I center a text box in bootstrap?

By adding the ” text-center” class of Bootstrap 3 We can use the “text-center” class of bootstrap 3 for the center-alignment of an element. So in our td when we add “text-center” class, and then our table text goes to the center.

What does form control do in bootstrap?

Give textual form controls like <input> s and <textarea> s an upgrade with custom styles, sizing, focus states, and more.


1 Answers

Adding margins is not the best idea. What if the label font-size changes? You'll need to adjust the margin as well.

Make the button to act as a block

.btn.btn-as-block {
  display: block;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<div class='container'>
  <div class="row">

    <div class="col-xs-9">
      <div class="form-group">
        <label>Name</label>
        <input type="text" class="form-control" />
      </div>
    </div>

    <div class="col-xs-3">
      <div class="form-group">
        <label>&nbsp;</label>
        <div class="btn btn-danger btn-as-block">Remove</div>
      </div>
    </div>

  </div>
</div>
like image 109
Dmytro Skliarov Avatar answered Sep 22 '22 05:09

Dmytro Skliarov