Align group of labels and inputs - html

Without the use of tables, using CSS, how can i align a group of labels and their associated input controls like below (these controls will be in a div):
The controls will be laid out like:
[Label1] [Input1] [Label4] [Input4]
[Label2] [Input2] [Label5] [Input5]
[Label3] [Input3] [Label6] [Input6]

This is kind of open-ended, since a lot depends on the questions, the labels and other specific considerations that you may have for your code.
However, the easiest way to accomplish something like this (IE8+) is probably to use display:table and display:table-cell.
Here's a quick fiddle that basically does that: http://jsfiddle.net/cwz3g/
It uses a lot of divs: one per column, one per label/field pairing and one each for the label and field, but should cover a lot of the common things like vertical alignment of labels and fields. You'll probably just need to tweak some things for your code situation, but it should get you started.

For your two columns, you'll want to put them in container divs, which could then be floated against each other or absolutely positioned, or whatever works for your layout.
To make the labels take up the same amount of room, they can be styled with a width attribute.
Finally, in your diagram, you've got the labels left-justified and the inputs right-justified. That's doable, but to my eyes it scans better if the inputs are justified toward their labels.
I've got it all in a fiddle here: http://jsfiddle.net/M7AjF/
<div class="form-column">
<label for="Input1">Option 1</label>
<select id="Input1"><option>All</option></select>
<br>
<label for="Input2">Option 2</label>
<input id="Input2" type="text"/>
<br>
<label for="Input3">Option 3</label>
<input id="Input3" type="text"/>
</div>
<div class="form-column">
<label for="Input4">Option 4</label>
<select id="Input4"><option>--</option></select>
<br>
<label for="Input5">Option 5</label>
<select id="Input5"><option>0</option></select>
<br>
<label for="Input6">Option 6</label>
<select id="Input6"><option>1</option></select>
</div>
And the css:
.form-column {
width: 50%;
float:left;
}
.form-column label {
width: 40%;
padding-right: 10%;
}
/*
This right justifies your inputs, which I don't recommend.
.form-column select, .form-column input {
float: right;
}
.form-column br {
clear: right;
}
*/

I suggest to use display:table layout like this:
html
<div class="table">
<div class="row">
<div class="cell"><label>Option 1</label></div>
<div class="cell"><select><option>ALL</option></select></div>
<div class="cell"><label>Option 4</label></div>
<div class="cell"><select><option>--</option></select></div>
</div>
<div class="row">
<div class="cell"><label>Option 2</label></div>
<div class="cell"><input type="text"></input></div>
<div class="cell"><label>Option 5</label></div>
<div class="cell"><input type="text"></input></div>
</div>
<div class="row">
<div class="cell"><label>Option 3</label></div>
<div class="cell"><input type="text"></input></div>
<div class="cell"><label>Option 6</label></div>
<div class="cell"><select><option>1</option></select></div>
</div>
</div>
css
.table{
display:table;
}
.row{
display:table-row;
}
.cell{
display:table-cell;
}
.table > .row > .cell{
text-align: right;
padding-right: 75px;
}
.table > .row > .cell > input{
width:75px;
}
fiddle

you can use bootstrap.css for easy way.
http://getbootstrap.com/components/
check my fiddle.
<form class="form-horizontal" role="form" method="post" action="">
<div class="col-xs-6">
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
<div class="form-group">
<label class="col-xs-4">Title</label>
<div class="col-xs-8">
<input name="title" type="text" class="form-control" value="Circular Countdown"/>
</div>
</div>
</div>
</form>
http://jsfiddle.net/4M7qH/

Related

Alignment of input text fields in angular html

<div class="form-group">
<label class="lable label-default"> Google Location</label>
<input class="form-control" type="text" name='latitude' placeholder="latitude"
[(ngModel)]="tourDetails.latitude">
<input class="form-control" type="text" name='longitude' placeholder="longitude"
[(ngModel)]="tourDetails.longitude">
</div>
My Css code:
label {
display: inline-block;
width: 140px;
text-align: left;
}
input {
display: inline-block;
width: 140px;
text-align: left;
}
I have my Following Output. But I want to align these input text fields one by one with same proper alignment.
Share your css to properly understand the style, but I am assuming the typo lable instead of label may affect the css rules
<div class="form-group">
<label class="label label-default"> Google Location</label>
<input class="form-control" type="text" name='latitude' placeholder="latitude"
[(ngModel)]="tourDetails.latitude">
<input class="form-control" type="text" name='longitude' placeholder="longitude"
[(ngModel)]="tourDetails.longitude">
</div>
I've done only for geo-location. But if like to get the idea of bootstrap, you can refer W3schools or Bootstrap offcial web
The working fiddle Jsfiddle
<div class="container">
<form class="form-horizontal">
<div class="row">
<div class="col-xs-6">
<label class="control-label label-default"> Google Location</label>
</div>
<div class="col-xs-6">
<div class="form-group">
<input class="form-control" type="text" name='latitude' placeholder="latitude"
[(ngModel)]="tourDetails.latitude">
<input class="form-control" type="text" name='longitude' placeholder="longitude"
[(ngModel)]="tourDetails.longitude">
</div>
</div>
</div>
</form>
</div>
Update 1 As you updated the requirement, I've updated the fiddle too. So you can easily remove <div class="row"> and get your expectation

Placing two columns beside each other in a partial view

I am working on a partial in MVC that contains a form in tow columns. However, one of the forms sits below the other. Here is my code.
<div class="row">
<div id="partialColumn1" class= "col-md-12">
<div class="field padding-left-40">
<label>First Name:</label>
<input id="add_Client_firstName-1" name="add_Client_firstName-1" type="text" value="[First Name goes here ]">
</div>
<div class="field padding-left-40">
<label>Last Name:</label>
<input id="add_Client_lastName-1" name="add_Client_LastName-1" type="text" value="[Last Name goes here ]">
</div>
<div id="partialColumn2" class="col-md-12">
<div class="field padding-left-40">
<label>Referral Date:</label>
<input id="add_Client_referralDate-1" name="add_Client_referralDate-1" type="Date" />
</div>
</div>
</div>
</div>
</div>
And the CSS for my two columns.
#partialColumn1{
float: left;
display: inline-block;
}
#partialColumn2{
display: inline-block;
float: right;
}
I am relatively new to this, it's my first project, so any advice is welcome.
#partialColunm2 is nested in #partialColumn1 ;) Try putting #partialColumn2 outside of #partialColumn1 :D
At least it worked for me.
DEMO: https://codepen.io/Chesswithsean/pen/gWZGGL
Put your cols into 6 each one and remove your css, cols already has inline.
<div class='row'>
<div id="partialColumn1" class= "col-md-6">
<div class="field padding-left-40">
<label>First Name:</label>
<input id="add_Client_firstName-1" name="add_Client_firstName-1" type="text" value="[First Name goes here ]">
</div>
<div class="field padding-left-40">
<label>Last Name:</label>
<input id="add_Client_lastName-1" name="add_Client_LastName-1" type="text" value="[Last Name goes here ]">
</div>
</div>
<div id="partialColumn2" class="col-md-6">
<div class="field padding-left-40">
<label>Referral Date:</label>
<input id="add_Client_referralDate-1" name="add_Client_referralDate-1" type="Date" />
</div>
</div>
</div>

How to align label and text box in line through html?

With the below code, the summary and it's text box are aligned inline.But the text box for description is appearing below the label 'description'. How can I make it align inline? Please help me in resolving this.
.left {
width: 30%;
float: left;
text-align: right;
}
.right {
width: 65%;
margin-left: 10px;
float: left;
}
<body>
<div class="container">
<div class="row">
<div class="col-sm-5 col-sm-push-3 col-xs-4 col-xs-push-2">
<h1 class="text-center" style="font-family:Colonna MT;font-size:50px;color:ForestGreen ;">Post Your Ad</h1>
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description" style="height:100px;width:400px">
</div>
</form>
</div>
</div>
</div>
Try like this: Demo
If you need multi line text box, use textarea instead of input type text
<div class="form-group row">
<label for="inputEmail3" class="col-md-6 col-sm-12 form-control-label text-right">Summary</label>
<div class="col-md-6 col-sm-12">
<input type="text" class="form-control" name="Summary" size="50">
</div>
</div>
<div class="form-group row">
<label for="inputEmail3" class="col-md-6 col-sm-12 form-control-label text-right">Description</label>
<div class="col-md-6 col-sm-12">
<textarea class="form-control" name="Description"></textarea>
</div>
</div>
Edit:
As you mentioned, its not form-control class, its form-control-label.. You can get more info here
Updated Demo: To change the height, try chnaging the value of rows="8" for <textarea>
If you're using Bootstrap (as the classes on your code suggest). I would recommend using their form layout options. Sounds like you want a Horizontal Form:
Docs
For Example:
<form class="form-horizontal">
<div class="form-group">
<label for="summary" class="col-sm-2 control-label">Summary</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="summary" />
</div>
</div>
<div class="form-group">
<label for="description" class="col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="description" style="height:100px;width:400px" />
</div>
</div>
</form>
<input class="right" type="text" name="Description" style="height:100px;width:400px;">
Remove width from input of description, also please change to textarea, instead of input.
You have already used bootstrap framework.so that there is no need to write extra css for inline forms. follow the following code
<div class="form-group">
<label class="col-lg-3 control-label ">City</label>
<div class="col-lg-9">
<div class="form-inline">
<div class="form-group ">
<div class="col-lg-12">
<label class="sr-only" for="city">City</label>
<input type="text" id="city" name="city" class="form-control " placeholder="E.g. Thrissur" >
</div>
</div>
<div class="form-group ">
<div class="col-lg-12">
<label class="sr-only" for="county">County</label>
<input type="text" id="county" name="county" class="form-control " placeholder="E.g. India" >
</div>
</div>
</div>
</div>
</div>
Like this?
https://jsfiddle.net/d6tscd18/
.left {
width: 30%;
display: inline-block;
text-align: right;
}
.right {
width: 65%;
margin-left: 10px;
display: inline-block;
}
try this:
<html>
<head>
<title>sample</title>
<style type="text/css">
.left {
width: 30%;
display: inline-block;
text-align: right
}
.right {
margin-left: 10px;
display: inline-block;
}
</style>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-5 col-sm-push-3 col-xs-4 col-xs-push-2">
<h1 class="text-center" style="font-family:Colonna MT;font- size:50px;color:ForestGreen ; text-align: center">Post Your Ad</h1>
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50" style="width:400px">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description" style="height:100px;width:400px">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
If you have trouble getting it in place you can also force a table layout without using tables:
.login-form {
display: table;
}
.login-form>div {
display: table-row;
}
.cell {
display:table-cell;
vertical-align: top;
}
...
Adding a div around the inputs and a "cell" class (and shouldn't it be a textarea?):
...
<form action="post" class="login-form">
<div>
<label class="left cell">Summary:</label>
<div class="cell">
<input class="right" type="text" name="Summary" size="50">
</div>
</div>
<div>
<label class="left cell">Description:</label>
<div class="cell">
<textarea class="right" name="Description" style="height:100px;width:400px"></textarea>
</div>
</div>
</form>
...
Check it here
Remove the style attribute from the description input type, it will then appear as the summary input box.
<form action="post" class="login-form">
<div>
<label class="left">Summary:</label>
<input class="right" type="text" name="Summary" size="50" style="width:400px">
</div>
<div>
<label class="left">Description:</label>
<input class="right" type="text" name="Description">

How to place Bootstrap form inputs closer to eachother?

I'm building a form using Bootstrap in which I need to place many inputs on one line like if it where a Spreadsheet. So using the regular Bootstrap grid system I did this (snippet):
<div class="col-sm-1">
<div class="form-group">
<textarea placeholder="Description" class="form-control" rows="1" required></textarea>
</div>
</div>
<div class="col-sm-1">
<input placeholder="Remaining contract term" class="form-control" type="text">
</div>
<div class="col-sm-1">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">€</div>
<input placeholder="Contract rental" class="form-control" type="text">
</div>
</div>
</div>
etc.
which results in:
Obviously that looks horrible. For this specific form I need to place the inputs closer to eachother so that they (almost) touch eachother.
Does anybody know how I can style this better? All tips are welcome!
You could override the padding on the col-sm-* with a class like this
.inputItem {
padding-left: 2px;
padding-right: 2px;
}
This might be useful.
You can adjust your column padding by media queries in the event you want the form to stack on smaller devices so the padding returns to normal.
#media (min-width: 768px) {
form .form-gutter >[class*='col-'] {
padding-right: 1px;
padding-left: 1px;
}
}
form .form-control,
form .input-group-addon {
border-radius: 0;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<form class="form">
<div class="container-fluid">
<h1>Form </h1>
<div class="row form-gutter">
<div class="col-sm-2">
<div class="form-group">
<div class="input-group">
<input placeholder="Contract rental" class="form-control" type="text">
<div class="input-group-addon">€</div>
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="option1">Some</label>
<label class="radio-inline">
<input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="option2">Thing</label>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<input placeholder="Remaining contract term" class="form-control" type="text">
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<input placeholder="Remaining contract term" class="form-control" type="text">
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">€</div>
<input placeholder="Contract rental" class="form-control" type="text">
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<div class="input-group">
<div class="input-group-addon">€</div>
<input placeholder="Contract rental" class="form-control" type="text">
</div>
</div>
</div>
</div>
</div>
</form>

Horizontal form with bootstrap 3

I am trying to create a fluid one-row form with text boxes, with each textbox having its corresponding label on top of it. Just like a table with a header and only one data-row, something roughly like this (but hopefully prettier...):
http://jsfiddle.net/52VtD/5553/
The form should preferably be fluid, so that the text boxes resize as needed (but the labels should always stay on top of their respective textbox).
Here is one unsuccessful attempt:
http://jsfiddle.net/fJ7Hb/3/
<form class="form-horizontal">
<div class="row">
<label class="col-sm-1">Item #</label>
<div class="col-sm-2"><input type="text" class="form-control" /></div>
<label class="col-sm-1">Description</label>
<div class="col-sm-2"><input type="text" class="form-control" /></div>
<label class="col-sm-1">Qty</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
<label class="col-sm-1">Price</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
<label class="col-sm-1">Total</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
</div>
</form>
Help would be appreciated.
You can wrap each of your columns in a div tag and then work with some css elements to set display and where you want each item.
Here's the fiddle : http://jsfiddle.net/fJ7Hb/6/
HTML
<div class="row">
<div>
<label class="col-sm-1">Item #</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
</div>
<div>
<label class="col-sm-1">Description</label>
<div class="col-sm-2"><input type="text" class="form-control" /></div>
</div>
<div>
<label class="col-sm-1">Qty</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
</div>
<div>
<label class="col-sm-1">Price</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
</div>
<div>
<label class="col-sm-1">Total</label>
<div class="col-sm-1"><input type="text" class="form-control" /></div>
</div>
</div>
CSS
.row {
width: 1100px;
}
.row > div {
display: inline-block;
}