How can I use .input-append with a .form-inline? - css

I am new to Twitter Bootstrap and am starting to fumble my way through its use. While certain aspects of the framework are starting to make sense I am still struggling with form styling. I am trying to setup several different sections of a form which will have elements that are styled utilizing .form-inline. In one such instance I am also attempting to use .input-append with little luck.
<div class="row">
<div class="well span12">
<div class="row">
<div class="span12 form-inline input-append">
<label for="assetSearch">Asset Search</label>
<input type="search" id="assetSearch" placeholder="">
<span class="add-on"><i class="icon-search"></i></span>
</div>
</div>
<div class="row">
<div class="span12 form-inline">
<label for="service">Service</label>
<input type="text" id="service" autocomplete="off" />
</div>
</div>
</div>
</div>
The above markup renders like this:
As you can see "Asset Search" is not inline with the form input. If i remove the .input-append class from the containing div things line up. However, the search icon is no longer embedded in the text box, but instead to the right of text box.
How can I use .form-inline in cunjunction with .input-append?

You should not put inside a input-append (or prepend) anything else than inputs, buttons or .add-ons (this might not be exhaustive).
Try wrapping the whole thing into a div.input-append and let the .form-inline handle the floating : Demo on jsfiddle
<div class="span12 form-inline">
<label for="assetSearch">Asset Search</label>
<div class="input-append">
<input type="search" id="assetSearch" placeholder="" />
<span class="add-on"><i class="icon-search"></i></span>
</div>
</div>

Here's a fiddle of working alignment: http://jsfiddle.net/Jeff_Meadows/xGRtL/
The two fixes are to set vertical-align of <label> elements inside elements of class .input-append, and to reset the font-size of your element to 14px (it's set to 0 somewhere else in bootstrap). Rather than create a rule based on .input-append, I created a new class that you can add to your containing element. That way, you won't get unexpected results elsewhere.
.input-prepend label, .input-append label {
vertical-align: middle;
}
.fix-text-spacing {
font-size: 14px;
}

Related

bootstrap hides my field or it reduce its width

I have a webpage utilizing the current bootstrap. It displays a label and text input box part of a form.
If I include the class="custom-control-input" to the input text field, Bootstrap hides it. Why?
If I don't include, it shows up, but it won't fulfill the full width according to its parent div tag.
The code is:
<div class="container">
<form class="needs-validation" novalidate action="submit_transgene_entry.php" method="post">
<div class="row">
<div class="col-md-2 mb-3">
<label for="comments_fieldID1">field below is shown, but won't extend full width</label>
</div>
<div class="col-md-10 mb-3">
<input type="text" id="comments_fieldID1" name="comments_fieldName">
</div>
</div>
<div class="row">
<div class="col-md-2 mb-3">
<label for="comments_fieldID2">the following field is hidden by custom-control-input</label>
</div>
<div class="col-md-4 mb-3">
<input type="text" id="comments_fieldID2" class="custom-control-input" name="comments_fieldName">
</div>
</div>
<div class="row">
<div class="col-md-3 mb-3">
<input type="submit" name='accept_transgene_entry' class="btn btn-primary btn-lg btn-block" value="Accept Transgene Entry" alt="Accept Transgene Entry"/>
</div>
</div>
</form>
</div>
You can see the code in action at this page
You must use the form-control class instead of custom-control-input.
I've edited your fiddle:
https://jsfiddle.net/2orbj50v/
Just add this in your CSS
#comments_fieldID1 {
width:100%;
}
Reason Behind Hiding this the class "custom-control-input" have some default css value from bootstrap like this, if you change the opacity 1 instead of 0 it will show the original field.
.custom-control-input {
position: absolute;
z-index: -1;
opacity: 0;
}
to make the input field full width just add this css
#comments_fieldID1 {
width:100%;
}
That may be because bootstrap 4 provide customized form elements for
Checkbox
Radio button
Range
Select
Upload
Toggle
And for these to work you have to wrap it around a <div> with class="custom-control custom-checkbox". The following is the example if you want to use custom-checkbox. But it won't work if you replace checkbox with text
<div class="custom-control custom-checkbox mb-3">
<input type="checkbox" class="custom-control-input" id="customCheck" name="example1">
<label class="custom-control-label" for="customCheck">Custom checkbox</label>
</div>
And for your first input box, you can simply make it's with to be 100%. Like shown below:
#comments_fieldID1{
width:100%;
}
Best Answer : Use form-control for your #comments_fieldID1 element
Or you can add css in your css file :
#comments_fieldID1 {
width: 100%;
}

CSS selector priority from different stylesheet files

I have a page with a form with input and textarea elements. I am using bootstrap along with my own site.css. The site.css is before the boostrap.css in the markup. I have a css rule in the site css that looks like this:
.formcontainer input, textarea {
background-color: lightgray;
}
The form elements also have the form-control class from bootstrap applied to them. The problem is that the input elements prioritize the rule from my site css and apply the background color correctly. However, for the textarea elements the bootstrap class is prioritized over the rule from my site.css. All form elements are wrapped in the same div containers with the same clasess applied. I can't make sense of why the elements are getting the rules prioritized differently.
Here's an example of the markup where the input gets the background color but the textarea doesn't:
<div id="formpart2" class="formcontainer">
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Email</label>
<div class="input-group">
<input type="text" class="form-control invalid" fieldrequired>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Contact Instructions</label>
<div class="input-group">
<textarea type="text" class="form-control" rows="10" cols="15" fieldrequired></textarea>
</div>
</div>
</div>
The site.css is before the boostrap.css in the markup.
There's your problem. The way CSS works, rules that appear later (either within one sheet or in terms of multiple sheets being included) will overwrite rules that appear before them. Switch your markup so your custom styles come last - ie, include bootstrap.css before site.css.
(Also, bear in mind that textarea is a pretty generic selector. Did you mean .formcontainer textarea?)
The css engine always prioritize .class and #id over tag-name. The problem with your style is, your are using the tag name textarea which has a class form-control to style. So, the css engine will prioritize .form-control over tag-name. See the following examples.
Won't Work
.formcontainer input,
textarea {
background-color: lightgray;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div id="formpart2" class="formcontainer">
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Email</label>
<div class="input-group">
<input type="text" class="form-control invalid" fieldrequired>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Contact Instructions</label>
<div class="input-group">
<textarea type="text" class="form-control" rows="10" cols="15" fieldrequired></textarea>
</div>
</div>
</div>
Will Work
input.form-control,
textarea.form-control {
background-color: lightgray;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div id="formpart2" class="formcontainer">
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Email</label>
<div class="input-group">
<input type="text" class="form-control invalid" fieldrequired>
</div>
</div>
<div class="form-group">
<label class="control-label visible-ie8 visible-ie9">Contact Instructions</label>
<div class="input-group">
<textarea type="text" class="form-control" rows="10" cols="15" fieldrequired></textarea>
</div>
</div>
</div>
Hope this helps!
In Bootstrap, you might find a rule that starts something like this:
.formcontainer input,
.formcontainer textarea {
/* whatever */
}
In your attempt to overwrite that, you forgot the context for textarea:
.formcontainer input,
textarea {
background-color: lightgray;
}
This makes your rules for textarea less specific than Bootstraps's rule, so even if you load the css files in the correct order, your textarea style won't overwrite Bootstrap's textarea style (assuming you are in a .formcontainer context).
I highly recommend you learn more about CSS specificity. Very fundamental if you ever want to work with CSS.

Alternate HTML Styling in Bootstrap Horizontal Form

I have some forms which mostly consist of input controls, but there are times when the horizontal form has a variant like the following:
<div class="form-group">
<span class="col-md-3 control-label">Features</span>
<div class="col-md-9">
<span>This is not available.</span>
</div>
</div>
Or sometimes I have multiple controls:
<span class="col-md-3 control-label">Payment</span>
<div class="col-md-9">
<input value="2" name="CustomerPaymentOption" type="radio">
<span>Credit Card</span>
</div>
The label content doesn't quite line up at the same level as the control-label. I've tried to mimic the css class for form-control to get span content to line up, but it never quite worked out so well in my scenarios. Any recommendations on getting the content to line up?
If you just want to print a static text you could use a static form control (http://getbootstrap.com/css/#forms-controls-static).
If you want to use multiple checkboxes and radio buttons, you can also use what bootstrap provoides. See http://getbootstrap.com/css/#forms-controls.
Would you like to have something like this? http://www.bootply.com/ztbj1dCJrP
Its Simple add class to the div (here my-label)
<span class="col-md-3 control-label">Payment</span>
<div class="col-md-9 my-label">
<input value="2" name="CustomerPaymentOption" type="radio">
<span>Credit Card</span>
</div>
and css
.my-label{
display:inline-block;
}
fiddle http://jsfiddle.net/harshdand/a4n4nc1r/
for multiple http://jsfiddle.net/harshdand/a4n4nc1r/2/

Input widths on Bootstrap 3

Update again: I am closing this question by selecting the top answer to keep people from adding answers without really understanding the question. In reality there is no way to do it with the build in functionality without using grid or adding extra css. Grids do not work well if you are dealing with help-block elements that need to go beyond a short input for example but they are 'build-in'. If that is an issue I recommend using extra css classes which you can find in the BS3 discussion here. Now that BS4 is out it is possible to use the included sizing styles to manage this so this is not going to be relevant for much longer. Thanks all for good input on this popular SO question.
Update: This question remains open because it is about built-in functionality in BS to manage input width without resorting to grid (sometimes they have to be managed independently). I already use custom classes to manage this so this is not a how-to on basic css. The task is in BS feature discussion list and has yet to be addressed.
Original Question:
Anyone figure out a way to manage input width on BS 3? I'm currently using some custom classes to add that functionality but I may have missed some non documented options.
Current docs say to use .col-lg-x but that clearly doesn't work as it can only be applied to the container div which then causes all kinds of layout/float issues.
Here's a fiddle. Weird is that on the fiddle I can't even get the form-group to resize.
http://jsfiddle.net/tX3ae/
<form role="form" class="row">
<div class="form-group col-lg-1">
<label for="code">Name</label>
<input type="text" class="form-control">
</div>
<div class="form-group col-lg-1 ">
<label for="code">Email</label>
<input type="text" class="form-control input-normal">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
What you want to do is certainly achievable.
What you want is to wrap each 'group' in a row, not the whole form with just one row. Here:
<div class="container">
<h1>My form</h1>
<p>How to make these input fields small and retain the layout.</p>
<form role="form">
<div class="row">
<div class="form-group col-lg-1">
<label for="code">Name</label>
<input type="text" class="form-control" />
</div>
</div>
<div class="row">
<div class="form-group col-lg-1 ">
<label for="code">Email</label>
<input type="text" class="form-control input-normal" />
</div>
</div>
<div class="row">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>
</div>
The NEW jsfiddle I made:
NEW jsfiddle
Note that in the new fiddle, I've also added 'col-xs-5' so you can see it in smaller screens too - removing them makes no difference. But keep in mind in your original classes, you are only using 'col-lg-1'. That means if the screen width is smaller than the 'lg' media query size, then the default block behaviour is used. Basically by only applying 'col-lg-1', the logic you're employing is:
IF SCREEN WIDTH < 'lg' (1200px by default)
USE DEFAULT BLOCK BEHAVIOUR (width=100%)
ELSE
APPLY 'col-lg-1' (~95px)
See Bootstrap 3 grid system for more info. I hope I was clear otherwise let me know and I'd elaborate.
In Bootstrap 3
You can simply create a custom style:
.form-control-inline {
min-width: 0;
width: auto;
display: inline;
}
Then add it to form controls like so:
<div class="controls">
<select id="expirymonth" class="form-control form-control-inline">
<option value="01">01 - January</option>
<option value="02">02 - February</option>
<option value="03">03 - March</option>
<option value="12">12 - December</option>
</select>
<select id="expiryyear" class="form-control form-control-inline">
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
</div>
This way you don't have to put extra markup for layout in your HTML.
ASP.net MVC go to Content- Site.css and remove or comment this line:
input,
select,
textarea {
/*max-width: 280px;*/
}
I think you need to wrap the inputs inside a col-lg-4, and then inside the form-group and it all gets contained in a form-horizontal..
<form class="form form-horizontal">
<div class="form-group">
<div class="col-md-3">
<label>Email</label>
<input type="email" class="form-control" id="email" placeholder="email">
</div>
</div>
...
</form>
Demo on Bootply - http://bootply.com/78156
EDIT: From the Bootstrap 3 docs..
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.
So another option is to set a specific width using CSS:
.form-control {
width:100px;
}
Or, apply the col-sm-* to the `form-group'.
Current docs say to use .col-xs-x , no lg.
Then I try in fiddle and it's seem to work :
http://jsfiddle.net/tX3ae/225/
to keep the layout maybe you can change where you put the class "row" like this :
<div class="container">
<h1>My form</h1>
<p>How to make these input fields small and retain the layout.</p>
<div class="row">
<form role="form" class="col-xs-3">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="name" >
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" id="email" name="email">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
</div>
http://jsfiddle.net/tX3ae/226/
<div class="form-group col-lg-4">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
Add the class to the form.group to constraint the inputs
If you are using the Master.Site template in Visual Studio 15, the base project has "Site.css" which OVERRIDES the width of form-control fields.
I could not get the width of my text boxes to get any wider than about 300px wide. I tried EVERYTHING and nothing worked. I found that there is a setting in Site.css which was causing the problem.
Get rid of this and you can get control over your field widths.
/* Set widths on the form inputs since otherwise they're 100% wide */
input[type="text"],
input[type="password"],
input[type="email"],
input[type="tel"],
input[type="select"] {
max-width: 280px;
}
I know this is an old thread, but I experienced the same issue with an inline form, and none of the options above solved the issue. So I fixed my inline form like so:-
<form class="form-inline" action="" method="post" accept-charset="UTF-8">
<div class="row">
<div class="form-group col-xs-7" style="padding-right: 0;">
<label class="sr-only" for="term">Search</label>
<input type="text" class="form-control" style="width: 100% !important;" name="term" id="term" placeholder="Search..." autocomplete="off">
<span class="help-block">0 results</span>
</div>
<div class="form-group col-xs-2">
<button type="submit" name="search" class="btn btn-success" id="search">Search</button>
</div>
</div>
</form>
That was my solution. Bit hacky hack, but did the job for an inline form.
You can add the style attribute or you can add a definition for the input tag in a css file.
Option 1: adding the style attribute
<input type="text" class="form-control" id="ex1" style="width: 100px;">
Option 2: definition in css
input{
width: 100px
}
You can change the 100px in auto
I hope I could help.
In Bootstrap 3
All textual < input >, < textarea >, and < select > elements with .form-control are set to width: 100%; by default.
http://getbootstrap.com/css/#forms-example
It seems, in some cases, we have to set manually the max width we want for the inputs.
Anyway, your example works. Just check it with a large screen, so you can see the name and email fields are getting the 2/12 of the with (col-lg-1 + col-lg-1 and you have 12 columns). But if you have a smaller screen (just resize your browser), the inputs will expand until the end of the row.
You don't have to give up simple css :)
.short { max-width: 300px; }
<input type="text" class="form-control short" id="...">
If you're looking to simply reduce or increase the width of Bootstrap's input elements to your liking, I would use max-width in the CSS.
Here is a very simple example I created:
<form style="max-width:500px">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="Name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email Address">
</div>
<div class="form-group">
<textarea class="form-control" rows="5" placeholder="Message"></textarea>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
I've set the whole form's maximum width to 500px. This way you won't need to use any of Bootstrap's grid system and it will also keep the form responsive.
I'm also struggled with the same problem, and this is my solution.
HTML source
<div class="input_width">
<input type="text" class="form-control input-lg" placeholder="sample">
</div>
Cover input code with another div class
CSS source
.input_width{
width: 450px;
}
give any width or margin setting on covered div class.
Bootstrap's input width is always default as 100%, so width is follow that covered width.
This is not the best way, but easiest and only solution that I solved the problem.
Hope this helped.
I do not know why everyone has seem to overlook the site.css file in the Content folder. Look at line 22 in this file and you will see the settings for input to be controlled. It would appear that your site is not referencing this style sheet.
I added this:
input, select, textarea { max-width: 280px;}
to your fiddle and it works just fine.
You should never ever update bootstrap.css or bootstrap.min.css. Doing so will set you up to fail when bootstrap gets updated. That is why the site.css file is included. This is where you can make changes to site that will still give you the responsive design you are looking for.
Here is the fiddle with it working
Add and define terms for the style="" to the input field, that's the easiest way to go about it:
Example:
<form>
<div class="form-group">
<label for="email">Email address:</label>
<input type="email" class="form-control" id="email" style="width:200px;">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" style="width:200px">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
Bootstrap uses the class 'form-input' for controlling the attributes of 'input fields'. Simply, add your own 'form-input' class with the desired width, border, text size, etc in your css file or head section.
(or else, directly add the size='5' inline code in input attributes in the body section.)
<script async src="//jsfiddle.net/tX3ae/embed/"></script>
Bootstrap 3 I achieved a nice responsive form layout using the following:
<div class="row">
<div class="form-group col-sm-4">
<label for=""> Date</label>
<input type="date" class="form-control" id="date" name="date" placeholder=" date">
</div>
<div class="form-group col-sm-4">
<label for="hours">Hours</label>
<input type="" class="form-control" id="hours" name="hours" placeholder="Total hours">
</div>
</div>

Bootstrap 2.0.2 - input-append whitespace

I've upgraded my bootstrap version to 2.0.2 and with this update the design of my input-append / add-on construct is broken.
Fiddle: http://jsfiddle.net/AACwG/
The Changelog for 2.0.2 says:
Removed all IE7 hacks and floats from .input-prepend and
.input-append, however, this requires you to ensure there is no
whitespace in your code between .add-on and the input. In
.input-prepend and .input-append, added ability to use add-ons on both
sides when you chain the selectors.
I used the bootstrap example for input-append. it's the code from github. any idea why it's not working?
according to one of the bootstrap dev's the problem is in 2.0.2-wip resolved, but i don't see the solution.
It is broken because the input groups only work within the context of a form. You can see that when you look at the css for the .controls class, e.g.:
.form-horizontal .controls {
margin-left: 160px;
}
You can fake that constraint by adding in your class instead of the .form-horizontal class or simply including your control group inside its proper container, like so:
<form class="form-horizontal">
<div class="control-group">
<label for="appendedInput" class="control-label">Appended text</label>
<div class="controls">
<div class="input-append">
<input type="text" size="16" id="appendedInput" class="span2"><span class="add-on">.00</span>
</div>
<span class="help-inline">Here's more help text</span>
</div>
</div>
</form>
Fixed fiddle: http://jsfiddle.net/AACwG/2/
For me the fix was to remove the added margin-right on the input, that interfered from another stylesheet in the framework I use (web2py).
<form class="form-horizontal">
<div class="control-group">
<label for="appendedInput" class="control-label">Appended text</label>
<div class="controls">
<div class="input-append">
<input type="text" size="16" id="appendedInput" class="span2" style="margin-right: 0px"><span class="add-on">.00</span>
</div>
<span class="help-inline">Here's more help text</span>
</div>
</div>
</form>

Resources