Twitter Bootstrap inline form spacing - css

Here is a JSFiddle showing my code in action.
I want to add a few pixels of spacing between the different parts of the form in the most correct way using Twitter Bootstrap 3 taking responsiveness into consideration. I've looked over the documentation, but it still isn't clear to me what the best way to accomplish this is.
Any ideas?
Here is my current form HTML:
<form class="form-inline" role="form">
<div class="form-group">
<label class="sr-only" for="timezone">Timezone</label>
<select class="form-control" id="timezone" name="timezone">
<option value="America/St_Lucia">America/St_Lucia</option>
<option value="Europe/Nicosia">Europe/Nicosia</option>
</select>
</div>
<label class="radio-inline">
<input id="timeformat-0" name="timeformat" value="24" type="radio" />
19:00
</label>
<label class="radio-inline">
<input checked id="timeformat-1" name="timeformat" value="12" type="radio" />
7:00 PM
</label>
<button class="btn" type="submit">Save</button>
</form>

You can try with margin to all direct childrens of .form-inline:
.form-inline > * {
margin:5px 3px;
}
View this fiddle http://jsfiddle.net/MFKBj/10/
PD: I only add !important in the fiddle to make me sure it's over the bootstrap CSS you don't need this.

Here is an example of using the Bootstrap Grid system. You could apply a bunch of classes to handle the different viewports.
<form class="form-inline" role="form">
<div class="form-group row">
<div class="col-md-6">
<label class="sr-only" for="timezone">Timezone</label>
<select class="form-control" id="timezone" name="timezone">
<option value="America/St_Lucia">America/St_Lucia</option>
<option value="Europe/Nicosia">Europe/Nicosia</option>
</select>
</div>
<div class="col-md-3"">
<label class="radio-inline">
<input id="timeformat-0" name="timeformat" value="24" type="radio" />
19:00
</label>
</div>
<div class="col-md-3">
<label class="radio-inline">
<input checked id="timeformat-1" name="timeformat" value="12" type="radio" />
7:00 PM
</label>
</div>
<button class="btn" type="submit">Save</button>
</div>
</form>

I had the same question as the OP - he is asking about spacing things out horizontally - BS smashes things together pretty well as seen here. Here's my solution:
.form-inline label {
margin-left: 5px;
}

Here's how I've done it; add minimal spacing to all labels and controls, and use a negative margin on the container to keep everything correctly aligned.
.form-inline {
margin: 0 -3px;
}
.form-inline .form-group label,
.form-inline .form-group input,
.form-inline .form-group select {
margin: 0 3px;
}

In Bootstrap 4 you have the Spacing utilities.
<div class="row">
<div class="col-sm-6 mb-lg-2">
<!-- column-small-50%, margin-bottom-large -->
</div>
</div>

In Bootstrap 3, you can wrap the elements in a div.form-group, like so:
<div class="form-group">
<label class="radio-inline">
<input id="timeformat-0" name="timeformat" value="24" type="radio" />
19:00
</label>
</div>
<div class="form-group">
<label class="radio-inline">
<input checked id="timeformat-1" name="timeformat" value="12" type="radio" />
7:00 PM
</label>
</div>
You can see this in this section of the Bootstrap 3 docs:
https://getbootstrap.com/docs/3.3/css/#forms-inline

Related

Bootstrap - Vertical Radio Buttons not aligned

Im having a problem stacking radio buttons once the screen size reaches a certain size (in this case, 768px for mobile phones). I have this local #media override that takes the radio-inline and displays it as a block - however, once I do this, the first Radio Button "0", is slightly offset and does not align with the rest below it. Any thoughts on a work around or if im doing something wrong?
<style>
#media (max-width: 768px){
.radio-inline{
display:block;
}
}
</style>
<div class="container-fluid">
<div class="jumbotron">
<div>
<asp:Label runat="server" CssClass="h3" ID="Header" Text="EXAMPLE
TEXT"/>
<br />
<br />
<asp:Label runat="server" CssClass="h3" id="S1W" Text="EX1" />
</div>
<div class="row" style="padding-bottom: 30px;">
<div class="col-lg-12">
<label class="radio-inline">
<input type="radio" name="A1" value="0" required> <b>0&nbsp
&nbsp</b>
</label>
<label class="radio-inline">
<input type="radio" name="A1" value="1" required> <b>1&nbsp
&nbsp</b>
</label>
<label class="radio-inline">
<input type="radio" name="A1" value="2" required>
<b>2&nbsp &nbsp</b>
</label>
</div>
</div>
</div>
You need to remove the spaces, "&nbsp", from each input. This should fix the issue as shown in this code snippet. If you need spacing, use CSS.
https://jsfiddle.net/tbuchanan/Lqj412tu/
<label class="radio-inline">
<input type="radio" name="A1" value="0" required> <b>0</b>
</label>

Horizontal form with inline radio button and textboxes

I have a horizontal form in bootstrap and I am trying to arrange a radio button with two text fields like so (sorry I can't embed the image):
However, I can't see to replicate this. I've tried the following (Jade markup):
.col-sm-4
.form-group
label.control-label(for='time') Time Range
.form-inline
.form-group
input(type='radio', id="from", name='time', value='from', data-toggle='radio')
label(for="from") From
.form-group
input(type='text', name='first')
.form-group
input(type='text', name='last')
Which results in everything being on one line, but the "From" is completely covered up and the two text boxes are back to back with no space. Is this a simple CSS fix, or am I mis-understanding form layouts in Bootstrap 3? Any feedback would be much appreciated.
EDIT: The rendered html is:
<div class="form-group">
<label for="time" class="control-label">Time Range</label>
<div class="form-inline">
<div class="form-group">
<input type="radio" id="from" name="time" value="from" data-toggle="radio">
<label for="from">From</label>
</div>
<div class="form-group">
<input type="text" name="first">
</div>
<div class="form-group">
<input type="text" name="last">
</div>
</div>
</div>
Here's how you want it:
<div class="form-inline">
<label for="time" class="control-label">Time Range</label>
<div class="form-group">
<input type="radio" id="from" name="time" value="from" data-toggle="radio">
<label class="control-label" for="from">From</label>
</div>
<div class="form-group">
<input class="form-control" type="text" name="first">
</div>
<div class="form-group">
<label class="control-label">To</label>
<input class="form-control" type="text" name="last">
</div>
</div>
See this demo
I'm not sure how to translate that into Jade markup--I'll leave that to you :)
EDITED to add: This is what an online converter spit out:
.form-inline
label.control-label(for='time') Time Range
.form-group
input#from(type='radio', name='time', value='from', data-toggle='radio')
label.control-label(for='from') From
.form-group
input.form-control(type='text', name='first')
.form-group
label.control-label To
input.form-control(type='text', name='last')

Bootstrap alignment and spacing of vertical checkboxes with associated horizontal text fields

What is the most elegant (ie. non-hacky) way to properly align a group of checkboxes, one or more of which have associated text inputs? Whatever I have tried, I end up with (i) unevenly spaced checkboxes, or (ii) misaligned checkboxes, or (iii) misaligned labels, or (iv) misaligned text boxes.
Since the use case of:
What is your favourite food?
(a) Apple
(b) Banana
(c) Carrot
(d) Other: _______
is surely very common, I wonder whether anyone has found a way to do this with Bootstrap standard classes, before I start forcing my own classes into the code.
Here is the code I have now:
<div class="form-group">
<label class="col-sm-2 control-label">
Favourite food
</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Apple
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Banana
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/>
Other <input type="email" placeholder="someone#somewhere.com" class="form-control"/>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox"/> Any fruit from one of these colour groups: Red, Green, Blue
</fieldset>
</label>
</div>
</div>
</div>
Screenshot attached showing output in Chrome:
The fourth checkbox is too far from the third, while the text and 'input type=text' on the third line are too low.
Yes, there is a simple bootstrap way, just change the code of the checkbox to this:
<div class="checkbox">
<div class="form-inline">
<label>
<input type="checkbox"/>
Other
</label>
<input type="email" placeholder="someone#somewhere.com" class="form-control"/>
</div>
</div>
Working Example
Here's one way to achieve what you're trying to do. The only change which I've made to your HTML structure is that I've wrapped the description for each checkbox inside a <span> tag and added used this CSS:
input[type="checkbox"] {
position: relative !important;
display: inline-block;
vertical-align: text-bottom;
}
span {
display: inline-block;
vertical-align: middle;
}
Here's a working demo:
input[type="checkbox"] {
position: relative !important;
display: inline-block;
vertical-align: text-bottom;
}
span {
display: inline-block;
vertical-align: middle;
}
.form-inline .form-control {
padding-top: 0px;
padding-bottom: 0px;
height: 20px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="form-group">
<label class="col-sm-2 control-label">Favourite food</label>
<div class="col-sm-10">
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Apple</span>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Banana</span>
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Other</span>
<input type="email" placeholder="someone#somewhere.com" class="form-control" />
</fieldset>
</label>
</div>
<div class="checkbox">
<label>
<fieldset class="form-inline">
<input type="checkbox" /> <span>Any fruit from one of these colour groups: Red, Green, Blue</span>
</fieldset>
</label>
</div>
</div>
</div>

Bootstrap Glyph Icon Textfield

I want to add a Textfield input-group-addon. The problem is that when I use this class with a glyphicon the icon is not positioned right next to textfield see the image below.
Maybe someone has some hints for me - why is that?
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="name2"></label>
<div class="col-md-4">
<input id="name2" name="name2" type="text" placeholder="" class="form-control input-md">
</div>
</div>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Geschlecht</label>
<div class="col-md-4">
<label class="radio-inline" for="gender-0">
<input type="radio" name="gender" id="gender-0" value="1" checked="checked">
männlich
</label>
<label class="radio-inline" for="gender-1">
<input type="radio" name="gender" id="gender-1" value="2">
weiblich
</label>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label" for="gender">Geburtsdatum</label>
<div class="col-md-4">
<div class='input-group date' id='datetimepicker2'>
<input type='text' class="form-control" />
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker2').datetimepicker({
language: 'de'
});
});
</script>
</div>
</div>
Your code works fine with Bootstrap only (see this fiddle http://jsfiddle.net/WNAB8/1/)
The problem is in your own css. Use developer tools (F12) to find out what gives margin-right for the input or margin-left for the addon. Or optionally paste in your custom css so we can help you.
Update:
It definitely is because of given max-width for inputs. If for some reason you want to use max-width anyway, one solution is to give the max-width to .input-group:
.input-group {
max-width: 280px;
}
Demo: http://jsfiddle.net/WNAB8/5/
In your fiddle you are overriding bootstrap's textarea maxwidth option. Remove this from CSS:
/* Set width on the form input elements since they're 100% wide by default */
input,
select,
textarea {
max-width: 280px;
}
Working example
If you want to change the width of your text areas, change it using the form layout. From your layout you should look at the horizontal form and width can be changed by placing the input into a div controlled with the col-*:
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>

Label on the left side instead above an input field

I would like to have the labels not above the input field, but on the left side.
<form method="post" action="" role="form" class="form-inline">
<div class="form-group">
<label for="rg-from">Ab: </label>
<input type="text" id="rg-from" name="rg-from" value="" class="form-control">
</div>
<div class="form-group">
<label for="rg-to">Bis: </label>
<input type="text" id="rg-to" name="rg-to" value="" class="form-control">
</div>
<div class="form-group">
<input type="button" value="Clear" class="btn btn-default btn-clear">
<input type="submit" value="Los!" class="btn btn-primary">
</div>
</form>
This code gives me:
I would like to have:
You can use form-inline class for each form-group :)
<form>
<div class="form-group form-inline">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
</form>
Put the <label> outside the form-group:
<form class="form-inline">
<label for="rg-from">Ab: </label>
<div class="form-group">
<input type="text" id="rg-from" name="rg-from" value="" class="form-control">
</div>
<!-- rest of form -->
</form>
The Bootstrap 3 documentation talks about this in the CSS documentation tab in the section labelled "Requires custom widths", which states:
Inputs, selects, and textareas are 100% wide by default in Bootstrap.
To use the inline form, you'll have to set a width on the form
controls used within.
If you use your browser and Firebug or Chrome tools to suppress or reduce the "width" style, you should see things line up they way you want. Clearly you can then create the appropriate CSS to fix the issue.
However, I find it odd that I need to do this at all. I couldn't help but feel this manipulation was both annoying and in the long term, error prone. Ultimately, I used a dummy class and some JS to globally shim all my inline inputs. It was small number of cases, so not much of a concern.
Nonetheless, I too would love to hear from someone who has the "right" solution, and could eliminate my shim/hack.
Hope this helps, and props to you for not blowing a gasket at all the people that ignored your request as a Bootstrap 3 concern.
You can create such form where label and form control are side using two method -
1. Inline form layout
<form class="form-inline" role="form">
<div class="form-group">
<label for="inputEmail">Email</label>
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
<div class="form-group">
<label for="inputPassword">Password</label>
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
<button type="reset" class="btn btn-default">Reset</button>
<button type="submit" class="btn btn-primary">Login</button>
</form>
2. Horizontal Form Layout
<form class="form-horizontal">
<div class="row">
<div class="col-sm-4">
<div class="form-group">
<label for="inputEmail" class="control-label col-xs-3">Email</label>
<div class="col-xs-9">
<input type="email" class="form-control" id="inputEmail" placeholder="Email">
</div>
</div>
</div>
<div class="col-sm-5">
<div class="form-group">
<label for="inputPassword" class="control-label col-xs-3">Password</label>
<div class="col-xs-9">
<input type="password" class="form-control" id="inputPassword" placeholder="Password">
</div>
</div>
</div>
<div class="col-sm-3">
<button type="reset" class="btn btn-default">Reset</button>
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>
You can check out this page for more information and live demo - http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/bootstrap-forms.php
Like this
DEMO
HTML
<div class="row">
<form class="form-inline">
<fieldset>
<label class="control-label"><strong>AB :</strong></label>
<input type="text" class="input-mini" >
<label class="control-label"><strong>BIS:</strong></label>
<input type="text" class="input-mini" >
<input type="button" value="Clear" class="btn btn-default btn-clear">
<input type="submit" value="Los!" class="btn btn-primary">
</fieldset>
</form>
</div>
I had the same problem, here is my solution:
<form method="post" class="form-inline form-horizontal" role="form">
<label class="control-label col-sm-5" for="jbe"><i class="icon-envelope"></i> Email me things like this: </label>
<div class="input-group col-sm-7">
<input class="form-control" type="email" name="email" placeholder="your.email#example.com"/>
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">Submit</button>
</span>
</div>
</form>
here is the Demo
You can see from the existing answers that Bootstrap's terminology is confusing. If you look at the bootstrap documentation, you see that the class form-horizontal is actually for a form with fields below each other, i.e. what most people would think of as a vertical form. The correct class for a form going across the page is form-inline. They probably introduced the term inline because they had already misused the term horizontal.
You see from some of the answers here that some people are using both of these classes in one form! Others think that they need form-horizontal when they actually want form-inline.
I suggest to do it exactly as described in the Bootstrap documentation:
<form class="form-inline">
<div class="form-group">
<label for="nameId">Name</label>
<input type="text" class="form-control" id="nameId" placeholder="Jane Doe">
</div>
</form>
Which produces:
You must float left all elements like so:
.form-group,
.form-group label,
.form-group input { float:left; display:inline; }
give some margin to the desired elements :
.form-group { margin-right:5px }
and set the label the same line height as the height of the fields:
.form-group label { line-height:--px; }
I think this is what you want, from the bootstrap documentation "Horizontal form
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. Doing so changes .form-groups to behave as grid rows, so no need for .row". So:
<form class="form-horizontal" role="form">
<div class="form-group">
<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="inputPassword3" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label>
<input type="checkbox"> Remember me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default">Sign in</button>
</div>
</div>
</form>
Fiddle: http://jsfiddle.net/beewayne/B9jj2/29/
You can use a span tag inside the label
<div class="form-group">
<label for="rg-from">
<span>Ab:</span>
<input type="text" id="rg-from" name="rg-from" value="" class="form-control">
</label>
</div>
I managed to fix my issue with. Seems to work fine and means I dont have to add widths to all my inputs manually.
.form-inline .form-group input {
width: auto;
}
I am sure you would've already found your answer... here is the solution I derived at.
That's my CSS.
.field, .actions {
margin-bottom: 15px;
}
.field label {
float: left;
width: 30%;
text-align: right;
padding-right: 10px;
margin: 5px 0px 5px 0px;
}
.field input {
width: 70%;
margin: 0px;
}
And my HTML...
<h1>New customer</h1>
<div class="container form-center">
<form accept-charset="UTF-8" action="/customers" class="new_customer" id="new_customer" method="post">
<div style="margin:0;padding:0;display:inline"></div>
<div class="field">
<label for="customer_first_name">First name</label>
<input class="form-control" id="customer_first_name" name="customer[first_name]" type="text" />
</div>
<div class="field">
<label for="customer_last_name">Last name</label>
<input class="form-control" id="customer_last_name" name="customer[last_name]" type="text" />
</div>
<div class="field">
<label for="customer_addr1">Addr1</label>
<input class="form-control" id="customer_addr1" name="customer[addr1]" type="text" />
</div>
<div class="field">
<label for="customer_addr2">Addr2</label>
<input class="form-control" id="customer_addr2" name="customer[addr2]" type="text" />
</div>
<div class="field">
<label for="customer_city">City</label>
<input class="form-control" id="customer_city" name="customer[city]" type="text" />
</div>
<div class="field">
<label for="customer_pincode">Pincode</label>
<input class="form-control" id="customer_pincode" name="customer[pincode]" type="text" />
</div>
<div class="field">
<label for="customer_homephone">Homephone</label>
<input class="form-control" id="customer_homephone" name="customer[homephone]" type="text" />
</div>
<div class="field">
<label for="customer_mobile">Mobile</label>
<input class="form-control" id="customer_mobile" name="customer[mobile]" type="text" />
</div>
<div class="actions">
<input class="btn btn-primary btn-large btn-block" name="commit" type="submit" value="Create Customer" />
</div>
</form>
</div>
You can see the working example here... http://jsfiddle.net/s6Ujm/
PS: I am a beginner too, pro designers... feel free share your reviews.
No CSS required. This should look fine on your page. You can set col-md-* as per your needs
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
<div class="row">
<div class="col-md-12">
<form class="form-inline" role="form">
<div class="col">
<div class="form-group">
<label for="inputEmail" class="col-sm-3">Email</label>
<input type="email" class="form-control col-sm-7" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="col">
<div class="form-group">
<label for="inputPassword" class="col-sm-3">Email</label>
<input type="password" class="form-control col-sm-7" id="inputPassword" placeholder="Email">
</div>
</div>
<button class="btn btn-primary">Button 1</button>
<button class="btn btn-primary">Button 2</button>
</form>
</div>
</div>
<div class="control-group">
<label class="control-label" for="firstname">First Name</label>
<div class="controls">
<input type="text" id="firstname" name="firstname"/>
</div>
</div>
Also we can use it Simply as
<label>First name:
<input type="text" id="firstname" name="firstname"/>
</label>
It seems adding style="width:inherit;" to the inputs works fine.
jsfiddle demo

Resources