In my form I've got fields with labels and fields without a label (i.e. a large textarea). I've given both my label and input fields a background color. Currently, the left margin of the labels differs from the left margin of the input fields.
The documentation says:
Content should be placed within columns, and only columns may be
immediate children of rows.
But the horizontal form example puts a <label> element inside a column:
<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>
The <label> element is a column, but it is content itself as well:
An easy way to have the same left margin for labels and inputs is to put the label inside a column, instead of being a column as well. Is that the best solution, or is there another way to have both labels and inputs have the same left margin?
What the documentation says:
Content should be placed within columns, and only columns may be
immediate children of rows.
It has to be interpreted in context of row. It means you don't put content directly in a row but use columns to layout the content. This is because, bootstrap adds a standard 15px padding on columns (first and last offsetted) to do a clean layout.
Further down the documentation says this:
Columns create gutters (gaps between column content) via padding. That
padding is offset in rows for the first and last column via negative
margin on .rows
Note: Now this padding is important in your use-case.
It doesn't really matter if you style your labels with col-xx- classes or wrap them in a div and apply col-xx- class to the wrapping div. Net effect of layout remains same.
You have provided a background-color for your labels and which is exactly why you are seeing a difference. See, when there is a label with col-xx- style applied, it behaves like a bootstrap column i.e. it gets 15px padding. So the label-text starts at 15px padding even though the label itself starts at the edge. However, when you wrap your label in a div and apply col-xx- style to the wrapping div, then this div gets the padding and the label starts after this padding.
This difference is what is manifesting itself visibly in your design.
This snippet will make it clearer: (Notice the "Email" label which is a label with col-xx- i.e. label styled as bootstrap column. The "Password" label however is wrapped inside a div which as col-xx- style applied. Notice the difference due to padding.)
div.form-horizontal { border: 1px solid gray; }
textarea { width: 100%; }
label { background-color: silver; }
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<br/><br/>
<div class="row">
<div class="form-horizontal col-xs-offset-2 col-xs-8 ">
<div class="form-group">
<label class="col-xs-12">Definitie</label>
<div class="col-xs-12">
<textarea></textarea>
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-xs-2 control-label">Email</label>
<div class="col-xs-10">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email" />
</div>
</div>
<div class="form-group">
<div class="col-xs-2">
<label for="inputPassword3" class="control-label">Password</label>
</div>
<div class="col-xs-10">
<input type="password" class="form-control" id="inputPassword3" placeholder="Password" />
</div>
</div>
</div>
</div>
Here is a fiddle for you to play with: http://jsfiddle.net/abhitalks/gkkwevnz/
.
Related
In Bootstrap, container-fluid has some padding, which I want. However, things inside a horizontal form seem to be ignoring the padding and getting pushed all the way to the edges of the container (I've added a border to the container here for illustration):
<div class="container-fluid" style="max-width:900px;border:1px solid black">
<div class="alert alert-danger">Correct, obeys container-fluid padding.</div>
<form class="form form-horizontal">
<div class="form-group alert alert-danger">Too Wide</div>
<div class="form-group panel panel-default">
<div class="panel-heading">Too Wide</div>
<div class="panel-body">Body</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Too far left</button>
</div>
</form>
</div>
A working example is on Bootply.
In that example, the first alert is as intended. Everything else inside the form is too wide.
Now, not shown in the Bootply, but if I add an input to the form:
<div class="form-group">
<label class="control-label col-sm-2" for="field">Label</label>
<div class="col-sm-10">
<input class="form-control" id="field" type="text"/>
</div>
</div>
The input is padded correctly, unlike the alerts and panels, which I don't understand.
How do I get all the things in the form to obey the padding? This is especially important to me because on small screens it pushes everything right to the edge and doesn't look that great.
The only thing I could think of to try was enclosing the form in a plain div, which had no effect.
I also achieved some success by manually setting the padding on the form, but that doesn't feel right, and it also breaks the properly padded input elements. Plus, it's not too robust in that I can't guarantee my hard-coded padding will match the container's usual padding which I have no control over.
//This will sort out your panels and alerts. (.less code)
//Or you could just put a .col-xs-12 on them.
.form-horizontal {
> .panel,
> .alert {
margin: 0 #grid-gutter-width / 2; //(or just 15px if your using bootstrap dist)
}
}
Then for your input groups, just using the col-sm-2 on your labels like you have done above.
And for your form group with the submit button simple put a col-xs-12 on it.
The reason for this is: .form-groups inside .form-horizontal receive margin-left: -15px; (The same as grid-gutter-width).
The intended design is that you use form-horizontal as a substitute for a .row and then use .cols inside. Or implement how you choose to fit your design.
Reference : bootstrap forms horizontal
Do your inputs like this
<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>
Do your submit button like this
<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>
You can do your alerts like this if you like
<div class="col-xs-12">
<!-- alert here -->
</div>
Here is my partial bootstrap code. The form lets you search a person by first name and last name:
<div style="text-align:right">
<div class="form-inline">
<div class="form-group">
<label for="Search">Search</label>
</div>
<div class="form-group">
<input class="form-control text-box single-line" id="FirstName" name="FirstName" placeholder="First Name" type="text" value="" />
</div>
<div class="form-group">
<input class="form-control text-box single-line" id="LastName" name="LastName" placeholder="Last Name" type="text" value="" />
</div>
<div class="form-group">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</div>
The topmost div helps the set of inner controls to be shown on the right side of the web page (as desired).
When the viewport is wide enough, all the controls are displayed in one line as they are supposed to be.
However, when the viewport width is reduced, the text-box controls line up vertically (as expected) but the "Search" label and the "search" button stay on the right of the page.
Why are some elements aligning vertically while some others are not?
This does not happen if I remove the topmost div. However, I do need it to display the controls on the right side of the page.
I am wondering if there is a way to make all the controls in the set work consistently. Regards.
Use the * selector to choose all elements within the div form-inline
.form-inline *
{
/* Desired style here */
}
Best way to do it is by using the * selector, this says that the style will be applied to all the elements within the div class form-inline
.form-inline *
{
/* Place your styling stuff here */
}
Setting left-align style on label as well as the button solved my problem:
<div class="form-group" style="text-align:right">
<label for="Search">Search</label>
</div>
Now, when the viewport is wide enough, all the elements are shown in one line on the right side of the view port. When the viewport reduces in width, all the controls are shown vertically as left-aligned.
I need my 2 inputs which placed inline to have width of parent div which defines col-md-6 and col-md-3. Right now inputs are aligned correctly but have width which in one case less than col-md-6 and in second case more than col-md-3. How to make this inputs have width col-md-6 and col-md-3 correspondingly?
<div class="col-md-9">
<form action="" role="form" class="form-inline">
<div class="col-md-6">
<div class="form-group">
<label for="iR" class="sr-only">i R</label>
<input type="text" class="form-control" id="iR" placeholder="some mid long explanation" />
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<label for="iS" class="sr-only">i S</label>
<input type="text" class="form-control" id="iS" placeholder="some mid long explanation 2" />
</div>
</div>
</div>
As others have mentioned, you need to specify custom widths with inline forms. From the docs:
Inputs and selects have width: 100%; applied by default in Bootstrap. Within inline forms, we reset that to width: auto; so multiple controls can reside on the same line. Depending on your layout, additional custom widths may be required.
In your case, you need to specify width: 100% for both the input and the form-group:
<div class="form-group" style="width: 100%">
and
<input type="text" class="form-control" id="iR" placeholder="some mid long explanation" style="width: 100%" />
Working example in JSFiddle
Just put style="width: 100%;" on the input; Btw, you are missing a form closing tag.
Also if you look at bootstraps for any elements you set up most of the time they leave a bit of margins. So if you don't want any margins just change the margins to 0.
I am using Bootstrap 3, and coding mainly for mobile websites. I am having trouble centering content when I don't want it to take up the whole line. When the screen is larger than a certain width, it doesn't get centered, & when it is smaller than that width (especially at the size we are optimizing it for), it won't even come away from the left border of the page. Also, some elements keep jumping in size when they get to be a certain size. I assume that last one is normal behavior, but can't for the life of me figure out why.
HTML:
<div class="col-xs-12">
<div class="container">
<div class="form-group col-xs-11">
<div class="col-xs-5 pull-left">
<input type="text" class='form-control' id="firstName" name="firstName" placeholder="First Name" required />
</div>
<div class="col-xs-5 pull-right">
<input type="text" class='form-control' id="lastName" name="lastName" placeholder="Last Name" required />
</div>
</div>
</div>
<div class="form-group">
<input type="email" class='form-control' id="email" name="email" placeholder="Email" required />
</div>
</div>
The #media's in bootstrap-custom.css are:
#media (min-width 1200)
.container {
max-width: 1140px;
}
#media (min-width 992)
.container {
max-width: 940px;
}
#media (min-width 768)
.container {
max-width: 720px;
}
.container {
margin-right: auto;
margin-left: auto;
padding-left: 0;
padding-right: 0;
}
I'm not sure if that's the right/accepted way to quote them.
I also have the rest of the bootstrap-custom.css, if you need that for your answer.
P.S. The whole reason I didn't have the first row (with firstName & lastName) take up the whole line is because I don't want it to. I wanted that row to be in the middle with space on either side.
P.P.S. Here is a fiddle.
Disclaimer: I haven't done any Bootstrap before - I'm just stabbing in the dark.
For a simple two column layout, create a .row and add the appropriate number of .span* columns. As this is a 12-column grid, each .span* spans a number of those 12 columns, and should always add up to 12 for each row (or the number of columns in the parent).
So, I changed your col-xs-5 elements to col-xs-6 so that they would add up to twelve, and therefore take up 100% of the row's width together. I moved your email input field into the .container and added the col-xs-11 class in order for it to mirror the other form-group's left and right margin. I wrapped your email input field in a div (as you had done with the other fields), and added the col-xs-12 class to make it stretch to 100% of its parent row's width.
Here is a fiddle.
Also, some elements keep jumping in size when they get to be a certain
size. I assume that last one is normal behavior, but can't for the
life of me figure out why.
Here is an updated fiddle that employs CSS3 transitions to ease between your different max-widths.
Have you tried text-align:centre; to your container class?
I think you need to change your markup a little. Wrap everything inside a container and make your form-group as rows..
For example:
<div class="container">
<div class="col-xs-12">
<div class="row form-group">
<div class="col-xs-6">
<input type="text" class="form-control" id="firstName" name="firstName" placeholder="First Name" required="">
</div>
<div class="col-xs-6 pull-right">
<input type="text" class="form-control" id="lastName" name="lastName" placeholder="Last Name" required="">
</div>
</div>
<div class="row form-group">
<div class="col-xs-12">
<input type="email" class="form-control" id="email" name="email" placeholder="Email" required="">
</div>
</div>
</div>
</div>
Working demo: http://bootply.com/86294
P.S. - Take a look at Option #2 in the demo so that your inputs don't get too small. This will allow them to stack vertically on the smallest screens.
I'm using Bootstrap for the first time, and am having a lot of trouble aligning this form-horizontal to the left.
The list items are horizontal, as they should be, but I want the control-labels (the Bootstrap class for form labels) to all be at the same position floated left.
The form is contained in a div with a span of 7, as my site is two columns -- 7 and 4 columns.
Below is my HTML. If you look under "Horizontal Forms" on this page http://accounts.tao.tw.shuttle.com/examples_bootstrap/basecss/forms, you'll see that the labels are left aligned, but not absolutely left-aligned so that the beginning of the labels are at the same position vertically.
<form class="form-horizontal">
<div class="control-group">
<label class="control-label" for="inputSaving">What are you saving for?</label>
<div class="controls">
<input class="span4" type="text" id="inputSaving" placeholder="e.g. Swimming Lessons, Birthday Party, College Fund, etc.">
</div>
</div>
<div class="control-group">
<label class="control-label" for="description">Add a short description</label>
<div class="controls">
<textarea class="span4" rows="4" placeholder="Describe in your own words the saving goal for this piggybank"></textarea>
</div>
</div>
<div class="control-group">
<label class="control-label" for="categoryselect">Choose a Category</label>
<div class="controls">
<select class="span4">
<!-- Add some CSS and JS to make a placeholder value-->
<option value="Kittens">Kittens</option>
<option value="Keyboard Cat">Keyboard Cat</option>
<option value="Twitter Bird">Twitter Bird</option>
</select>
</div>
</div>
<div class="control-group">
<label class="control-label" for="goal">Your Saving Goal</label>
<div class="controls">
<input type="text" class="span4" id="goal">
</div>
</div>
<button class="btn btn-large btn-primary" type="button">Submit</button>
</form>
Just my two cents. If you are using Bootstrap 3 then I would just add an extra style into your own site's stylesheet which controls the text-left style of the control-label.
If you were to add text-left to the label, by default there is another style which overrides this .form-horizontal .control-label. So if you add:
.form-horizontal .control-label.text-left{
text-align: left;
}
Then the built in text-left style is applied to the label correctly.
If you are saying that your problem is how to left align the form labels, see if this helps:
http://jsfiddle.net/panchroma/8gYPQ/
Try changing the text-align left / right in the CSS
.form-horizontal .control-label{
/* text-align:right; */
text-align:left;
background-color:#ffa;
}
Instead of altering the original bootstrap css class create a new css file that will override the default style.
Make sure you include the new css file after including the bootstrap.css file.
In the new css file do
.form-horizontal .control-label{
text-align:left !important;
}
"pull-left" is what you need, it looks like you are using Bootstrap 2, I am not sure if that is available, consider bootstrap 3, unless ofcourse it is a huge rework! ... for Bootstrap 3 but you need to make sure you have 12 columns in each row as well, otherwise you will have issues.
Just add style="text-align: left" to your label.
text-align:left; did not work for me but by adding display:flex; I was able to get the label to the left.
I was having the exact same problem. I found the issue within bootstrap.min.css. You need to change the label:
label {display:inline-block;} to label {display:block;}