Rmartin
Rmartin

Reputation: 243

Bootstrap3: right-align labels next to input box (in django 1.5)

I wish to have labels right-aligned next to its input box (with a space in between). However, labels are still left-aligned. According to Boostrap3 documentation and this question, the form-horizontal class should be used, but still result is not right.

The django template code below does generate fields in a two-column fashion, but with left-aligned labels:

<form class="form-horizontal" role="form">
    <div class="form-group">

        <div class="col-md-3 col-md-offset-1 input-md">
            {{ form.code|bootstrap_horizontal }}
        </div>
        <div class="col-md-3 col-md-offset-2 input-md">
            {{ form.name|bootstrap_horizontal }}
        </div>
        <div class="col-md-3 col-md-offset-1 input-md">
            {{ form.company|bootstrap_horizontal }}
        </div>

    </div>

    <div class="btn-group btn-group-lg">
        <button class="btn" type="submit">Crear Marca</button>
    </div>
</form>

I guess I'm missing something. Thanks in advance for your help.

EDITED: Output HTML in jsfiddle.
Image of actual output included as well in this link. As you can see, Code & Company are left-aligned.

Upvotes: 0

Views: 24953

Answers (1)

Bass Jobsen
Bass Jobsen

Reputation: 49044

I think your text is right-aligned but the effect is not visible due to the nesting of your grid classes.

Try something like:

<form class="form-horizontal container" role="form">
  <div class="form-group">
    <label for="inputEmail1" class="col-sm-2 control-label">Code</label>
    <div class="col-sm-4">
      <input type="email" class="form-control" id="inputEmail1" placeholder="Email">
    </div>

    <label for="inputPassword1" class="col-sm-2 control-label">Name</label>
    <div class="col-sm-4">
      <input type="password" class="form-control" id="inputPassword1" placeholder="Password">
    </div>
  </div>
    <div class="form-group">
    <label for="inputEmail1" class="col-sm-2 control-label">Company</label>
    <div class="col-sm-4">
      <input type="email" class="form-control" id="inputEmail1" placeholder="Email">
    </div>
  </div>
  <div class="btn-group btn-group-lg">
        <button class="btn" type="submit">Crear Marca</button>
  </div>
</form>

When you nest your grid classes (col--) a child get a percentage of the width of its parent. When you nest a col-lg-6 in a col-lg-6 its with will be 25% of the viewport 50% of 50%.

In the case you want to input next to each other with a float left (your col-- classes add this float) they can't be in different input-groups. The .from-group has a clearfix which undo the float left. So try something like:

<div class="col-sm-6">
    <div class="form-group">
    <label for="inputEmail1" class="col-sm-4 control-label">Company</label>
    <div class="col-sm-8">
      <input type="email" class="form-control" id="inputEmail1" placeholder="Email">
    </div>
    </div>
</div>      
<div class="col-sm-6">
    <div class="form-group">
    <label for="inputEmail1" class="col-sm-4 control-label">Company 2</label>
    <div class="col-sm-8">
      <input type="email" class="form-control" id="inputEmail1" placeholder="Email">
    </div>
    </div>
</div>

Upvotes: 2

Related Questions