It is possible to put the labels above of each textbox without using table? My problem is when i try to put a "new line" inside a "NoWrap".
Basically i got an array of array, that have "name" and "Value", the value will be changed by user, so the first set of elements will display side by side, the next set of elements will be display below, side by side, and go on. I can do with this simple code, but the labels are at LEFT of the component.
<style>
.NoWrap{white-space:nowrap;margin-top:5px;margin-left:5px;}
</style>
<div style="border:1px solid black; width:500px;height:800px;overflow:auto;">
<div class="NoWrap">
Label A1 <input type="text" />
Label A2 <input type="text" />
Label A3 <input type="text" />
Label A4 <input type="text" />
</div>
<div class="NoWrap">
Label B1 <input type="text" />
</div>
<div class="NoWrap">
Label C1 <input type="text" />
Label C1 <input type="text" />
</div>
<div class="NoWrap">
Label D1 <input type="text" />
</div>
</div>
But when i try to break a line inside, every thing mess up. I Tried to put a div inside with float but no luck with that. Looking for examples i found some but i couldn't put them together
<div id='div1' style='width:300px; border: 1px solid black; white-space:nowrap; padding-right: 50px;'>
<label>Test <br />
<input type='text' style='width:100%;' id='inputBox'/>
</label>
<label>Test <br />
<input type='text' style='width:100%;' id='inputBox'/>
</label>
</div>
That is what i'm looking for
Label A1 Label A2 Label A3
TextBox... Textbox... Textbox... .... goes to scroll forever
Label B1 Label B2
TextBox... Textbox...
Label C1
TextBox...
It's always a good idea to associate your inputs and their text using a label:
<label>Label A1 <input type="text" /></label>
And then you can use these styles:
label { display: inline-block; } /* One next to the others */
input { display: block; } /* Line break between text and input */
.wrapper {
border: 1px solid black;
width: 500px;
height: 800px;
overflow: auto;
}
.wrapper > div {
white-space: nowrap;
margin-top: 5px;
margin-left: 5px;
}
label {
display: inline-block;
}
input {
display: block;
}
<div class="wrapper">
<div>
<label>Label A1 <input type="text" /></label>
<label>Label A2 <input type="text" /></label>
<label>Label A3 <input type="text" /></label>
<label>Label A4 <input type="text" /></label>
</div>
<div>
<label>Label B1 <input type="text" /></label>
</div>
<div>
<label>Label C1 <input type="text" /></label>
<label>Label C2 <input type="text" /></label>
</div>
<div>
<label>Label D1 <input type="text" /></label>
</div>
</div>
If you put your text inside proper html label elements, you could remove them from the normal flow of the document and position them slightly above where they would normally appear.
For example, make your containers position: relative and your label elements position: absolute. Then give your container a margin-top: XXpx and your label elements a top: -XXpx
You can see this working here (jsbin link):
<div style="border:1px solid black; width:500px;height:800px;overflow:auto;">
<div class="NoWrap">
<label for="A1">Label A1</label>
<input id="A1" type="text" />
<label for="A2">Label A2</label>
<input id="A2" type="text" />
<label for="A3">Label A3</label>
<input id="A3" type="text" />
<label for="A4">Label A\4</label>
<input id="A4" type="text" />
</div>
</div>
And styles:
.NoWrap{
white-space:nowrap;
margin-top:25px;
margin-left:5px;
position: relative;
}
label {
position: absolute;
top: -20px;
}
you can use position:relative + margins
.NoWrap{white-space:nowrap;margin-top:5px;margin-left:5px;}
input {
margin-bottom:3em;/* make room under */
width:5em;/* resize to yyour needs */
margin-right:-3em;/* reduce virtually rooms it uses , tune to your needs */
position:relative;/* now time to move it at screen */
top:1.8em;/* enough lower than baseline */
right:5em;/* its own size */
}
<div style="border:1px solid black; width:500px;height:800px;overflow:auto;">
<div class="NoWrap">
Label A1 <input type="text" />
Label A2 <input type="text" />
Label A3 <input type="text" />
Label A4 <input type="text" />
</div>
<div class="NoWrap">
Label B1 <input type="text" />
</div>
<div class="NoWrap">
Label C1 <input type="text" />
Label C1 <input type="text" />
</div>
<div class="NoWrap">
Label D1 <input type="text" />
</div>
</div>
Related
EDIT: I have updated both the CSS and the html. I have figured out the centering, but am still having trouble with the alignment of labels and fields.
I need for the input fields and labels to be lined up to where there is a perfect line running down the center of them, essentially dividing them because the labels are stacked on top of one another and input fields are stacked on top of one another.
The code below already resembled how I want it to look, other than that the form is on the left edge and my labels and input fields aren't perfectly lined up.
This is my form so far:
<div style="text-align:center">
<form>
<div>
Name: <input type="text" name="Name" size="40"/>
<br/>
<br/>
</div>
<div>
Address: <input type="text" name="Address" size="50"/>
<br/>
<br/>
</div>
<div>
City: <input type="text" name="City" size="25"/>
<br/>
<br/>
</div>
<div>
State: <input type="text" name="State" size="25"/>
<br/>
<br/>
</div>
<div>
Zip: <input type="text" name="Zip" size="25"/>
<br/>
<br/>
</div>
<div>
Email: <input type="text" name="Email" size="40"/>
<br/>
<br/>
</div>
<div>
Subscribe to our mailing list?
<br/>
<input type="radio" name="AddToList" value="yes" checked="checked" />Yes
<input type="radio" name="AddToList" value="no" />No
<br/>
<br/>
</div>
<div>
Comments:<br/>
<textarea name="comments" cols="70" rows="10" placeholder="Expected value of input"></textarea>
<br/>
<br/>
<input type="submit" />
<input type="reset" />
</div>
</form>
</div>
So far, the only CSS I have for this form is:
form {
display: inline-block;
margin-left: auto;
margin-right: auto;
text-align: left;
}
This is what I am trying to accomplish:
Any help or advice would be great and highly appreciated, I am really just dumbfounded by this one. I tried using some of the centering methods for tables, none of which worked.
check this i just positioned it with css
<!DOCTYPE html>
<html>
<head>
<title>index</title>
<style>label{
position: relative;
bottom: 2px;
}
input{
position: relative;
margin-top: 10px;
}
.text{
display: flex;
justify-content: center;
}
.text{
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<div style="text-align:center">
<form>
<div>
<label> Name:</label>
<input type="text" name="Name" size="40"/>
</div>
<div>
<label style="left:27px"> Address:</label>
<input type="text" name="Address" size="50" style="left:27px;"/>
</div>
<div>
<label style="right: 46px;">City: </label>
<input type="text" name="City" size="25" style="right:46px;"/>
</div>
<div>
<label style="right:48px">State:</label>
<input type="text" name="State" size="25"/ style="right: 49px;">
</div>
<div>
<label style="right: 43px;"> Zip:</label> <input type="text" name="Zip" size="25" style="right: 44px;"/>
</div>
<div>
<label style="bottom: 0px;" > Email:</label>
<input type="text" name="Email" size="40"/>
</div>
<div style="right: 180px; position: relative;" >
<label>Subscribe to our mailing list?</label>
<input type="radio" name="AddToList" value="yes" checked="checked" />Yes
<input type="radio" name="AddToList" value="no" />No
<br/>
<br/>
</div>
<div class="text">
<br>
<br>
<label style="top: 60px; left: 70px;"> Comments: </label>
<textarea name="comments" cols="70" rows="10" placeholder="Expected value of input" style="left: 84px; position: relative;"></textarea>
<br/>
<br/>
</div >
<div class="submit" style="right: 80px; position: relative;">
<input type="submit" width="" />
<input type="reset" />
</div >
</form>
</div>
<script src="test.js"></script>
</body>
</html>
For centering things, try:
margin: auto;
justify-content: center;
text-align: center
This should align all the contents in the page as well as the text also with margins being equal.
Second option:
margin: auto;
width: 80%
This will take only 80% of total width of parent element and will automatically be centered if that also doesn't work then remove margin: auto from second option.
To center align, give the elements you want to align a class of "center", and then select the class in CSS. An example is :
.center {
text-align: center;
}
<div>
Email: <input type="text" name="Email" size="40" class="center"/>
<br/>
</div>
EDIT : W3 Schools is where I found the answer.
I want to prevent irregular gaps between my inputs and my text. How should I do this with a CSS property?
For example that my input will be placed all over that red line:
My HTML:
<body>
<div>
<p class ="LabelInput">Programme/CP/Ville
<input type="text" id="cp" name="cp"
placeholder="" />
</p>
</div>
<div class = "Typologie">
<p class ="LabelInput">Typologie
<input type="checkbox" id="Studio" name="Studio" checked />
<label for="Studio">Studio</label>
<input type="checkbox" id="T2" name="T2" checked />
<label for="T2">T2</label>
<input type="checkbox" id="T3" name="T3" checked />
<label for="T3">T3</label>
<input type="checkbox" id="T4" name="T4" checked />
<label for="T4">T4</label>
<input type="checkbox" id="T5P" name="T5P" checked />
<label for="T5P">T5P</label>
</p>
</div>
<div class = "Type">
<p class ="LabelInput">Type
<input type="checkbox" id="Appartement" name="Appartement" checked />
<label for="Appartement">Appartement</label>
<input type="checkbox" id="Maison" name="Maison" checked />
<label for="Maison">Maison</label>
<input type="checkbox" id="Commerce" name="Commerce" checked />
<label for="Commerce">Commerce</label>
<input type="checkbox" id="Parking" name="Parking" checked />
<label for="Parking">Parking</label>
</p>
</div>
<div class = "Budget">
<p class ="LabelInput">Budget
<div id="slider-range"></div>
</p>
</div>
<div class = "Livraison">
<p class ="LabelInput">Livraison
<input type="text" id="cp" name="cp"
placeholder="" />
</p>
</div>
<div class = "Annexes">
<p class ="LabelInput">Annexes
<input type="text" id="cp" name="cp"
placeholder="" />
</p>
</div>
I'm sure it is something you can do but I can't remember or find the property.
You will need to make some adjustments to your HTML for the best results.
As p is not allowed to contain block-level elements, you can not put divs in it as seen in your Budget section.
Form the w3 site:
The P element represents a paragraph. It cannot contain block-level elements (including P itself).
Instead, add two div wrappers around your label and your content for a clean solution:
.LabelInput {
width: 30%;
display: inline-block;
}
.LabelContent {
width: 69%;
display: inline-block;
}
<div class = "Type">
<div class="LabelInput">Type</div>
<div class="LabelContent">
<input type="checkbox" id="Appartement" name="Appartement" checked />
<label for="Appartement">Appartement</label>
<input type="checkbox" id="Maison" name="Maison" checked />
<label for="Maison">Maison</label>
<input type="checkbox" id="Commerce" name="Commerce" checked />
<label for="Commerce">Commerce</label>
<input type="checkbox" id="Parking" name="Parking" checked />
<label for="Parking">Parking</label>
</div>
</div>
If you do not want to change your html, add a wrapper element like span around your label and set min-width on it. That is an easy, albeit dirty solution.
<div class="LabelInput"><span class="myLabel">Type</span></div>
.myLabel {
min-width: 200px //adjust as needed to size of biggest label
display: inline-block;
}
I am trying to format a form using CSS. Here is my sample HTML:
<div id='login'>
<h3>Log In</h3>
<form action='/' method='post' accept-charset='UTF-8'>
<div class='centerform'>
<label for='userId'>User Id:</label>
<input id='userId' maxlength='30' />
<label for='password'>Password:</label>
<input id='password' type='password' maxlength='30' />
<label for='longer'>Longer Field:</label>
<input id='longer' maxlength='50' width='50' />
<label for='checkbox'>I have a bike:</label>
<input id='checkbox' type='checkbox' name='vehicle' value='Bike'>
<input type='submit' class='button' value='Submit' />
</div>
</form>
</div>
I want the form to look like this:
Notice that the input fields are left aligned to each other, the labels are right aligned to each other, the submit button is right aligned to the entire form, and the whole thing needs to be centered horizontally in the page. This same CSS needs to work for many different forms with variable width fields.
My current CSS looks like this:
.centerform label{
float: left;
width: 50%;
text-align: right;
/*background-color: green;*/
}
.centerform input{
font-size: 100%;
float: right;
width: 50%;
}
The only thing that gets right is the alignment of the labels. I also am not sure on how to indicate the lengths of the fields. Any width I set in the html gets overridden by the CSS.
I DO NOT want to use any hard coded pixel or percent sizes except for the variable length fields if necessary.
EDIT: I figured out how to have variable length fields by using the size attribute.
This layout might be of help to you. I have given the dimensions in percentage as asked.
.centerform label {
text-align: right;
display: inline-block;
width: 16%;
}
.centerform input {
display: inline-block;
width: 82%;
}
.button {
width: 20% !important;
float: right;
}
<div id='login'>
<h3>Log In</h3>
<form action='/' method='post' accept-charset='UTF-8'>
<div class='centerform'>
<label for='userId'>User Id:</label>
<input id='userId' maxlength='30' />
<label for='password'>Password:</label>
<input id='password' type='password' maxlength='30' />
<label for='longer'>Longer Field:</label>
<input id='longer' maxlength='50' width='50' />
<label for='checkbox'>I have a bike:</label>
<input id='checkbox' type='checkbox' name='vehicle' value='Bike'>
<input type='submit' class='button' value='Submit' />
</div>
</form>
</div>
.centerform ul{margin:0;padding:0}
.centerform ul li{list-style:none;padding:5px 0;}
.centerform label{width:18%;display:inline-block;text-align:right;}
.centerform input{display:inline-block;width:80%;text-align:left;}
.centerform input[type='checkbox']{display:inline;width:auto;}
.centerform input[type='submit'] {display:inline;width:auto;float:right;}
<div id='login'>
<h3>Log In</h3>
<form action='/' method='post' accept-charset='UTF-8'>
<div class='centerform'>
<ul>
<li>
<label for='userId'>User Id:</label>
<input maxlength='30' />
</li>
<li>
<label for='password'>Password:</label>
<input type='password' maxlength='30' />
</li>
<li>
<label for='longer'>Longer Field:</label>
<input maxlength='50' width='50' />
</li>
<li>
<label for='checkbox'>I have a bike:</label>
<input type='checkbox' name='vehicle' value='Bike'>
</li>
<li>
<label></label>
<input type='submit' class='button' value='Submit' />
</li>
</ul>
</div>
</form>
</div>
Hope this is what you are expecting, you can simply adjust .centerform label and .centerform input WIDTH to adjust and fit the form's overall width.
I am trying to get a better handle on CSS positioning by using only basic positioning properties. The goal is to get an HTML5 input and it's associated label to line up horizontally, one pair on each row, with the label on the left and input on the right. Essentially there will appear to be two columns, one for labels and the other for inputs.
I also want each column to be left-justified, which is where I'm currently stuck.
Using the CSS below I can get the two-column look I want, however none of the input elements are justified correctly.
If I set the position of the input elements to absolut, however (the thinking that adjusting the left property will align each element the same pixel length from the left containing edge), each element justifies properly, however all on the same row.
Any hints as to how to accomplish the two-column/left-justified layout w/o using tables or grid-column?
Fiddle: http://jsfiddle.net/fjwy3Lov/
CSS
/*Styles for basic form label and input elements*/
.basicForm{
margin: 10px 0 10px 10px;
}
.basicForm label{
float:left;
clear:left;
margin:inherit;
}
.basicForm input{
position:relative;
left:100px;
float:left;
margin: inherit;
}
HTML
<!DOCTYPE html>
<head>
<title>Form Validation Demo</title>
<link href="form.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>HTML 5 Input Types and Form Validation</h1>
<form class="basicForm">
<label for="UserName">User Name:</label>
<input type="text" id="UserName" required="required">
<label for="Password">Password:</label>
<input type="password" id="Password" required="required" />
<label for="UserEmail">Email:</label>
<input type="email" id="UserEmail">
<label for="PhoneNumber">Phone Number:</label>
<input type="tel" id="PhoneNumber">
<label for="Website">Homepage:</label>
<input type="url" id="Website">
<label for="Quantity">Quantity:</label>
<input type="number" id="Quantity" min="1" max="10" step="1" pattern="/\d/">
<label for="StartDate">Start Date:</label>
<input type="date" id="StartDate" min="2000-01-02" max="2016-01-01">
<label for="FavColor">Favorite Color:</label>
<input type="color" id="FavColor">
<label for="CurrentMonth">Current Month:</label>
<input type="month" id="CurrentMonth">
<label for="CurrentWeek">Current Week:</label>
<input type="week" id="CurrentWeek">
<label for="CurrentTime">Current Time:</label>
<input type="time" id="CurrentTime">
<input type="button" id="submit" value="submit">
</form>
</body>
</html>
This happens because as per your CSS all input elements are 150px to the left of the corresponding label but those are not the same width, so your inputs are not aligned.
You need to make all labels the same width:
.basicForm label{
float:left;
clear:left;
min-width:150px;
}
.basicForm input{
float:left;
}
Instead of min-width you could also use width, whichever you prefer.
If you insist on using absolute positioning, you could wrap each label/input pair in a div so you don't need to position each element individually, check this example:
.input-group {
position: relative;
height:2em;
}
.input-group label {
position: absolute;
top: 0;
left: 0;
}
.input-group input {
position: absolute;
top: 0;
left: 100px;
}
<div class="input-group">
<label>Label 1</label>
<input type="text">
</div>
<div class="input-group">
<label>longer Label</label>
<input type="text">
</div>
<div class="input-group">
<label>short</label>
<input type="text">
</div>
I'm learning some css and want to make a two column form, without any table tags and such.
This is what i have got (code from CSS Cookbook 3ed edition).
JSfiddle HERE...
HTML code:
<div class="container">
<form id="regform" name="regform" method="post" action="/regform.php">
<div id="register">
<h4>Register</h4>
<label for="fmlogin">Login</label>
<input type="text" name="fmlogin" id="fmlogin" />
<label for="fmemail">Email Address</label>
<input type="text" name="fmemail" id="fmemail" />
<label for="fmemail2">Confirm Address</label>
<input type="text" name="fmemail2" id="fmemail2" />
<label for="fmpswd">Password</label>
<input type="password" name="fmpswd" id="fmpswd" />
<label for="fmpswd2">Confirm Password</label>
<input type="password" name="fmpswd2" id="fmpswd2" />
</div>
<div id="contactinfo">
<h4>Contact Information</h4>
<label for="fmfname">First Name</label>
<input type="text" name="fmfname" id="fmfname" />
<label for="fmlname">Last Name</label>
<input type="text" name="fmlname" id="fmlname" />
<label for="fmaddy1">Address 1</label>
<input type="text" name="fmaddy1" id="fmaddy1" />
<label for="fmaddy2">Address 2</label>
<input type="text" name="fmaddy2" id="fmaddy2" />
<label for="fmcity">City</label>
<input type="text" name="fmcity" id="fmcity" />
<label for="fmstate">State or Province</label>
<input type="text" name="fmstate" id="fmstate" />
<label for="fmzip">Zip</label>
<input type="text" name="fmzip" id="fmzip" size="5" />
<label for="fmcountry">Country</label>
<input type="text" name="fmcountry" id="fmcountry" />
<input type="submit" name="submit" value="send" class="submit" />
</div>
</form>
</div>
CSS code:
label {
margin-top: .33em;
display: block;
}
input {
display: block;
width: 250px;
}
#register {
float: left;
}
#contactinfo {
padding-left: 275px;
}
Because you float one div and not the other.
With a few simple CSS changes it'll work (as long as the h4 does not span multiple lines):
#register {
float: left;
width: 275px;
}
#contactinfo {
float: left;
}
See the updated fiddle.
Here's how I'd debug (except I'd use Firebug or another Inspect/devtools): http://jsfiddle.net/PhilippeVay/yuxTA/2/
As stated by #Arjan in his answer, this is due to floating and its effects.
Uncomment the last CSS declaration for a solution that won't modify layout. Also add margin-top to both columns or padding-top if you want a vertical margin back...
Another option is to remove the margins from the h4 (although, as said in other answers, floating [or similar] both columns makes more sense).
h4 {margin: 0;}
You have to float all the div in your containter
#register {
float: left;
}
#contactinfo {
float:left;
margin-left:30px; /*increase or decrease if you like*/
}