Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display two fields side by side in a Bootstrap Form

I am trying to display a year range input on a form that has a 2 textboxes. One for the min and one for the max and are separated by a dash.

I want this all on the same line using bootstrap, but I cannot seem to get it to work correctly.

Here's my code:

<form id="Form1" class="form-horizontal" role="form" runat="server">     <div class="row">         <div class="form-group">             <label for="tbxContactPhone" class="col-sm-2 control-label">Year</label>             <div class="col-sm-4">                 <asp:TextBox ID="TextBox1" CssClass="form-control" runat="server" MaxLength="4" />             </div>             <label class="col-sm-2 control-label">-</label>             <div class="col-sm-4">                 <asp:TextBox ID="TextBox2" CssClass="form-control" runat="server" MaxLength="4" />             </div>         </div>     </div> </form> 

Here's what it looks like now:

screenshot

like image 437
user3788671 Avatar asked Dec 12 '14 20:12

user3788671


People also ask

How do I make two forms side by side in bootstrap?

The technique to create two forms horizontally is simple. You simply code two <div class=”span6″> columns and insert the required form markup. Do not add the . well class to the span columns or else it breaks up everything.

How do I display two columns side by side in HTML?

The most common way to place two divs side by side is by using inline-block css property. The inline-block property on the parent placed the two divs side by side and as this is inline-block the text-align feature worked here just like an inline element does.

How do I split a two column form in bootstrap?

Form Grids You can add columns and rows by adding <div class="row"> and <div class="col"> to your form. In the example above, the email and password fields are separated into two columns within a row. This is why they are displayed side by side on a webpage.


2 Answers

How about using an input group to style it on the same line?

Here's the final HTML to use:

<div class="input-group">     <input type="text" class="form-control" placeholder="Start"/>     <span class="input-group-addon">-</span>     <input type="text" class="form-control" placeholder="End"/> </div> 

Which will look like this:

screenshot

Here's a Stack Snippet Demo:

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" rel="stylesheet"/>    <div class="input-group">      <input type="text" class="form-control" placeholder="Start"/>      <span class="input-group-addon">-</span>      <input type="text" class="form-control" placeholder="End"/>  </div>

I'll leave it as an exercise to the reader to translate it into an asp:textbox element

like image 121
KyleMit Avatar answered Sep 23 '22 05:09

KyleMit


@KyleMit's answer on Bootstrap 4 has changed a little

<div class="input-group">     <input type="text" class="form-control">     <div class="input-group-prepend">         <span class="input-group-text">-</span>     </div>     <input type="text" class="form-control"> </div> 
like image 31
Sam Bellerose Avatar answered Sep 21 '22 05:09

Sam Bellerose