Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a dropdown select form in Twitter Bootstrap

They've provided the example markup for a regular input-text form:

<form class="form-horizontal">
  <fieldset>
    <legend>Legend text</legend>
    <div class="control-group">
      <label class="control-label" for="input01">Text input</label>
      <div class="controls">
        <input type="text" class="input-xlarge" id="input01">
        <p class="help-block">Supporting help text</p>
      </div>
    </div>
  </fieldset>
</form>

But how could I get something like

enter image description here

Just a bit confused on how the markup would look for a select list. Thanks!

like image 692
Elias7 Avatar asked May 23 '12 01:05

Elias7


People also ask

How do I add a dropdown to a form in Bootstrap?

Basic Dropdown To open the dropdown menu, use a button or a link with a class of . dropdown-toggle and the data-toggle="dropdown" attribute. Add the . dropdown-menu class to a <div> element to actually build the dropdown menu.

How set dropdown position in Bootstrap?

By default, a dropdown menu is automatically positioned 100% from the top and along the left side of its parent. Add . dropdown-menu-right to a . dropdown-menu to right align the dropdown menu.

Why is my dropdown menu not working Bootstrap?

Why is my drop down menu not working in bootstrap? Solution : The dropdown should be toggled via data attributes or using javascript. In the above program, we have forgotten to add a data attribute so the dropdown is not working. So add data-bs-toggle="dropdown" to toggle the dropdown.

How do I move a drop down to left in Bootstrap?

To override it, use . dropdown-menu-left.


2 Answers

Bootstrap 2.3.2:

<form class="form-horizontal">
    <fieldset>          
        <div class="control-group">
            <label class="control-label">Select:</label>
            <div class="controls">
                <select>
                    <option value="1">Option 1</option>
                    <option value="2">Option 2</option>
                    <option value="3">Option 3</option>
                </select>
            </div>
        </div>
    </fieldset>
</form>

Bootstrap 3.x:

<form role="form" class="form-horizontal">
    <fieldset>
      <div class="form-group">
        <label class="col-sm-2 control-label">Select:</label>
        <div class="col-sm-3">
            <select class="form-control">
                <option value="1">1</option>
                <option value="2">2</option>
                <option value="3">3</option>
            </select>
        </div>
      </div>
    </fieldset>
</form>

col-sm-2 and col-sm-3 define the widths for you can change accordingly. Have a look at the Grid system on the Bootstrap site to see the different variations.

Hope this helps

like image 145
Lodder Avatar answered Oct 21 '22 17:10

Lodder


The answer by Lodder is correct for Bootstrap 2.x, but it will not work for Bootstrap 3. This will work Bootstrap 3:

<form class="form-horizontal">
    <fieldset>          
        <div class="form-group">
            <label class="control-label col-lg-2">Select:</label>
            <div class="col-lg-3">
                <select class="form-control">
                    <option value="1">Option 1</option>
                    <option value="2">Option 2</option>
                    <option value="3">Option 3</option>
                </select>
            </div>
        </div>
    </fieldset>
</form>
like image 27
rudivonstaden Avatar answered Oct 21 '22 16:10

rudivonstaden