I have the following code:
<fieldset>
<legend>
<strong>Personal Information</strong>
</legend>
<ul>
<li>
<label for="first_name">First Name</label><br />
<input type="text" id="first_name" name="first_name" value="" maxlength="30" />
</li>
...
</ul>
</fieldset>
<fieldset>
<legend>
<strong>Car Information</strong>
</legend>
<ul>
<li>
<label for="car_make">Make</label><br />
<input type="text" id="car_make" name="car_make" value="" maxlength="30" />
</li>
...
</ul>
</fieldset>
Right now everything is on the left hand side where the second fieldset (car information) is right below the first fieldset (personal information).
I would like the car information fieldset to be on the right hand side of the personal information fieldset so they are side by side. Using css, how would I do this?
Since fieldsets are considered blocks, you need to use
fieldset {
width: 200px;
float: left;
}
Or whatever you want for the width of the fieldsets.
What you can do is float both fieldsets to the left. The css would look like this:
fieldset {
float: left;
width: 200px;
}
This is going to effect every fieldset on the page, so if you want to isolate only specific fieldsets use css classes:
<style>
.FloatLeft { float: left; width: 200px;}
</style>
<fieldset class="FloatLeft">
What they said, but you can also float the car information right, which gives you the advantage of always being flush against that edge no matter the width, if that's important.
fieldset {
-moz-border-radius:5px;
border-radius: 5px;
-webkit-border-radius: 5px;
width: 200px;
float: left;
}
If you assign a width to your fieldsets and float the to the left, something like this:
HTML:
<fieldset class="myFieldSet">
<legend>
<strong>Personal Information</strong>
</legend>
<ul>
<li>
<label for="first_name">First Name</label><br />
<input type="text" id="first_name" name="first_name" value="" maxlength="30" />
</li>
...
</ul>
</fieldset>
<fieldset class="myFieldSet">
<legend>
<strong>Car Information</strong>
</legend>
<ul>
<li>
<label for="car_make">Make</label><br />
<input type="text" id="car_make" name="car_make" value="" maxlength="30" />
</li>
...
</ul>
</fieldset>
CSS:
.myFieldSet
{
width:300px;
float:left;
}
Related
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'm new to all of this so I have a question and it's probably silly but here we go anyway.
I have this HTML for a form but I need to use CSS to align my labels to the left of the text box and not have it sit on top. I don't know what CSS to use in order to do this.
<form action="process.php">
<h1>Registration</h1>
<ol>
<li>
<label for="name">Name:</label>
<input type>"text" id="name" name="name">
</li>
</ol>
</form>
There are three more "labels" like this in my ol but I don't feel like typing them all out.
I need the labels to the left aligned with my text boxes.
I have tried:
label{display:inline-block}
And:
label ol{display:inline-block}
I've tried giving floats, the text book (yes this is for a college class) says to do this:
.label{
display:inline-block;
}
But that doesn't seem to work either. Please tell me how on earth I can do this.
Here is my exact CSS so far:
h1{font-family:Oregano;}
form{margin-bottom:1em;}
form ol{list-style-type:none;}
form li{width:100px;
border:1px solid black;
text-align:right;
background-color:black;
color:white;
margin:20px;
height:20px;
white-space:5px;}
It looks exactly like it should, I just have an issue with the alignment of the labels. Have I mentioned how much I hate this crap? I'm changing my major! (not really... but still)
Are you looking for something like this? If so, then no CSS is needed.
<ol>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
</ol>
So using your updated CSS here is a Solution:
form {
margin-bottom: 1em;
}
form ol {
list-style-type: none;
}
form li {
margin: 20px;
height: 20px;
white-space: 5px;
}
form label {
width: 100px;
border: 1px solid black;
text-align: right;
background-color: black;
color: white;
}
<form>
<ol>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
<li>
<label for="name">Name:</label>
<input type="text" value="some text" />
</li>
</ol>
</form>
Both the label and the input must me inline/inline-block
label, input{
display:inline-block;
}
They of course must be next to each other, with the label on the left side in your case.
Btw: it's not a good idea to have labels & inputs inside li elements
If you check your example:
<form action="process.php">
<h1>Registration</h1>
<ol>
<li>
<label for="name">Name:</label>
<input type="text" id="name" />
</li>
</ol>
</form>
you will see that its like you want by default.
So it must be a problem in some CSS rule that gives display block to the input or label tag (or both)
Try this:
label, input {display:inline-block !important}
"!important" at the end is there just in case you need to override some other rule.
I have an issue where my style sheet isn't applying equally to all objects of the same type. Here is my HTML:
<form method="post" action="FILLTHISINLATER" name="NewFeaturesRequest" target="_blank">
<fieldset id="CustInfo">
<legend>Customer Information</legend>
<ol>
<p>Please provide the name of the person requesting this new feature.</p>
<li>
<label id="lblFName" for="txtFName">First Name:</label>
<input type="text" name="FName" tabindex="1" id="txtFName" />
</li>
<li>
<label id="lblLName" for="txtLName">Last Name:</label>
<input type="text" name="LName" tabindex="2" id="txtLName" />
</li>
</fieldset>
<fieldset id="FeatureInfo">
<legend>New Feature Information</legend>
<p align="left">What category does this feature fit into?</p>
<div id="radios" align="left">
<ol>
<li>
<input name="fType" type="radio" value="iPhone" />iPhone</li>
<li>
<input name="fType" type="radio" value="Messaging" />Messaging</li>
<li>
<input name="fType" type="radio" value="Contacts" />Contacts</li>
</ol>
</div>
</fieldset>
<fieldset id="AgentInfo">
<legend>Support Agent</legend>
<p>Your Name: <select id="ddbxAGENT" name="ddbxAGENT" onchange="display_unlisted_agent()">
<option selected="selected">Select Your Name</option>
<option value="Allen">Allen</option>
<option value="Ash">Ash</option>
<option value="Beau">Beau</option>
</select>
</p>
</fieldset>
I cut it down quite a bit. Next, my CSS for legend and fieldset (EDITED to include suggestion from misterManSam:
fieldset {
margin:0;
padding:0;
border-top: Green 2px solid;
border-left: none;
border-right: none;
border-bottom:none;
}
legend {
font-weight: bold;
color: Navy;
padding:0;
margin:0;
}
And finally, what it actually looks like:
Why, oh why, is that green border on the second fieldset sticking out so far? The same style is used as on the other two, so what the heck is going on here? It happens in Chrome, IE, and Firefox. It's purely cosmetic, but having an error like that is unprofessional.
There is a width on #infoboxes you need to remove.
#infoboxes {
/*width:650px;*/
float:left;
padding:10px;
}
Here is a Jsfiddle!
The fieldset borders are expanding to the width of the container, whatever that may be. You didn't show the code that's drawing the blue border around the whole form. If I correct the HTML validation errors and put a width on the body tag, I get this, which appears to display correctly, i.e. all the fieldset top borders are the same width:
<head>
<style type="text/css">
body {width: 500px;}
fieldset {
margin:0px;
border-top: Green 2px solid;
border-left: none;
border-right: none;
border-bottom:none;
}
legend {
font-weight: bold;
color: Navy;
}
</style>
</head>
<body>
<form method="post" action="FILLTHISINLATER" name="NewFeaturesRequest" target="_blank">
<fieldset id="CustInfo">
<legend>Customer Information</legend>
<p>Please provide the name of the person requesting this new feature.</p>
<ol>
<li>
<label id="lblFName" for="txtFName">First Name:</label>
<input type="text" name="FName" tabindex="1" id="txtFName" />
</li>
<li>
<label id="lblLName" for="txtLName">Last Name:</label>
<input type="text" name="LName" tabindex="2" id="txtLName" />
</li></ol>
</fieldset>
<fieldset id="FeatureInfo">
<legend>New Feature Information</legend>
<p align="left">What category does this feature fit into?</p>
<div id="radios" align="left">
<ol>
<li>
<input name="fType" type="radio" value="iPhone" />iPhone</li>
<li>
<input name="fType" type="radio" value="Messaging" />Messaging</li>
<li>
<input name="fType" type="radio" value="Contacts" />Contacts</li>
</ol>
</div>
</fieldset>
<fieldset id="AgentInfo">
<legend>Support Agent</legend>
<p>Your Name: <select id="ddbxAGENT" name="ddbxAGENT" onchange="display_unlisted_agent()">
<option selected="selected">Select Your Name</option>
<option value="Allen">Allen</option>
<option value="Ash">Ash</option>
<option value="Beau">Beau</option>
</select>
</p>
</fieldset>
I note some align="left" attributes which should be replaced with CSS, but they do not appear to be the cause of the problem.
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>
I am just trying to float an unordered list left, and a set of textboxes to the right so that they are adjacent to each other and have a uniform look within a div tag. The issue is that the text boxes are to the right ... but are positioned below the ul items
.PersonLI
{
float: left;
clear: both;
width: 100px;
margin-bottom: 10px;
}
.PersonBox
{
float: right;
clear: both;
width: 99px;
margin-bottom: 10px;
}
.FirstObj
{
border: 1px solid black;
margin-left: 100px;
width: 300px;
height: 200px;
}
<div class="FirstObj">
<ul style="list-style: none;">
<li class="PersonLI">First Name:</li>
<li class="PersonLI">Last Name:</li>
<li class="PersonLI">Address:</li>
<li class="PersonLI">City:</li>
<li class="PersonLI">State:</li>
<li class="PersonLI">Zip Code:</li>
</ul>
<input id="txtFname" type="text" value="" class="PersonBox"/>
<input id="txtLname" type="text" value="" class="PersonBox"/>
<input id="txtAddr" type="text" value="" class="PersonBox"/>
<input id="txtCity" type="text" value="" class="PersonBox"/>
<input id="txtState" type="text" value="" class="PersonBox"/>
<input id="txtZip" type="text" value="" class="PersonBox"/>
</div>
Could it be that I need to NOT clear the float on the last list item?
Your markup is kind of weird. A semantic form adapting your styles would look like this:
.FirstObj ul {
list-style: none;
}
.FirstObj li {
margin-bottom: 10px;
clear: both;
}
.FirstObj label {
width: 100px;
float: left;
}
.FirstObj input {
float: right;
width: 99px
}
<div class="FirstObj">
<ul>
<li>
<label for="txtFname">First Name:</label>
<input id="txtFname" type="text" value="" />
</li><li>
<label for="txtLname">Last Name:</label>
<input id="txtLname" type="text" value="" />
</li><li>
<label for="txtAddr">Address:</label>
<input id="txtAddr" type="text" value="" />
</li><li>
<label for="txtCity">City:</label>
<input id="txtCity" type="text" value="" />
</li><li>
<label for="txtState">State:</label>
<input id="txtState" type="text" value="" />
</li><li>
<label for="txtZip">Zip Code:</label>
<input id="txtZip" type="text" value="" />
</li>
</ul>
</div>
It's alway a good idea to use labels. Here's the working version: http://jsfiddle.net/Fmzbm/
Is there any specific reason why you are not using <label> tags for these fields?
To answer your CSS question, clear:both is not needed on either of the elements if you want them side by side.
Consider changing your markup:
HTML:
First Name:
CSS:
.FirstObj label { float:left; }
.FirstObj input { float:right; }
The code hinting is jacked up, need to try some more formatting.
Demo http://jsfiddle.net/qnev2/
You may want to change the fixed width of the li CSS rule to suit your needs but also change the markup and use the more semantically correct label tag. This also avoids the float property which in my experience can lead to undesirable behaviour if the HTML is re-flowed.