Align input controls on page - html

I have a form with 4 textboxes, a checkbox and a select with their labels on their left. Here's my code:
<fieldset class="form-field">
<legend>
<label class="form-field k-checkbox">Parent Material</label>
</legend>
<ul class="form-field">
<li>
<label for="use_test_certificate" class="form-field">Use Test Certificate</label>
<input type="checkbox" name="use_test_certificate" class="k-checkbox form-field" />
</li>
<li>
<label for="parent_material" class="form-field">Material</label>
<select name="parent_material" class="form-field"></select>
</li>
<li>
<label for="proof_stress" class="form-field">0.2% Proof Stress</label>
<input type="text" name="proof_stress" size="24" maxlength="23" style="width:170px;" class="k-textbox form-field" />
</li>
<li>
<label for="do_weld_calculation" class="form-field">Do Weld Calculation</label>
<input type="checkbox" name="do_weld_calculation" class="k-checkbox form-field" />
</li>
<li>
<label for="weld_redistribution_factor" class="form-field">Weld Redistribution Factor</label>
<input type="text" name="weld_redistribution_factor" size="24" maxlength="23" style="width:170px;" class="k-textbox form-field" />
</li>
</ul>
</fieldset>
Problem is that the select displays next to the checkbox and not next to it's label. And when I increase the window size it jumps up to next to my first textbox. How can I tell it to show next to it's label. I'm not a CSS expert. I normally can work things out for myself, but this one baffles me completely. Any help is appreciated.
Amanda

Seeing as you haven't shown your CSS, I might as well show you the ideal solution using nothing but form elements :)
Note how I have changed the name attributes to id. The id, which needs to be unique, is used to identify the corresponding label by means of the labels for attribute.
Have a look at this fiddle!
CSS
fieldset {
width: 500px;
}
label {
display: block;
float: left;
clear: left;
width: 200px;
margin: 10px;
}
input, select {
display: block;
float: left;
margin: 10px;
}
input[type=text] {
width: 170px;
}
HTML
<fieldset class="form-field">
<legend>Parent Material</legend>
<label for="use_test_certificate" class="form-field">Use Test Certificate</label>
<input type="checkbox" id="use_test_certificate" class="k-checkbox form-field" />
<label for="parent_material" class="form-field">Material</label>
<select id="parent_material" class="form-field"></select>
<label for="proof_stress" class="form-field">0.2% Proof Stress</label>
<input type="text" id="proof_stress" maxlength="23" class="k-textbox form-field" />
<label for="do_weld_calculation" class="form-field">Do Weld Calculation</label>
<input type="checkbox" id="do_weld_calculation" class="k-checkbox form-field" />
<label for="weld_redistribution_factor" class="form-field">Weld Redistribution Factor</label>
<input type="text" id="weld_redistribution_factor" maxlength="23" class="k-textbox form-field" />
</fieldset>

Related

Layout form with CSS so that form is centered but with left aligned variable width fields and right aligned button

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.

Why are my HTML forms appearing differently in Sharepoint and jsfiddle?

I'm mocking up a Sharepoint Web Part in this fiddle and it currently looks like this (the second form has yet to be formatted/aligned, as you can see):
(even this, in addition to the ugly formatting on the second form, is not perfect, as the second form aligns a "row" below where I'd like it to be).
I'm using the exact same CSS and HTML on a Sharepoint Web Part, and it is considerably different:
I want the second form to the right, not below, the first form. The Sharepoint representation does not seem to restrict the form widths to allow this, though. Why the difference, and how can I make the Sharepoint Web Page more closely mimic the jsfiddle version?
Here is the code (again, exactly the same in both places):
CSS
.underline {
text-decoration: underline;
}
input[placeholder] {
font-size: 0.6em;
width: 500;
}
.firstblocklabel {
width: 160px;
display: inline-block;
margin: 2px;
}
.firstblockdatelabel {
width: 108px;
display: inline-block;
}
.firstblockinput {
width: 224px;
}
.dateinput {
width: 124px;
margin: 2px;
}
.timeinput {
width: 100px;
}
form {
display: inline-block;
vertical-align:top;
//float: left;
}
.borderedform {
border: 1px solid;
margin: 1em;
padding: 1em;
}
.reditalics {
color: red;
font-style: italic;
}
.wrappedlabel { width: 320px; display: block; }
HTML
<h2 class="underline">UCSC POST TRAVEL EXPENSE</h2>
<label>Form Filled Out:</label>
<input type="date" style="width=100px"></input>
<label>Access the Post Travel Guide for reimbursement validation</label>
<br/>
<br/>
<form>
<label class="firstblocklabel">Traveler's name:</label>
<input class="firstblockinput" type="text" id="travelername" title="Last Name, First Name, Middle Initial" />
<br/>
<label class="firstblocklabel">Traveler's E-mail:</label>
<input class="firstblockinput" type="email" id="traveleremail" />
<br/>
<label class="firstblocklabel">Traveler's Phone:</label>
<input class="firstblockinput" type="tel" id="travelerphone" />
<br/>
<label class="firstblocklabel">Traveler's Mail Stop:</label>
<input class="firstblockinput" type="text" id="travelermailstop" />
<br/>
<label class="firstblocklabel" id="travelerstreetorpoboxlabel">Traveler's Street or P.O.:</label>
<input class="firstblockinput" type="text" id="travelerstreetorpobox" />
<br/>
<label class="firstblocklabel">Traveler's Destinations:</label>
<input class="firstblockinput" type="text" id="travelersdestinations" />
<br/>
<label class="firstblocklabel">UC Business Purpose:</label>
<input class="firstblockinput" type="text" id="ucbusinesspurpose" />
<br/>
<label class="firstblockdatelabel">Departure Date:</label>
<input class="dateinput" type="date" id="travelersdeparturedate" />
<label>Time:</label>
<input class="timeinput" type="time" id="travelersdeparturetime" />
<br/>
<label class="firstblockdatelabel">Return Date:</label>
<input class="dateinput" type="date" id="travelersreturndate" />
<label>Time:</label>
<input class="timeinput" type="time" id="travelersreturntime" />
</form>
<form class="borderedform">
<label>Trip Number:</label>
<input type="text" id="tripnumber" title="If Applicable" />
<br/>
<label>Form prepared by:</label>
<input type="text" id="formpreparedby" />
<hr/>
<label>Dept / Division</label>
<input type="text" id="deptdivision" />
<br/>
<label>Email:</label>
<input type="email" id="email" />
<label>Ext:</label>
<input type="text" id="extension" />
<hr/>
<label class="reditalics">Required:</label>
<label>US Citizen - YES</label>
<input type="radio" id="uscitizenyes" value="YES" />
<label>NO</label>
<input type="radio" id="uscitizenno" value="NO" />
<br/>
<input type="radio" id="visitor" />Visitor
<label>Foreign Visa Type:</label>
<select title="Please select a visa type">
<option value="pleaseselect">Please Select</option>
. . .
</select>
<br/>
<input type="radio" id="ucstudent" />UC Student
<br/>
<input type="radio" id="ucemployee" />UC Employee
<label>UC Campus:</label>
<select title="Please select a campus">
<option value="pleaseselect">Please Select</option>
. . .
</select>
<br/>
<label class="wrappedlabel"><span style="color:red">Note: </span>If traveler chooses to include personal travel, record times/dates based only on the business portion of the trip. Provide explanation of personal travel.</label>
</form>
UPDATE
Adding Sully's CSS and HTML, the jsfiddle looks better, but the same additions to the Sharepoint Web Part don't change it at all there.
Maybe Sharepoint disallows multiple forms to be on the same "row"?
UPDATE 2
Unfortunately but not surprisingly, it looks different in Chrome (the other non-fiddle scream shot was IE):
Try making your forms float:left.
Bonus: for borderedform, use margin-left instead of just margin in order to remove that vertical space at the top.
CSS
.firstform { float: left; }
.borderedform {
float: left;
border: 1px solid;
margin-left: 1em;
padding: 1em;
}
To use that CSS, add class="firstform" to the first form.
http://jsfiddle.net/z8y6L303/

Placing form elements on new lines without <br>

I'm trying to create a form to use for my work, I guess my question is more of a why does this happen.
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
my css class of "checkbox" looks like this
.checkbox {
float: left;
display: inline;
}
now my code at the next element
<div>First name:<br>
<input type="text" name="firstname"><br>
</div><br><br><br>
I have to add 3 <br>'s to get the "First name:" to be on a new line. I started with only 2 radio buttons and then I only needed 2 <br>'s. Is there a way to format my css to not need any <br>'s?
I think I need the <br>'s (correct me if I'm wrong) due to the fact that html file is reading the radio buttons as new lines and displaying them on one line, therefore the <br>'s fix that issue, but I don't like using them nor do I think it is semantically correct.
Let's start with a nicely marked up form
The form elements
The radio buttons can be wrapped in a <fieldset> element
The labels can all be marked up with <label> elements. The for attribute links to its input via the matching id attribute. One benefit of this is that users can click/touch on the label.
That gives us this:
<form>
<fieldset class="checkbox">
<input type="radio" name="transport_method" id="delivery">
<label for="delivery">Delivery</label>
<input type="radio" name="transport_method" id="pick-up">
<label for="pick-up">Store Pick-Up</label>
<input type="radio" name="transport_method" id="day-trip">
<label for="day-trip">Day Trip</label>
</fieldset>
<fieldset class="names">
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname">
</fieldset>
</form>
Bring each text input onto a new line
The default display value for inputs is display: inline which brings them all onto one line. Use display: block on text inputs to knock them down:
input[type=text] {
display: block;
}
We want the radio buttons to remain on the one line, so they can be left at their default display: inline. More information on display.
Full example
Bring it all together with a little bit more CSS:
input[type=text] {
display: block;
margin: 5px 0;
}
input[type=radio] + label {
margin-right: 10px;
}
label,
input[type=radio] {
cursor: pointer;
}
fieldset {
border: none;
}
form {
background: #FFF9C4;
width: 500px;
font-family: sans-serif;
}
<form>
<fieldset class="checkbox">
<input type="radio" name="transport_method" id="delivery">
<label for="delivery">Delivery</label>
<input type="radio" name="transport_method" id="pick-up">
<label for="pick-up">Store Pick-Up</label>
<input type="radio" name="transport_method" id="day-trip">
<label for="day-trip">Day Trip</label>
</fieldset>
<fieldset class="names">
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname">
</fieldset>
</form>
Try like this: Demo
<div class="checkbox">
<input type="radio" name="transport_method">Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method">Day Trip</div>
<div class="clear"></div>
<div>First name:
<input type="text" name="firstname">
</div>
.clear{clear:both} instead of <br/>
EDIT: If you dont want to create new class you can use like this too :
Updated dmo
.checkbox::after {
display:block;
clear:both;
content:"";
}
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
<div class="clear"></div>
<div>
First name:
<br>
<input type="text" name="firstname"><br>
</div>
<div class="clear"></div>
in css
.clear{
clear:both
}
It's as simple as this:
.checkbox{display:block}
And if you mean to have those checbox inputs floated to left, then use
.checkbox input{display:inline-block}
And there you go, no floats, no br tags, nothing weird
Using the new class amit made
use .clear{clear:both} instead of
on the following element, in my case
<div >First name:<br>
<input type="text" name="firstname"><br>
</div>
turned into
<div class="clear">First name:<br>
<input type="text" name="firstname"><br>
</div>

Adjusting the floating elements on a webpage

I'm trying to achieve the following result:
Email Password
[Email box] [Password Box] [Log In]
[]keep me logged in Forgot Password?
I've been successful in implementing the first two lines (Email ... [Log In]) but can't shape the last line.
Here's the code.
<ul>
<li>
<label for="mail">Email: </label><br>
<input type="email" name="mail" /><br>
<input type="checkbox" name="stayLoggedIn" />
<small>Keep me signed in</small>
</li>
<li>
<label for="paswrd">Password: </label><br>
<input type="password" name="paswrd" /><br>
<small>Forgot Password?</small>
</li>
<li><button type="button" name="login">Log In</button></li><br><p> </p>
</ul>
The CSS code is as follows.
header ul {float: right;}
header ul li {
display: inline-block;
color: #FFEBCD;
font-family: "Maiandra GD";
}
header small {float: right;}
You will need to do this with two div's one float to the left the second float to the right
see an example
<div class="left_div">
<label for="mail">Email: </label><br>
<input type="email" name="mail" /><br>
<input type="checkbox" name="stayLoggedIn" />
<small>Keep me signed in</small>
</div>
<div class="right_div">
<label for="paswrd">Password: </label><br>
<input type="password" name="paswrd" /><br>
<small>Forgot Password?</small>
</div>
The CSS code
.left_div {
float: left;
width: 300px;
}
.right_div {
float: right;
width: 300px;
}

i want to place a div nearer to a text box in an html form

can anyone help me?
I need to place a div after a textbox in a html form.
ie.label,textbox,and new div is in same line
please see my html code .i didn't add div code yet.
please can any one help me to add a div in same line without any modification to this codes.
because i made several css codes for aligning this labels and text boxes
<form action="operates/signup.php" method="post" name="signupform" id="signupform">
<label id="fnamelabel" for="fnam">First Name :</label>
<input type="text" name="fnam" id="fnam" tabindex="1" />
<p>
<label id="lnamelabel" for="lnam">Last Name :</label>
<input type="text" name="lnam" id="lnam" tabindex="2" />
</p>
<p>
<label id="yemail" for="email">Your Email :</label>
<input type="text" name="email" id="email" tabindex="3" />
</p>
<p>
<label id="reemail" for="remail">Re-enter Email :</label>
<input type="text" name="remail" id="remail" tabindex="4" />
</p>
<p>
<label id="npass" for="password">New Password :</label>
<input type="text" name="password" id="password" tabindex="5" />
</p>
<p>
<label id="mskill" for="bskill">Main Skill :</label>
<select name="bskill" id="bskill" tabindex="6">
</select>
</p>
<p>
<input type="checkbox" name="termsanc" id="termsanc" tabindex="6" />
<label id="terms" for="termsanc">I agreed the Terms and Conditions</label>
</p>
<div id="signupbutton" onclick="document.forms.signupform.submit()"></div>
</form>
Thank you
You can style the div as inline, but you should rather use a span.
<label id="fnamelabel" for="fnam" style = "display:inline">First Name :</label>
<input type="text" name="fnam" id="fnam" tabindex="1" style = "display:inline" />
<div id="newDiv" style = "display:inline"></div>
normally I wouldn't use in-line CSS like that, but as you didn't post the css i felt it'd be necessary.
First of all, let's work on that markup!
<form action="operates/signup.php" method="post" name="signup_form">
<label>First Name:
<input name="first_name"></label>
<label>Last Name:
<input name="last_name"></label>
<label>Your Email:
<input type="email" name="email"></label>
<label>Please Reenter Your Email:
<input type="email" name="validate_email"></label>
<label>New Password:
<input type="password" name="password"></label>
<label>Main Skill:
<input name="main_skill"></label>
<label><input type="checkbox" name="terms_and_conditions">I agreed the Terms and Conditions</label>
<button type="submit">Submit</button>
</form>
<style type="text/css">
form {
display: block;
width: 400px;
}
label {
display: block;
padding: 5px;
margin: 5px;
}
form label input {
float: right;
}
input[type=checkbox] {
float: none;
}
</style>
There, now doesn't that look much better?
As for the original question, don't use a div, div is a completely-unsemantic block-level element. If you want an inline element (i.e. to show on the same line), use a span, which is a completely-unsemantic inline-level element.