Bootstrap horizontal form is ignoring container-fluid padding - html

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>

Related

CSS - Align big Element right of multiple smaller Rows

I have a form with multiple inputs.
All of those inputs are Dropdowns, Checkboxes, short Texts, etc. Except one which can be come quite large (textarea). I want all the smaller Inputs on the left side of the Page and the textarea on the right side of the Page.
The Textarea is supposed to have the same height as all the elements on the right side and take roughly half of the Pages width.
My problem is now that I am unable to properly add the Textarea to the right Side. When I use "float:right" then it either has an huge gap between the smaller Elements and itself, or it is small and when I make it bigger then the small Elements become smaller. Or it has the correct size, but then it only has one Element on the left side and every other Element comes after the Textarea has ended.
<form method="post">
<input asp-for="ID" type="hidden" />
<div asp-validation-summary="All" class="text-danger"></div>
<div class="row " >
<div class="form-group col-md-6">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group col-md-5">
<label asp-for="Comment" class="control-label"></label>
<textarea asp-for="Comment" rows="14" class="form-control" oninput='this.style.height = "";this.style.height = this.scrollHeight + 3 + "px"' height:"100vh">
</textarea>
<span asp-validation-for="Comment" class="text-danger"></span>
</div>
</div>
<div class="row">
<div class="form-group col-md-3">
.....
</div>
<div class="form-group col-md-3">
....
</div>
</div>
<div class="row">
#if (Model.Valid is not null)
{
<div class="form-group col-md-3">
....
</div>
}
<div class="form-group col-md-3">
....
</div>
</div>
<div class="row">
......
<hr />
.....
So, all the smaller Elements are inside the "form-group", which are mostly inside "row". And on the right side I now want the Comment TextArea.
I would prefer a plain Bootstrap Solution (especially since everything else is already working in Bootstrap), but I am open to everything.

Bootstrap grid framwork does not work with form-control

I am using Bootstraps grid system to make my input box smaller. However, it seems to conflict with form-control.
HTML:
<form class="col-md-8">
<div class="form-group">
<input type="text" name="input_box" class="form-control col-md-3"/>
</div>
</form>
In Chrome's developer tools, the 25% (for .col-md-3 is automatically crossed out). If I take out .form-control then it works but looks ugly.
How do I use them both? Note that I do not want to change the width in .formcontrol because I have other forms that also use this and will get messed up
How about this
<form>
<div class="col-md-8">
<div class="form-group col-md-3">
<input type="text" name="input_box" class="form-control"/>
</div>
</div>
</form>

Label of the control is jumping to the right side when resized

The generated HTML I have is this:
I put it in Bootply so you can just see and play with it easier, notice it has a small CSS section too:
http://www.bootply.com/NrxiDfZJdC
Problem is it is not "bootstrappy" enough! If you start making the window smaller the labels jump to the right side of the control.
What have I done that has caused this issue?
You have a couple of major problems here.
Right Alignment:
You have set this by adding .text-right on your labels. Obviously this was meant for the desktop view only. Take it off of your labels and use a min-width media query to set the alignment or override the alignment with max-width at small resolutions
Overflowing text boxes:
You didn't use a row. You should most always use a row because it corrects the padding wit negative left and right margins. You tried to fix this yourself by instead adding a class that removes the padding on the .col-sm-4. The padding is there for a reason and should not be removed. Even adding in the row and removing the .multi-row doesn't completely correct the issue, however. When you do that you run into the text inputs being too wide. That is because you added the 100% width to the inputs. This is not a bad thing per se, but it causes problems because you have used spans for your inner columns. spans are naturally collapsed in width. They don't fill their parents' container like divs do. Swap them for divs.
Weird "Ext" label:
This is because you added a margin-left: 85% to the label to simulate the right alignment that the others have. Just remove that margin and add text-right to this label like you have on all the other similar labels.
No padding:
After all that, you'll have no padding on smaller resolutions. Add a .container around your form.
In the end, you should have this: http://www.bootply.com/uiPpBytre3
Demo:
#import url(https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css);
.form-input{
width :100%;
}
#media(max-width: 768px) {
.form-group .text-right {
text-align: left;
}
}
<div class="container">
<form class="form-horizontal" role="form">
<br>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Name">Name</label></div>
<div class="col-sm-4"><select class="form-control" id="nameadmin" name="nameadmin"><option>77881</option>
<option>77882</option>
<option>77883</option>
<option>77884</option>
<option>77885</option>
</select></div>
<div class="col-sm-4 col-sm-offset-1">
<div>
<input class="checkbox-inline" id="ShowEmailInFooter" name="ShowEmailInFooter" type="checkbox" value="true"><input name="ShowEmailInFooter" type="hidden" value="false">
<label class="control-label" for="Show_Email_in_Footer">Show Email in Footer</label>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Email">Email</label></div>
<div class="col-sm-4">
<input id="AdminEmail" name="AdminEmail" style="width:100%;padding-right:30px;" type="text" value="">
<span class="glyphicon glyphicon-envelope form-control-feedback" style="right: 10px; line-height: 27px; color: lightblue"></span>
</div>
<div class="col-sm-4 col-sm-offset-1">
<div>
<input class="checkbox-inline" id="ShowAdminPhone" name="ShowAdminPhone" type="checkbox" value="true"><input name="ShowAdminPhone" type="hidden" value="false">
<label class="control-label" for="Show_Admin_phone">Show Admin phone</label>
</div>
</div>
</div>
<div class="row form-group">
<div class="col-sm-offset-2 col-sm-1 text-right"><label class="control-label" for="Phone">Phone</label></div>
<div class="col-sm-4">
<div class="row">
<div class="col-sm-5"><input class="form-input" id="AdminPhone" name="AdminPhone" type="text" value=""></div>
<div class="col-sm-2 text-right"><label class="control-label" for="Ext">Ext</label></div>
<div class="col-sm-5"><input class="form-input" id="AdminExt" name="AdminExt" type="text" value=""></div>
</div>
</div>
</div>
</form>
</div>
Add responsive text align class text-sm-right instead of text-right
#media (min-width: #screen-sm-min) {
.text-sm-right { text-align: right; }
}
bootply

How to make the input full-length in a column in Bootstrap?

Bootply codes can be seen here => http://www.bootply.com/QpvisrtAJR
I want the input box to be longer, however, the width:100% doesn't work for it.. And I don't want to use width: xxxpx or size=xxx to make it longer because it will be un-responsive in different resolution..
Does anyone have ideas about this?
Your input rule isn't actually being applied. It is not specific enough so is being overwritten by a default bootstrap rule. Try this instead:
.form-inline button.form-control,
#contain_word
{
display: block;
width: 100%;
}
http://www.bootply.com/Qh2VwydnHx
Also you have a an erroneous character in your html where you give the input field an id. Should be:
<input type="email" class="form-control" id="contain_word">
Not:
<input type="email" class="form-control" id="contain_word`">
You can use the calc() method to have the input field 100% in width but still float left to the label.
Updated Bootply: http://www.bootply.com/2K3ZIWsuWy
Calc() is compatible with most browsers except Opera Mini. For Blackberry you still need -webkit.
Check out the compatibility table here: http://caniuse.com/calc
You can add your class with a specified width
or override existing styles, but it is better to create your own style file
http://www.bootply.com/QpvisrtAJR#
div class="row">
<form class="form-inline" role="form">
<div class="form-group col-sm-6 no-padding">
<label for="contain_word`">Containing word(s): </label>
<input class="form-control test" id="contain_word`" type="email">
</div>
<div class="col-sm-2">
<button class="btn btn-primary form-control">Search</button>
</div>
<div class="col-sm-2">
<button class="btn btn-primary form-control">Clear</button>
</div>
</form>
</div>

Bootstrap full-width text-input within inline-form

I am struggling to create a textbox that fits the entire width of my container area.
<div class="row">
<div class="col-md-12">
<form class="form-inline" role="form">
<input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
<button type="submit" class="btn btn-lg">Search</button>
</form>
</div>
</div>
When I do the above, the two form elements are in-line, as I expect, but don't take up more than a few columns, at best. Hovering over the col-md-12 div in firebug shows it taking up the expected full width. It's just the text input that doesn't seem to fill. I even tried adding an in-line width value but it didn't change anything. I know this should be simple, just feeling really dumb now.
Here is a fiddle: http://jsfiddle.net/52VtD/4119/embedded/result/
EDIT:
The selected answer is thorough in every way and a wonderful help. It's what I ended up using. However I think my initial issue was actually a problem with the default MVC5 template within Visual Studio 2013. It contained this in Site.css:
input,
select,
textarea {
max-width: 280px;
}
Obviously that was blocking the text-input from expanding appropriately... Fair warning to future ASP.NET template users...
The bootstrap docs says about this:
Requires custom widths 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.
The default width of 100% as all form elements gets when they got the class form-control didn't apply if you use the form-inline class on your form.
You could take a look at the bootstrap.css (or .less, whatever you prefer) where you will find this part:
.form-inline {
// Kick in the inline
#media (min-width: #screen-sm-min) {
// Inline-block all the things for "inline"
.form-group {
display: inline-block;
margin-bottom: 0;
vertical-align: middle;
}
// In navbar-form, allow folks to *not* use `.form-group`
.form-control {
display: inline-block;
width: auto; // Prevent labels from stacking above inputs in `.form-group`
vertical-align: middle;
}
// Input groups need that 100% width though
.input-group > .form-control {
width: 100%;
}
[...]
}
}
Maybe you should take a look at input-groups, since I guess they have exactly the markup you want to use (working fiddle here):
<div class="row">
<div class="col-lg-12">
<div class="input-group input-group-lg">
<input type="text" class="form-control input-lg" id="search-church" placeholder="Your location (City, State, ZIP)">
<span class="input-group-btn">
<button class="btn btn-default btn-lg" type="submit">Search</button>
</span>
</div>
</div>
</div>
have a look at something like this:
<form role="form">
<div class="row">
<div class="col-xs-12">
<div class="input-group input-group-lg">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="submit" class="btn">Search</button>
</div><!-- /btn-group -->
</div><!-- /input-group -->
</div><!-- /.col-xs-12 -->
</div><!-- /.row -->
</form>
http://jsfiddle.net/n6c7v/1/
As stated in a similar question, try removing instances of the input-group class and see if that helps.
refering to bootstrap:
Individual form controls automatically receive some global styling.
All textual , , and elements with
.form-control are set to width: 100%; by default. Wrap labels and
controls in .form-group for optimum spacing.
Try something like below to achieve your desired result
input {
max-width: 100%;
}
You can use flex-fill class for input
<div class="row">
<div class="col-md-12">
<form class="form-inline" role="form">
<input type="text" class="form-control input-lg flex-fill" id="search-church" placeholder="Your location (City, State, ZIP)">
<button type="submit" class="btn btn-lg">Search</button>
</form>
</div>
With Bootstrap >4.1 it's just a case of using the flexbox utility classes. Just have a flexbox container inside your column, and then give all the elements within it the "flex-fill" class. As with inline forms you'll need to set the margins/padding on the elements yourself.
.prop-label {
margin: .25rem 0 !important;
}
.prop-field {
margin-left: 1rem;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-12">
<div class="d-flex">
<label class="flex-fill prop-label">Label:</label>
<input type="text" class="flex-fill form-control prop-field">
</div>
</div>
</div>
I know that this question is pretty old, but I stumbled upon it recently, found a solution that I liked better, and figured I'd share it.
Now that Bootstrap 5 is available, there's a new approach that works similarly to using input-groups, but looks more like an ordinary form, without any CSS tweaks:
<div class="row g-3 align-items-center">
<div class="col-auto">
<label>Label:</label>
</div>
<div class="col">
<input class="form-control">
</div>
<div class="col-auto">
<button type="button" class="btn btn-primary">Button</button>
</div>
</div>
The col-auto class makes those columns fit themselves to their contents (the label and the button in this case), and anything with a col class should be evenly distributed to take up the remaining space.