Musical Shore
Musical Shore

Reputation: 1381

bootstrap form elements not aligning properly

I am trying to align form input elements using bootstrap and not having much luck. I have tried using pull-left. Is there any way to do this using pure bootstrap, without having to add crufty one-off css?

<div class="modal-body">
    <form class="form-horizontal" role="form">
        <div class="row">
            <div class="form-group">
                <label class="control-label col-xs-2">Title:</label>
                <div class="col-xs-10">
                    <input  type="text" class="form-control" data-bind="value: title" />
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="control-label col-xs-2">Start:</label>
                <div class="input-group input-group-sm col-xs-3">
                    <input type="text" placeholder="mm/dd/yy" data-provide="datepicker" data-bind="value:date" id="Date" />
                    <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
            </div>
        </div>
    </form>
</div>

bootstrap form

Upvotes: 1

Views: 3224

Answers (2)

Peter Girnus
Peter Girnus

Reputation: 2739

First off you're missing class="form-control" on your second form group.

Second you're mixing columns with input groups, that's not the bootstrap way.

Don't mix input-group with other components. (see:http://getbootstrap.com/components/#input-groups)

Do not mix form groups or grid column classes directly with input groups. Instead, nest the input group inside of the form group or grid-related element."

Proper HTML Structure:

<div class="modal-body">
    <form class="form-horizontal" role="form">
        <div class="row">
            <div class="form-group">
                <label class="control-label col-xs-2">Title:</label>
                <div class="col-xs-10">
                    <input  type="text" class="form-control" data-bind="value: title" />
                </div>
            </div>
        </div>
        <div class="row">
            <div class="form-group">
                <label class="control-label col-xs-2">Start:</label>
                <div class="col-xs-4">
                   <div class="input-group">
                      <input  type="text" class="form-control" data-bind="value: title" />
                      <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                </div>
               </div>
            </div>
        </div>
   </form>
</div>

Fixed Demo CODEPEN

Upvotes: 2

vanburen
vanburen

Reputation: 21663

As stated, your structure isn't using the correct components together. You should not mix column classes with input-groups (docs) nor should you use rows within a horizontal form class. See the Docs

Use Bootstrap's predefined grid classes to align labels and groups of form controls in a horizontal layout by adding .form-horizontal to the form (which doesn't have to be a ). Doing so changes .form-groups to behave as grid rows, so no need for .row.

Working example Snippet.

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Open Modal</button>
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4>Some Form</h4>

      </div>
      <div class="modal-body">
        <form class="form-horizontal">
          <div class="form-group">
            <label class="control-label col-xs-3">Title:</label>
            <div class="col-xs-9">
              <input type="text" class="form-control" data-bind="value: title" />
            </div>
          </div>
          <div class="form-group">
            <label class="control-label col-xs-3">Start:</label>
            <div class="col-xs-9">
              <div class="input-group">
                <input type="text" class="form-control" placeholder="mm/dd/yy" data-provide="datepicker" data-bind="value:date" id="Date" /> <span class="input-group-addon"><i class="fa fa-calendar"></i></span>

              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
</div>

Upvotes: 2

Related Questions