Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing form into two columns with twitter bootstrap

What is the correct way to style form with bootstrap in the following scenario:

two columns form view

  1. I need to use fieldset (that will be styled later)

  2. I need to divide form into two columns

  3. Each column has label+control, some will have label+control1+control2 etc.

I am new to bootstrap. I have come to the following solution (jsfiddle).

The question is: is this the correct way to do this? Does it not violate the bootstrap semantics?

I do not understand when to use form-group, am I using it correctly?

Should I not include some class="row" here for correct semantics?

HTML:

<div class="container">
... <some content here> ....

<form class="form-horizontal">
    <fieldset>
        <legend>Portfolio</legend>
        <div class="col-xs-6">
            <div class="form-group">
                <label for="name" class="col-xs-4 control-label">Label1</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" placeholder="control1" />
                </div>
            </div>
            <div class="form-group">
                <label for="name" class="col-xs-4 control-label">label2</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" placeholder="control2" />
                </div>
            </div>
            <div class="form-group">
                <label for="name" class="col-xs-4 control-label">label3</label>
                <div class="col-xs-8 form-inline">
                    <div class="form-group col-xs-6">
                        <input type="text" class="form-control" placeholder="control1" />
                    </div>
                    <div class="form-group col-xs-6">
                        <input type="text" class="form-control" placeholder="control2" />
                    </div>
                </div>
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label for="name" class="col-xs-4 control-label">Label1</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" placeholder="control1" />
                </div>
            </div>
            <div class="form-group">
                <label for="name" class="col-xs-4 control-label">label2</label>
                <div class="col-xs-8">
                    <input type="text" class="form-control" placeholder="control2" />
                </div>
            </div>
        </div>
    </fieldset>
</form>
</div>

I have seen all Bootstrap form examples, but I do not get how to separate form into two columns. I have also read the whole documentation, but I feel that this is not the right way how to use col and other classes.

like image 822
renathy Avatar asked Oct 28 '13 14:10

renathy


1 Answers

Column separation happens inside container elements.
Each time you have an element you want to do grid inside it, give it class="container" and in it u can use the normal row and column classes.

Also check Bootstrap's out of the box form styles.

Below are the bare bones, add on to it what u need (like text align etc)

<form class="container">
    <div class="row">
        <div class="col-md-3">
            <label for="username" class="control-label">label</label>
        </div>

        <div class="col-md-3">
            <input type="text" name="username" class="form-control" placeholder="Email address" autofocus>
        </div>

        <div class="col-md-3">
            <label for="username" class="control-label">label</label>
        </div>

        <div class="col-md-3">
            <input type="text" name="username" class="form-control" placeholder="Email address" autofocus>
        </div>

    </div>
</form>
like image 60
Itay Moav -Malimovka Avatar answered Nov 06 '22 11:11

Itay Moav -Malimovka