Grouping form elements together - css

I'm trying to create two css container classes that can be used to:
Vertically align form elements using .group.
Horizontally align form elements using .group.group--inline.
Each for element will use the class .group__item to make sure there's 16px vertical and horizontal distance between the form elements. For example:
.group__item { margin-top: 16px; }
I however want to sure that the entire height and width of the .group can be used for the form elements and that there is no unwanted whitespace. Not having any margin around our components makes it easier to properly layout them.
To negate the margin on the .group__item's I'm adding it as negative margin to the .group and .group--inline. For example:
.group { margin-top: -16px; }
I'm wondering if there are any negative side effects to giving the .group container a negative margin?
function toggleGroupBorder() {
var groups = document.querySelectorAll('.group');
for (var i = 0, j = groups.length; i < j; i++) {
groups[i].classList.toggle('group--show-border');
}
}
.container {
margin: 32px;
padding: 32px;
border: 1px solid #99f;
}
.group {
margin-top: -16px;
}
.group.group--show-border {
border: 1px solid #f99;
}
.group .group__item {
display: block;
margin-top: 16px;
}
.group.group--inline {
margin-left: -16px;
}
.group.group--inline .group__item {
margin-left: 16px;
display: inline-block;
}
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial;
margin: 0;
}
input {
height: 32px;
padding: 0 8px;
}
button {
height: 32px;
padding: 0 24px;
border: none;
}
<button onclick="toggleGroupBorder();">Toggle Group Border</button>
<h2>Vertical field alignment using <code>.group</code></h2>
<div class="container">
<div class="group">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
<h2>Horizontal field alignment using <code>.group.group-inline</code></h2>
<div class="container">
<div class="group group--inline">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
Or see this CodePen

Related

Why isn't this flexbox code displaying half of my checkboxes in each of the child div elements in my container?

I'm trying to get two groups of checkboxes to display side by side in my parent flex container to make more room for some additional elements on my webpage. I have coded it per below and following the training on had on flex but I'm obviously missing something. When this area of my page is displayed, it is still just a vertical column with all of the checkboxes instead of two groups side my side. Can someone help me figure out what I'm doing wrong?
img {
width: 100%;
height: auto;
}
#larryfoto {
max-width: 90px;
}
#brochure {
max-width: 70px;
}
.headertd {
width: 34%;
}
#socialcontainer {
padding: 20px;
}
#headerlogo {
width: 85%;
display: block;
margin-left: 0px;
margin-right: auto;
}
#fb {
width: 33%;
margin-left: 40px;
margin-right: auto;
}
#linkedin {
width: 33%;
margin-left: 40px;
margin-right: auto;
}
#ig {
width: 33%;
margin-left: 40px;
margin-right: auto;
}
#warrantywisdom {
margin-left: 0px;
margin-rignt: auto;
}
input[type=text],
select {
width: 100%;
padding: 7px 10px;
margin: 5px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 10px;
box-sizing: border-box;
}
input[type=radio] label {}
input[type=submit] {
width: 100%;
}
#bodydiv {
margin-left: 30px;
margin-right: 30px;
background-color: transparent;
}
#headerdiv {
margin-left: 35px;
margin-right: 30px;
border-radius: 10px;
}
.warranty_option_container {
display: flex;
flex-direction: row;
justify-content: space-between;
}
.wty_option_child {
width: 50%;
height: 50%;
margin: auto;
}
table {
border-spacing: 10px;
width: 100%;
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
.toolbox {
font-family: Arial, Helvetica, sans-serif;
font-size: 12px;
}
#inputcell {
margin-left: 20px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
width: 33%;
vertical-align: top;
}
#radiocell {
margin-left: 20px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
width: 33%;
vertical-align: top;
}
#contactinfocell {
margin-left: 20px;
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
width: 33%;
vertical-align: top;
}
body {
background-image: url('hwa_house_bkgrnd.jpg');
width: 100%;
position: absolute;
top: 0;
left: 0;
}
form {
padding: 5px;
}
body {
font-family: Arial, Helvetica, sans-serif;
}
#myImg {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg:hover {
opacity: 0.7;
}
#myImg2 {
border-radius: 5px;
cursor: pointer;
transition: 0.3s;
}
#myImg2:hover {
opacity: 0.7;
}
/* The Modal (background) */
.modal {
display: none;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 1;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 0;
top: 0;
width: 100%;
/* Full width */
height: 100%;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(0, 0, 0);
/* Fallback color */
background-color: rgba(0, 0, 0, .8);
/* Black w/ opacity */
}
/* Modal Content (image) */
.modal-content {
margin: auto;
display: block;
width: 80%;
max-width: 1000px;
}
/* Caption of Modal Image */
#caption {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Caption of Modal Image */
#caption2 {
margin: auto;
display: block;
width: 80%;
max-width: 700px;
text-align: center;
color: #ccc;
padding: 10px 0;
height: 150px;
}
/* Add Animation */
.modal-content,
#caption {
-webkit-animation-name: zoom;
-webkit-animation-duration: 0.6s;
animation-name: zoom;
animation-duration: 0.6s;
}
#-webkit-keyframes zoom {
from {
-webkit-transform: scale(0)
}
to {
-webkit-transform: scale(1)
}
}
#keyframes zoom {
from {
transform: scale(0)
}
to {
transform: scale(1)
}
}
/* The Close Button */
.close {
position: absolute;
top: 15px;
right: 35px;
color: #f1f1f1;
font-size: 40px;
font-weight: bold;
transition: 0.3s;
}
.close:hover,
.close:focus {
color: #bbb;
text-decoration: none;
cursor: pointer;
}
/* 100% Image Width on Smaller Screens */
#media only screen and (max-width: 700px) {
.modal-content {
width: 100%;
}
}
<div id="headerdiv">
<table>
<tr>
<td class="headertd"><img src="hwa_header.jpg" id="headerlogo"></td>
<td class="headertd"><img src="header2.jpg" id="headerlogo2"></td>
<td id="socialcontainer">
<td class="socialtd" id="socialcell">
<a href="https://www.facebook.com/profile.php?id=100008339314922"><img src="fb.png" id="fb"></td>
<td class="socialtd">
<a href="https://www.linkedin.com/in/larry-hines-66a8a21/"><img src="linkedin.png" id="linkedin"></td>
<td class="socialtd">
<a href="https://www.instagram.com/lwhmicrovision/"><img src="ig.png" id="ig"></td>
</td>
</tr>
</table>
</div>
<div id="bodydiv">
<form class="input-form" name="textinput" id="input_form" method="POST">
<table id="formtable">
<tr id="mainrow">
<td id="inputcell">
<label for="propertyaddress">Covered Property Address</label>
<input type="text" id="propertyaddress" name="propertyaddress" required>
<label for="mailingaddress">Mailing Address (If Different)</label>
<input type="text" id="mailingaddress" name="mailingaddress">
<label for="buyername">Buyer Name(s)</label>
<input type="text" id="buyername" name="buyername" required>
<label for="buyeremail">Buyer Email Address</label><br>
<input type="text" id="buyeremail" name="buyeremail" required><br>
<label for="buyerphone">Buyer Phone</label><br>
<input type="text" id="buyerphone" name="buyerphone"><br>
<label for="titlecompany">Title Company</label><br>
<input type="text" id="titlecompany" name="titlecompany"><br>
<label for="escrowofficer">Escrow Officer Name</label><br>
<input type="text" id="escrowofficer" name="escrowofficer"><br>
<label for="escrowofficeremail">Escrow Officer Email Address</label><br>
<input type="text" id="escrowofficeremail" name="escrowofficeremail"><br>
<label for="escrowofficerphone">Escrow Officer Phone</label><br>
<input type="text" id="escrowofficerphone" name="escrowofficerphone"><br>
<label for="referringagent">Agent Name and Agency</label><br>
<input type="text" id="referringagent" name="referringagent" required><br>
<label for="referringagentphone">Agent Phone</label><br>
<input type="text" id="referringagentphone" name="referringagentphone" required><br>
<label for="referringagentemail">Agent Email Address</label><br>
<input type="text" id="referringagentemail" name="referringagentemail" required><br>
</td>
<td id="radiocell">
<table>
<tr>
<td>
<label for="closingdate"><u>Closing Date</u></label><br>
<input type="date" id="closingdate" name="closingdate" required><br><br>
<label for="hometype"><u>Type of Home</u></label><br><br>
<input type="radio" id="Single Family" name="hometype" value="singlefamily" required><label for="Single Family">Single Family</label><br>
<input type="radio" id="Townhome/Condo/Mobile Home" name="hometype" value="towncondomobile"><label for="Townhome/Condo/Mobile Home">Townhome/Condo/Mobile Home</label><br>
<select form="input_form" name="multitype" id="duplex_triplex_fourplex" value="Multi-Flat Warranty Options">
<option value="" selected disabled hidden>Select Multi-Flat Option</option>
<option value="duplex_gold">Duplex Gold - $720</option>
<option value="duplex_platinum">Duplex Platinum - $855</option>
<option value="duplex_diamond">Duplex Diamond - $945</option>
<option value="triplex_gold">Triplex Gold - $1040</option>
<option value="triplex_platinum">Triplex Platinum - $1235</option>
<option value="triplex_gold">Triplex Diamond - $1365</option>
<option value="triplex_gold">Fourplex Gold - $1360</option>
<option value="triplex_platinum">Fourplex Platinum - $1615</option>
<option value="triplex_gold">Fourplex Diamond - $1785</option>
</select>
</td>
<td>
<label for="warrantytype"><u>Select Warranty Type</u></label><br><br>
<input type="radio" id="Gold" name="warrantytype" value="Gold" required><label for="Gold">Gold - $400</label><br>
<input type="radio" id="Platinum" name="warrantytype" value="Platinum"><label for="Platinum">Platinum - $475</label><br>
<input type="radio" id="Diamond" name="warrantytype" value="Diamond"><label for="Diamond">Diamond - $525</label><br>
<input type="radio" id="Sellers" name="warrantytype" value="Sellers"><label for="Sellers">Sellers Warranty</label><br>
<input type="radio" id="New Construction" name="warrantytype" value="New Construction"><label for="New Construction">New Construction</label><br>
<input type="radio" id="Multiyear" name="warrantytype" value="Multi-Year"><label for="Multiyear">Multi-Year Warranty</label><br><br>
</td>
</tr>
</table>
<label for="warrantynotes" title="Describe warranty type if new construction or multi-year, also please note if warranty to be paid by proceeds at closing or directly by buyer, seller or real estate agent"><u>Warranty Notes - (hover mouse over this label for instructions)</u></label><br>
<input type="text" id="notes" name="warrantynotes"><br>
<label for="options"><u>Options</u></label><br>
<div class="warranty_option_container">
<div class="wty_option_child">
<input type="checkbox" id="greenplus" name="optiontype[]" value="greenplus"><label for="greenplus">$70 Green Plus</label><br>
<input type="checkbox" id="termite" name="optiontype[]" value="termite"><label for="termite">$75 Subterranean Termite Treatment</label><br>
<input type="checkbox" id="freezer" name="optiontype[]" value="freezer"><label for="freezer">$50 Freezer-Standalone</label><br>
<input type="checkbox" id="wetbar" name="optiontype[]" value="wetbar"><label for="wetbar">$25 Wet Bar Refrigerator/2nd Fridge</label><br>
<input type="checkbox" id="poolspa" name="optiontype[]" value="poolspa"><label for="poolspa">$150 Pool/Spa Combo</label><br>
<input type="checkbox" id="addpoolspa" name="optiontype[]" value="addpoolspa"><label for="addpoolspa">$150 Additional Pool or Spa</label>
</div>
<div class="wty_option_child">
<input type="checkbox" id="saltpool" name="optiontype[]" value="saltpool"><label for="saltpool">$300 Salt Water Pool w/Spa Combo</label><br>
<input type="checkbox" id="wellpump" name="optiontype[]" value="wellpump"><label for="wellpump">$100 Well Pump</label><br>
<input type="checkbox" id="septicpump" name="optiontype[]" value="septicpump"><label for="septicpump">$75 Septic /Ejector Pump/Tank Pumping</label><br>
<input type="checkbox" id="waterline" name="optiontype[]" value="waterline"><label for="waterline">$90 External Water Line Repair</label><br>
<input type="checkbox" id="waterlineandsewer" name="optiontype[]" value="waterlineandsewer"><label for="waterlineandsewer">$195 External Water/Sewer Line Repair</label>
</div>
</div>
<input type="text" id="sendername" name="sendername" required value="Enter Your Name Here"><br>
<input type="text" id="orderemail" name="orderemail" required value="Enter Your Email Address"><br><br>
<input type="submit" id="placeorder" name="placeorder" style="width:100%;" value="Place Warranty Order"><br><br> *Payment is not required at time of order. You will receive an invoice after the order is placed. Contact me to discuss payment
options.
</td>
<td id="contactinfocell" class="toolbox">
<h2>Warranty Toolbox</h2>
<hr>
<table>
<tr>
<td id="warrantycell">
<!-- Trigger the Modal -->
<img id="myImg" src="updated_warranty_matrix.jpg" alt="Warranty Option Matrix" style="width:100%;max-width:110px"><br> View Warranty Options
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- The Close Button -->
<span class="close" id="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img01">
<!-- Modal Caption (Image Text) -->
<div id="caption"></div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg");
var modalImg = document.getElementById("img01");
var captionText = document.getElementById("caption");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
<br>
</td>
<td>
<!-- Trigger the Modal -->
<img id="myImg2" src="file_claim.jpg" alt="File a Claim" style="width:100%;max-width:125px"><br> File a Claim
<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img02">
<!-- Modal Caption (Image Text) -->
<div id="caption2"></div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal2");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg2");
var modalImg = document.getElementById("img02");
var captionText = document.getElementById("caption2");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = this.src;
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[1];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
</td>
<td "brochurecell">
<img src="brochure_thumb.jpg" width="70" height="95" id="brochure"><br>Brochure
</td>
</tr>
</table>
<hr>
<table>
<tr>
<td id="warrantywisdom" class="toolbox">
<ul class="doubleBullets">
<u><b>Watch our Warranty Wisdom Videos to get the most from your home warranty!</b></u><br><br>
<ul class="doubleBullets">
<li>Understand HWA Home Warranty Coverage</li>
<li>Home Warranty vs. Homeowners Insurance - What's the Difference?</li>
<li>Why Choose HWA</li>
<li>Home Warranty of America (HWA): Who We Are</li>
<li>How Do I Know What Coverage I Need for My Home Warranty?</li>
</ul>
<ul class="doubleBullets">
<li>What's Covered by an HWA Home Warranty?</li>
<li>How Do I File a Claim with Home Warranty of America?</li>
<li>Why Was My Claim Rejected?</li>
</ul><br>
</td>
</tr>
</table>
<hr>
<table>
<tr>
<td id="contactme" class="toolbox">
<u><b>Contact Me</b></u><br><br> Larry Hines<br> Account Executive, Idaho<br> Home Warranty of America<br> (208) 761-1666<br>
larry.hines#hwahomewarranty.com
</td>
<td><img src="larry_bc_photo.jpg" id="larryfoto">
</td>
</tr>
</table>
<hr>
<table>
<tr>
<td id="promotext">
<h2>Check out our new<br>Spring Promo!</h2>
</td>
<td width="100px;">
<img src="arrow.png" style="width:100%; max-width:80px;">
</td>
<td id="arlocell">
<!-- Trigger the Modal -->
<img id="myImg3" src="arlo_thumbnail.JPG" alt="Spring Promo" style="width:100%;max-width:80px;"><br>
<!-- The Modal -->
<div id="myModal3" class="modal">
<!-- The Close Button -->
<span class="close">×</span>
<!-- Modal Content (The Image) -->
<img class="modal-content" id="img03">
<!-- Modal Caption (Image Text) -->
<div id="caption3"></div>
</div>
<script>
// Get the modal
var modal = document.getElementById("myModal3");
// Get the image and insert it inside the modal - use its "alt" text as a caption
var img = document.getElementById("myImg3");
var modalImg = document.getElementById("img03");
var captionText = document.getElementById("caption3");
img.onclick = function() {
modal.style.display = "block";
modalImg.src = "HWA_Arlo_flyer.jpg";
captionText.innerHTML = this.alt;
}
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[2];
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
</script>
</td>
</tr>
</table>
<hr>
</tr>
</table>
</td>
</tr>
</table>
</form>
</div>
Here is the whole .CSS file. I don't see anything competing but it's worth mentioning that the container and child div's in my flexbox are all inside of a element that is part of a 3 column table that lays out the format of the page. Does flexbox work inside of a TD?

set text inside a check box

Hi is it possible to add text inside check box replacing the tick icon.
I am could not achieve it with this code could someone suggest me how to make a size selection box with a text inside it
<ion-col>
<p>Choose the size</p>
<ion-item>
<ion-checkbox >S</ion-checkbox>
</ion-item>
</ion-col>
Help me to bring out this type of UI
You can hide the input and use the label to select the checkbox. Then style your label like in the example below, using :not(:checked) and :checked selectors. Same logic can be applied to radio buttons.
ul {
padding: 0;
margin: 0;
clear: both;
}
li{
list-style-type: none;
list-style-position: outside;
padding: 10px;
float: left;
}
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999%;
}
input[type="checkbox"] + label {
display: inline-block;
padding: 10px;
cursor: pointer;
border: 1px solid black;
color: black;
background-color: white;
margin-bottom: 10px;
}
input[type="checkbox"]:checked + label {
border: 1px solid white;
color: white;
background-color: black;
}
<ul>
<li>
<input type="checkbox" id="check_1" name="check_1" value="check_1">
<label for="check_1">S</label>
</li>
<li>
<input type="checkbox" id="check_2" name="check_2" value="check_2">
<label for="check_2">M</label>
</li>
<li>
<input type="checkbox" id="check_3" name="check_3" value="check_3">
<label for="check_3">L</label>
</li>
<li>
<input type="checkbox" id="check_4" name="check_4" value="check_4">
<label for="check_4">XL</label>
</li>
</ul>
Updated
check updated demo here
Add the following Js to get the relevant radio value
JS:
$("body").on("click", "label", function(e) {
var getValue = $(this).attr("for");
var goToParent = $(this).parents(".select-size");
var getInputRadio = goToParent.find("input[id = " + getValue + "]");
console.log(getInputRadio.attr("id"));
});
I assume that the user select only one size at a time. if user can select the multiple size modify the example with checkbox.
Try this
Check Demo here
HTML:
<div class="select-size">
<input type="radio" name="s-size" id="small" checked/>
<input type="radio" name="s-size" id="medium" />
<input type="radio" name="s-size" id="large" />
<input type="radio" name="s-size" id="x-large" />
<input type="radio" name="s-size" id="xx-large" />
<label for="small">S</label>
<label for="medium">M</label>
<label for="large">L</label>
<label for="x-large">XL</label>
<label for="xx-large">XXL</label>
</div>
CSS:
.select-size input{
display: none;
}
label {
display: inline-block;
width: 50px;
height: 50px;
text-align: center;
border: 1px solid #ddd;
line-height: 50px;
cursor: pointer
}
#small:checked ~ label[for="small"],
#medium:checked ~ label[for="medium"],
#large:checked ~ label[for="large"],
#x-large:checked ~ label[for="x-large"],
#xx-large:checked ~ label[for="xx-large"] {
background: #999;
color: #ffffff;
}

Align two element rows by second element

I have rows consisting of two elements that I'd like to align by the second element (they're input boxes, and it's nice to have them all line up vertically). The solution I behaves exactly how I want when the elements fit onto one line, I don't want to be using the width of the first element to set the horizontal position. This starts to act weird in small windows or on mobile (when the width of the label is more than 45% of the screen it starts wrapping again).
What would be the proper way to achieve the same result?
FIDDLE
HTML:
<label>Test</label>
<input type="number" value="0">
<br />
<label>Test longer</label>
<input type="number" value="0">
<br />
<label>Test longest text</label>
<input type="number" value="0">
<br />
CSS:
label {
display: inline-block;
float: left;
width: 45%;
text-align: right;
padding: 2px;
margin: 2px;
}
input {
display: inline-block;
float: left;
width: 200px;
padding: 2px;
margin: 2px;
}
You can use flex properties to display rows and columns and remove float
<div class="flex">
<label>Test</label>
<input type="number" value="0">
</div>
<div class="flex">
<label>Test longer</label>
<input type="number" value="0">
</div>
<div class="flex">
<label>Test longest text</label>
<input type="number" value="0">
</div>
in css
.flex {
display:flex;
}
label {
width: 45%;
text-align: right;
padding: 2px;
margin: 2px;
}
input {
width: 200px;
padding: 2px;
margin: 2px;
}
#media(max-width:200px){
.flex{
display:block;
}
}
and use media queries for screen below 200px(or your preference) to make the text and input appear vertically

Fluid input elements

I got this form...
<form method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<fieldset>
<legend>Who are you?</legend>
<label for="first-name">First name</label><input type="text" name="first_name" required /><br />
<label for="last-name">Surname</label><input type="text" name="last_name" required /><br />
<label for="email">E-mail</label><input type="email" name="email" required /><br />
<input type="button" name="submit1" id="submit1" value="Next" />
<input type="button" name="clear" id="clear" value="Clear" />
</fieldset>
</form>
With this CSS…
form {
margin: 24px 0 0 0;
}
form legend {
font-size: 1.125em;
font-weight: bold;
}
form fieldset {
margin: 0 0 32px 0;
padding: 8px;
border: 1px solid #ccc;
}
form label {
float: left;
width: 125px;
}
form label, form input {
margin: 5px 0;
}
I'm looking for an easy way to make the input fields fluid so that the width of input elements is always relative to the width of the fieldset element. In other words, the width of the label (125px) and input element should always be 100% of the width of the fieldset element. Is there an easy way to do this (without adding divs)?
See: http://jsfiddle.net/thirtydot/pk3GP/
You can do this by adding a harmless little span around each input:
<span><input type="text" name="first_name" required /></span>
And this new CSS:
form input {
width: 100%;
}
form span {
display: block;
overflow: hidden;
padding: 0 5px 0 0;
}
You can also do it with display: table, which is usually a better approach: How can I put an input element on the same line as its label?

Forms with multiple columns, no tables

How to position a complex form with multiple fields in line across the screen?
Why are people so hell-bent on avoiding tables?
Tables are not deprecated and should be used when displaying content which logically belongs in a table.
If your form is logically grouped such that a table would be intuitive, please use a table.
Always be thinking: "What's the cleanest, simplest, most maintainable way to achieve this result."
If you want a fluid form with a variable number columns, then disregard this.
I prefer the slightly-more-semantic way, using a definition list:
<dl class="form">
<dt><label for="input1">One:</label></dt>
<dd><input type="text" name="input1" id="input1"></dd>
<dt><label for="input2">Two:</label></dt>
<dd><input type="text" name="input2" id="input2"></dd>
</dl>
Then your CSS:
dl.form {
width:100%;
float:left;
clear:both;
}
dl.form dt {
width:50%;
float:left;
clear:left;
text-align:right;
}
dl.form dd {
width:50%;
float:left;
clear:right;
text-align:left;
}
This should produce a form centered in the page, with the labels in the left column and the inputs in the right
There are many different ways to do this. It's all a matter of preference. What I typically do is have a wrapper div that contains all of the rows, and then a div block per row that contains the label, input, and validator. You can use the line-height CSS property to help you with vertical alignment. Example:
<div class="formWrapper">
<form>
<div class="formItem">
<label for="firstName">First Name:</label>
<input name="firstName" id="firstName" class="required" type="text" />
<span class="validator" style="display: none;">*</>
</div>
... <!-- Rinse repeat -->
</form>
</div>
<style type="text/css">
.formWrapper { width: 400px }
.formWrapper .formItem { line-height: 35px; height: 35px; }
.formWrapper label { width: 50px; }
.formWrapper input { width: 100px; border: 1px solid #000; }
.formWrapper .validator { padding-left: 10px; color: #FF0000; }
</style>
Hope that helps.
After looking at many many different solutions, I found the examples on this page (particularly the one from 'Fatal'?) some of the most helpful. But the extensive and tags did bother me a bit. So here is a little bit of a modification that some may like. Also, you find some sort of 'wrapper' or 'fieldset' style very necessary to keep the float from affecting other HTML. Refer to examples above.
<style>
.formcol{
float: left;
padding: 2px;
}
.formcol label {
font-weight: bold;
display:block;}
</style>
<div class="formcol">
<label for="org">organization</label>
<input type="text" id="org" size="24" name="org" />
</div>
<div class="formcol">
<label for="fax">fax</label>
<input type="text" id="fax" name="fax" size="2" />
</div>
<div class="formcol">
<label for="3">three</label>
<input type="text" id="3" name="3" />
<label for="4">four</label>
<input type="text" id="4" name="4" />
<label for="5">five</label>
<input type="text" id="5" name="5" />
</div>
<div class="formcol">
<label for="6">six</label>
<input type="text" id="6" name="6" />
</div>
That would be done using CSS by setting the "display" property to "inline" (since form elements are, by default, block level elements).
Do a search for "layouts without tables". Many sites describe formatting with CSS. Here is a simple intro: http://www.htmlgoodies.com/beyond/css/article.php/3642151
I suggest you blueprint CSS framework. Have a quick look at the demo page.
This is what I usually use when I need to design pretty complex forms.
HTML:
<fieldset> <legend>Consent group</legend> <form> <fieldset class="nolegend"> <p><label><span>Title</span> <input type="text" name="title" size="40" value="" /></label></p> <p><label><span>Short name</span> <input type="text" name="sname" size="20" value="" /></label></p> <p><label><br /><input type="checkbox" name="approval"> This consent group requires approval</label></p> </fieldset> <fieldset class="nolegend"> <p><label><span>Data use limitations</span> <textarea name="dul" cols="64" rows="4"></textarea></label></p> </fieldset> <input type="submit" value="Submit" /> </form></fieldset>
CSS:
body, input, textarea, select { font: 1em Arial, Helvetica, sans-serif;}input, textarea, select { font-size: .8em }fieldset,fieldset legend { background-color: #EEE;}fieldset { border: none; margin: 0; padding: 0 0 .5em .01em; top: 1.25em; position: relative; margin-bottom: 2em;}fieldset fieldset { margin: 0 0 1em 0;}fieldset legend { padding: .25em .5em 0 .5em; border-bottom: none; font-weight: bold; margin-top: -1.25em; position: relative; *left: -.5em; color: #666;}fieldset form,fieldset .fieldset { margin: 0; padding: 1em .5em 0 .5em; overflow: hidden;}fieldset.nolegend { position: static; margin-bottom: 1em; background-color: transparent; padding: 0; overflow: hidden;}fieldset.nolegend p,fieldset.nolegend div { float: left; margin: 0 1em 0 0;}fieldset.nolegend p:last-child,fieldset.nolegend div:last-child { margin-right: 0;}fieldset.nolegend label>span { display: block;}fieldset.nolegend label span { _display: block;}
I omitted couple lines of CSS with Safari hacks. You can check out live version of this code.
Pace KyleFarris but I just had to give Ben S a vote for having the guts to mention tables. Just look at the variety of CSS solutions on this page and around the internet for a ridiculously simple problem. CSS may one day become a good solution, but for the time being replicating the simple row and column grid that the table tag provides is extremely complex. I have spent countless fruitless hours with this prejudice against tables for things like a form. Why do we do this to ourselves?
input fields, by default, are inline. Therefore, you can simply use line them up without Another option if you want them lined up correctly is as follows:
<div id="col1" style="float: left;>
<input type="text" name="field1" />
<br />
<input type="text" name="field3" />
</div>
<div id="col2" style="float: left;>
<input type="text" name="field2" />
<br />
<input type="text" name="field4" />
</div>
I prefer to use fieldset to group all elements and p for each form field.
<html>
<head>
<style type="text/css">
fieldset {
width: 500px;
background-color: lightblue;
}
fieldset legend {
font-weight: bold;
}
fieldset p {
clear:both;
padding: 5px;
}
fieldset label {
text-align: left;
width: 100px;
float: left;
font-weight: bold;
}
fieldset .Validator {
color: red !important;
font-weight: bold;
}
</style>
<head>
<body>
<form>
<fieldset>
<legend>Data</legend>
<p>
<label for="firstName">First Name:</label>
<input name="firstName" id="firstName" class="required" type="text" />
<span class="Validator" style="display: none;">*</span>
</p>
<p>
<label for="lastName">Last Name:</label>
<input name="lastName" id="lastName" class="required" type="text" />
<span class="Validator">*</span>
</p>
</fieldset>
</form>
</body>
</html>

Resources