How to position label and input in a stacked layout? - html

I've defined a number of Bootstrap elements and want to position each label and input set in a stacked layout. At the moment the layout positioning forces the label to be positioned left of the corresponding control when the browser in maximized.
In order to re-position to a stacked layout I tried placing the label inside the same div as the input. I also tried setting the col-xs-1 property on the parent div mentioned here or increasing the col size which only pushes the label right covering the input instead of on top.
So the layout at present looks like this where my label is left aligned to the input:
Instead I'm aiming to position like this:
The window does resize to stack the labels when I reduce the size of my browser window, but when it's maximized the labels appear alongside.
Question:
Can someone explain how to stack sets of label and input controls in Bootstrap 3?
Sample of my layout:
<div class="container body-content">
<div class="page-header">
<div class="form-group">
<fieldset>
<form action="" id="createForm" class=
"form-group has-feedback" method="post">
<div class="form-horizontal">
<div class="col-lg-12">
<div class="form-group">
<label class="col-md-1 control-label" for=
"Application">Application</label>
<div class="col-md-4">
<input id="ApplicationID" name="Application"
required="required" type="text" class=
"form-control" />
</div>
</div>
<div class="form-group">
<label class="col-md-1 control-label" for=
"EscID">EM</label>
<div class="col-md-4">
<input id="EscID" name="EM" type="text"
placeholder="(If Applicable)" class=
"form-control" />
</div>
</div>
</div>
</div>
</form><!--END OF FORM ^^-->
</fieldset>
</div>
</div>
</div>

The reason here is you have parent column of 12( col-lg-12 - right inside .form-horizontal) grid. Now the child columns you have is col-md-1 (for label) and col-md-4(for input). So they are suppose to be in same parent row having column size of 12(col-lg-12).
What you could do is as following:
Give the label column of 9 and input column of 4(as it is). So in sum it exceeds 12. Which forces input to bring down(stacked as you want).
But in giving label column of 9 your text would appear on right(far far away...), so don't forget to add CSS: text-align:left; to .control-label. Also this class .control-label must be selected by parent, so it won't effect any other similar class in your document.
UPDATE: Scenario -
Image 1:
Image 2:
Image 3:
.control-label { text-align: left !important; }
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container body-content">
<div class="page-header">
<div class="form-group">
<fieldset>
<form action="" id="createForm" class="form-group has-feedback" method="post">
<div class="form-horizontal">
<div class="col-lg-12">
<div class="form-group">
<label class="col-md-9 control-label" for="Application">Application</label>
<div class="col-md-4">
<input id="ApplicationID" name="Application" required="required" type="text" class="form-control" />
</div>
</div>
<div class="form-group">
<label class="col-md-9 control-label" for="EscID">EM</label>
<div class="col-md-4">
<input id="EscID" name="EM" type="text" placeholder="(If Applicable)" class="form-control" />
</div>
</div>
</div>
</div>
</form>
<!--END OF FORM ^^-->
</fieldset>
</div>
</div>
</div>

See the basic form on Bootstrap's website.
<form>
<div class="form-group">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Email">
</div>
<div class="form-group">
<label for="exampleInputPassword1">Password</label>
<input type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
</div>
<div class="form-group">
<label for="exampleInputFile">File input</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Example block-level help text here.</p>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Check me out
</label>
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

Just remove:
The div with class="form-horizontal" (and it's closing tag)
Then remove your col-md-4, col-md-9 class references for the label and input tags.
Revised code:
https://jsfiddle.net/4bnndd8b/3/
Edit: if you want your form only 4 cols at md and large adjust your : class="col-lg-12" reference... i.e. col-md-4 (md and larger will only be a 4 col form).
<div class="container body-content">
<div class="page-header">
<div class="form-group">
<fieldset>
<form action="" id="createForm" class=
"form-group has-feedback" method="post">
<div class="col-lg-12">
<div class="form-group">
<label class="control-label" for=
"Application">Application</label>
<div class="">
<input id="ApplicationID" name="Application"
required="required" type="text" class=
"form-control" />
</div>
</div>
<div class="form-group">
<label class="control-label" for=
"EscID">EM</label>
<div class="">
<input id="EscID" name="EM" type="text"
placeholder="(If Applicable)" class=
"form-control" />
</div>
</div>
</div>
</form><!--END OF FORM ^^-->
</fieldset>
</div>

Related

How to get a layout like this in .cshtml

Hi,
How do I get a layout like in the picture?
When I expand the Accordion control on the right side , the left side panel shouldnot change.Please help.Thanks!
Use legend and fieldset tag with form.
Use .row class as the parent class of col-6 and add form tag in that.
Visit for more information https://www.w3schools.com/tags/tryit.asp?filename=tryhtml_fieldset
Example
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="row">
<div class="col-6">
<form>
<fieldset class="border p-2">
<legend class="w-auto">Personalia:</legend>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
</fieldset>
</form>
</div>
<div class="col-6">
<form>
<fieldset class="border p-2">
<legend class="w-auto">Personalia:</legend>
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lname"><br><br>
</fieldset>
</form>
</div>
</div>
For left side panel, I tried this and it works.
<!--First row starts here-->
<div class="row" style="padding-bottom:8px;margin-left:-350px;font-family:arial;font-size:15px;">
<div class="col-sm-2 required;">
<label>
col 1
</label>
</div>
<div class="col-md-4 required">
#Html.TextBoxFor(c => c.FormCode, new { id = "formcode", #class = "txtboxclass1" })
</div>
</div>
<!--First row ends here-->
How do I align Expand 1 and Expand 2 columns properly? I tried modifying above code as below, but when I click the expand button its making the left side controls to come down
<!--First row starts here-->
<div class="row" style="padding-bottom:8px;margin-left:-350px;font-family:arial;font-size:15px;">
<div class="col-sm-2 required;">
<label>
col 1
</label>
</div>
<div class="col-md-4 required">
#Html.TextBoxFor(c => c.FormCode, new { id = "formcode", #class = "txtboxclass1" })
</div>
<div id="dvAccordian">
<div class="col-sm-2 required">
Template Code
</div>
<div class="col-md-4 required">
<aClick Here</a>
</div>
</div>
</div>
<!--First row ends here-->

Aligning a textarea besides several vertically aligned input elements

As shown in the image below, I would like to align a text area besides the vertically aligned several input elements. I am using bootstrap grid to achieve the required effect.
Currently, when I use "form-horizontal", all the items stack up in a single column, hence, the textarea lies under the phone input element.
I want it to be responsive. If I use margin property on the text area, then the inputs overlap each other and so, makes the UI look messy. It would be great if I could get your help.
Desired layout:
Current layout:
<div class="container">
<form class="form-horizontal" role="form">
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="text" class="form-control" id="name" placeholder="NAME">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="email" class="form-control" id="email" placeholder="E-MAIL">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-2 col-lg-2 col-lg-offset-0">
<input type="tel" class="form-control" id="tel" placeholder="PHONE">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-md-6 col-lg-6 col-lg-offset-0">
<textarea class="form-control message" rows="6" id="comment" placeholder="MESSAGE"></textarea>
</div>
</div>
</form>
</div>
You can do this by nesting columns. Create two columns (equal to 12 columns), then place another row inside each along with your columns that contain the form inputs. I imagine you want your input to use the entire space available so set the nested columns to col-*-12 so they utilize the entire parent columns width.
Working Example: Open at FullPage
form {
background: #f2f2f2;
padding: 20px 20px 5px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<form>
<div class="row">
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<input type="text" class="form-control" id="name" placeholder="NAME">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="E-MAIL">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="tel" class="form-control" id="tel" placeholder="PHONE">
</div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
<div class="form-group">
<textarea class="form-control message" rows="6" id="comment" placeholder="MESSAGE"></textarea>
</div>
</div>
</div>
</div>
</div>
</form>
</div>

Bootstrap 3 - 2 inputs in the same line but not in the same width as one input

The title might sound tricky but here is my problem :
Inputs for "Nom" and "Prénom" are, as i want, in the same line but they are not in the same width line the first input.
Here is my code :
<form class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label"><abbr title="Code National de l'Étudiant">CNE</abbr> <span class="important">*</span></label>
<div class="col-sm-10">
<input type="text" name="cne" class="form-control">
</div>
</div>
<div class="form-group">
<label for="nom" class="col-sm-2 control-label">Nom <span class="important">*</span></label>
<div class="col-sm-10">
<div class="col-sm-6">
<input type="text" name="nomFr" class="form-control" id="nom" placeholder="En français">
</div>
<div class="col-sm-6">
<input type="text" name="nomAr" class="form-control" id="nom" placeholder="العربية" dir="rtl">
</div>
</div>
</div>
<div class="form-group">
<label for="prenom" class="col-sm-2 control-label">Prénom <span class="important">*</span></label>
<div class="col-sm-10">
<div class="col-sm-6">
<input type="text" name="prenomFr" class="form-control" id="prenom">
</div>
<div class="col-sm-6">
<input type="text" name="prenomAr" class="form-control" id="prenom" placeholder="العربية" dir="rtl">
</div>
</div>
</div>
<div class="form-group">
<label for="dateNaissance" class="col-sm-2 control-label">Date de naissance <span class="important">*</span></label>
<div class="col-sm-10">
<input type="date" name="dateNaissance" class="form-control" id="dateNaissance">
</div>
</div>
</form>
You need to add a row class on your <div class="col-sm-10"> divs. This sets a negative padding which means the position padding set by columns allows it to line up.
Look at http://getbootstrap.com/css/#grid-nesting for info.
<div class="form-group">
<label for="nom" class="col-sm-2 control-label">Nom <span class="important">*</span></label>
<div class="col-sm-10 row">
<div class="col-sm-6">
<input type="text" name="nomFr" class="form-control" id="nom" placeholder="En français">
</div>
<div class="col-sm-6">
<input type="text" name="nomAr" class="form-control" id="nom" placeholder="العربية" dir="rtl">
</div>
</div>
</div>
Be careful how you nest your elements. You must have a new row before nesting col-*s.
<div class="col-sm-10 row">
<div class="row">
<div class="col-sm-6">
<input type="text" name="nomFr" class="form-control" id="nom" placeholder="En français">
</div>
<div class="col-sm-6">
<input type="text" name="nomAr" class="form-control" id="nom" placeholder="العربية" dir="rtl">
</div>
</div>
</div>
Every container and col-* has a 15px padding left and right. The row element has margin left and right set to -15px to compensate. so container > row > col > row >col etc
Actually you don't need to have a row for each set of col- elements, as the .form-group class is the same as .row with the addition of a bottom-margin - you could argue that semantically it's better... Meh.
The main issue is you are nesting 2 x 6-column-span elements in a 10-column-span element, and you have already used up 2 columns with the label, so it's all wrong.
As you probably know, Bootstrap uses a 12 column grid system, so if you create an element and apply col-sm-2 then you have 10 columns left. If you have 2 elements, each spanning 6 columns, it's not going to work.
What you can do is lose the extra <div class="col-sm-10"> and change your 2 inputs to span 5 cols each:
<div class="form-group">
<label for="nom" class="col-sm-2 control-label">Nom <span class="important">*</span></label>
<div class="col-sm-5">
<input type="text" name="nomFr" class="form-control" id="nom" placeholder="En français">
</div>
<div class="col-sm-5">
<input type="text" name="nomAr" class="form-control" id="nom" placeholder="العربية" dir="rtl">
</div>
</div>
That will give you what you really want - each input field in 2 column areas aligned to the edges of the fields in single column areas.
Note: I use form-group here because the bottom margin pads out the rows and looks nice IMO.

Inline text field using Twitter Bootstrap 3.0

I am trying to make an inline text field within a horizontal form, basically I want to split the form in to two sections ( left and right) , in the left hand I will have like horizontal text fields and some inline as well and on the right hand side I want to have some inline and some are not.
How could I establish something like that , maybe this drawing could help
JSFiddle Here : http://jsfiddle.net/XQx6v/
Here is my code which is not working ( I want to make it 900 px centered and also responsive to drop to the other line if its small):
<form class="form-horizontal" action="" method="POST">
<!-- first Row -->
<div style="margin:auto; width:900px;" class="row">
<div class="col-xs-6">
<div class="row">
<div class="form-group">
<label class="control-label">First Name</label>
<input type="text" value="test" name="first_name" class="form-control beta">
<label class="control-label">Last Name</label>
<input type="text" value="test" name="last_name" class="form-control beta"> </div>
</div>
</div>
<!-- Next Column -->
<div class="col-xs-6">
<div class="row">
<p> testhghghjg </p>
</div>
</div>
</div>
</form>
Thanks
I think you don't actually want what Bootstrap refers to as an inline form, but a form with inline elements... Try this Bootply
<div class="container" style="max-width:900px">
<div class="row">
<div class="col-md-3">
<form role="form">
<div class="form-group">
<label>Name 1</label>
<input type="email" class="form-control" placeholder="Enater Name">
</div>
</form>
</div>
<div class="col-md-3">
<form role="form">
<div class="form-group">
<label>Name 2</label>
<input type="email" class="form-control" placeholder="Enater Name">
</div>
</form>
</div>
<div class="col-md-3">
<form role="form">
<div class="form-group">
<label>Name 3</label>
<input type="email" class="form-control" placeholder="Enater Name">
</div>
</form>
</div>
<div class="col-md-3">
<form role="form">
<div class="form-group">
<label>Name 4</label>
<input type="email" class="form-control" placeholder="Enater Name">
</div>
</form>
</div>
</div></div>

Twitter Bootstrap - Two Column layout for <form>

In Bootstrap 2.3, is there a standardized way to have a two-column layout for an HTML <form> with labels/inputs/actions? I don't see any in the documentation.
An example image of what I want: http://i.imgur.com/3o6IoN4.png
As an aside, I need there to be a solid background color that spans the entire width of the div.span12 or other enclosing container. Having two .span6 causes a break in the background color in the center (which I suppose can be fixed by wrapping the two .span6 in a div.clearfix with the background class applied?
Easy stuff. Bring the bootstrap row within a parent div and set the background of that div to a color of your choosing.
The Markup:
<div id="background-color">
<form id="form1" name="form1" method="post" action=""><!-- START THE FORM -->
<div class="row-fluid">
<div class="span6"> <!-- FIRST COLUMN -->
<label>First Name</label>
<label for="textfield"></label>
<input type="text" />
<label>Last Name</label>
<label for="textfield"></label>
<input type="text" />
</div>
<div class="span6"> <!-- SECOND COLUMN -->
<label>Other</label>
<label for="textfield"></label>
<input type="text" />
<label>Fields</label>
<label for="textfield"></label>
<input type="text" />
<input type="submit" name="button" id="button" value="Submit" />
</form> <!-- END THE FORM -->
</div>
</form>
</div> <!-- End row -->
</div> <!-- END BACKGROUND -->
The CSS:
#background-color {background-color: #CCC;}
I hope this helps
To my understanding for Bootstrap 3 these changes are required;
all code sitting in container to access grid system info here
span to col for new version
use device class as suits your requirements info here
labels and controls wrapped in form-group for spacing info here
form class to form-horizontal removes the need for row divs (i have used nested columns without rows, seems to display correctly with form-horizontals function)
panel used to frame the form as pictured
<div class="container">
<div class="panel panel-default" style="background-color: #CCC">
<div class="panel-body">
<form id="form1" name="form1" method="post" action="" class="form-horizontal"><!-- START THE FORM -->
<div class="col-sm-6"> <!-- FIRST COLUMN -->
<div class="form-group">
<label for="inputFirstname" class="col-sm-4 control-label">First Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputFirstname" placeholder="First Name">
</div>
</div>
<div class="form-group">
<label for="inputLastname" class="col-sm-4 control-label">Last Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputLastname" placeholder="Last Name">
</div>
</div>
</div>
<div class="col-sm-6"> <!-- SECOND COLUMN -->
<div class="form-group">
<label for="inputEmail" class="col-sm-4 control-label">Email</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputEmail" placeholder="Email">
</div>
</div>
<div class="form-group">
<label for="inputUsername" class="col-sm-4 control-label">Username</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputUsername" placeholder="Username">
</div>
</div>
</div>
</form> <!-- END THE FORM -->
</div>
</div><!-- END PANEL -->
</div>