473,804 Members | 3,204 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Aligning form labels and fields: easy with table, how with CSS?

8 New Member
When putting a form on a webpage, I prefer the labels and edit boxes for all fields to be aligned, e.g. like this:



This is easily done by putting it all in a table. What is the best way to do this properly with CSS?

Note: I don't want to define fixed widths for certain columns or whatever. The width for the labels should just be automatically as much as necessary to fit the widest label.

I first thought to create two columns, one for the labels, one for the input fields. But then there is nothing that vertically aligns each label and its corresponding input field. So if one label has a <br> in it, or an input field is actually a selection with 3 radiobuttons above eachother, then I have to manually adjust the other column to keep everything in line (which is obviously a dumb method, and not even possible for dynamically generated content).
Feb 4 '09 #1
3 11965
drhowarddrfine
7,435 Recognized Expert Expert
Here and here for two examples. I do this all the time. It's easy.
Feb 4 '09 #2
Stomme poes
14 New Member
Unfortunately neither of those pages show real-life forms with sometimes very short and other times very long labels. They made it very easy on themselves by having all short labels. As if almost to prove it can't be done with real forms.

*edit the howden one has two longer labels, though one has a text-area with a row number higher than the line-height of the label

I struggle with this almost daily, Bram. You get a few options, but may need to use one or the other per form depending on what you've got. I've scoured various CSS sites looking for "real life" forms and haven't found many that were all CSS and accessible. But this doesn't mean it can't be done.

One thing you can do is set a width for the labels. Yeah, I know you said you don't want to but in the example you posted, if they all had a width of "howeverman y" em you needed to accommodate the longest label, the inputs (naturally inline) would be kept at bay so their left sides all line up nicely as your image shows. So long as the form is wide enough to do this, of course.

I usually need something to wrap the labels and inputs to make them a unit, so that when longer labels are floated, the inputs of the following labels can't ride up too high.
I usually use a div for this, though I've seen p's used. I don't think they're paragraphs so I don't use them.
If you use divs and float the labels, you'll need something to make them enclose their floats. I'm currently using display: table just for them, but it makes firefox do the first render goofy (a refresh always fixes it). If my form is too long, Opera also starts having rendering issues with display: table so you may need something like overflow: hidden or the clearfix to do this instead. I had CSS tooltips so overflow wasn't an option for me... when there are no tooltips overflowing, then I use it as it's very easy. (hint: while building your forms, give the divs/whatever, the labels or whatever's floating and the fieldsets al ugly background colours so you can see who is where).

However if there are no help-text spans in your forms you can use the label itself as the wrapper, which can work very nicely. I saw this done by some crusty gurus at another forum:
Expand|Select|Wrap|Line Numbers
  1. <label for="name"><span>Name:</span><input type="text" name="blah" id="name"> </label>
  2.  
See the form.
If the label is set to display: block, and clear: left just to be safe, the spans inside can be floated left (again, if they all have the same width then the inputs will be pushed the same distance away) and the inputs stay in the vicinity of their labels.

For a multi-input, there are a few options:
I often use a fieldset with the class of "access" for things like dates, because I'm forced to have three separate inputs for each chunk (day, month, year) and so I want a label for each (if you can't, using the title attribute on the inputs will work great in screen readers) but don't want visual surfers to see the labels.

fieldset.access has the border gone, the legend off-set by -9999em (watch out, firefox needs some extra code for this), has a p as a first child with the text for the long question (for the visual people) and then a traditional label-input setup, with the labels also offscreen by -999em.

Expand|Select|Wrap|Line Numbers
  1. <fieldset class="access">
  2. <legend>Insurer birth date</legend>
  3.   <p>Birthdate of insured:</p>
  4.     <label for="day">Day: </label><input type="text" name="birthdate1" id="day" maxlength="2" size="2">
  5.     <label for="month">Month: </label><input type="text" name="birthdate2" id="month" maxlength="2" size="2">
  6.     <label for="year">Year: </label><input type="text" name="birthdate2" id="year" maxlength="4" size="4">
  7. </fieldset>
  8.  
Visual people will see the p as a question (and gets the same styling as regular labels, so it looks like a label), and then three inputs. Screen readers will of course ignore the p but will state the legend before each question, and the label before each input. So far has worked for me.

For radios and checkboxes, I often have a div or something with a class of "checkradio " so the labels in there don't inherit the styles of a regular labels (remain inline). This could also be in a fieldset instead, whatever works for you.

Expand|Select|Wrap|Line Numbers
  1. <div class="checkradio">
  2.   <label>How many oreos do you eat per day?</label> <--don't use a non-form control here or screen readers will miss it
  3.   <label for="none"><input type="radio" name="oreo" id="none"> None</label>
  4.   <lable for="some"><input type="radio" name="oreo" id="some"> A few</label>
  5.   <label for="lots"><input type="radio" name="oreo" id="lots"> A lot</label>
  6. </div>
  7.  
I've also seen where just checks and radios get their own box with a left margin equal to the width of the labels, or each label-input pair gets the margin... this is only safe when they don't have Layout in IE, otherwise IE will have the margin start in the wrong place while modern browsers do it correctly.

The css needs to have different styles for these different types. It starts out being more coding, but if the same css can work for many forms, it ultimately is faster as any new form can be created and will automatically get the styling you want, without anything more than form controls, possibly a few containers, and 2 or 3 classes (no more are really needed).

Tabled forms are always more HTML than necessary, and it uses the HTML to set the styling, mixing content and presentation. Also, if done incorrectly (as I usually see it) people with multi-column tabled forms often make the questions go in a strange source order. Using CSS can cure all this, if you're willing to spend a lot of time getting familiar with how browsers treat these form elements.

There are also some great styling forms tutorials out there, each with specific solutions to problems like the FireFox legend bug. Google "style forms css". The two Doc mentioned above will also appear, so look carefull at the code they use. I was very much not impressed with some of the forms listed on the silverback site, many using non-form controls like p's and otherwise inaccessible practises. Always look closely at the code, and avoid JS or Flash for the forms whenever possible.
Feb 5 '09 #3
Bram2
8 New Member
Thanks a lot for your detailed reply! I will study it in detail and experiment with it :)
Cheers!
Feb 9 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

11
5163
by: Peter Foti | last post by:
Hi all, I have a form that contains a 2 column table. In the left column is the description for a particular input (ie - "First Name", "Last Name", "Phone Number", etc.). In the right column is the input element. The left column is right aligned and the right column is left aligned. I would like to replace this table with code that does not use a table for layout, and was hoping someone might be able to help me with the HTML and...
3
3220
by: Geoff Hague | last post by:
I've got a small little problem with my 'website-in-progress': http://www.captainsoftheworld.com/modernrepublic/strict/index.php http://www.captainsoftheworld.com/modernrepublic/strict/ModernRepublic.css Is it possible, without messing around with per-pixel positioning or sizing, to make the labels ("Name" & "E-mail") in the Mailing List form line up with their associated text-input areas? In my system's setup (using IE6) the labels...
2
1477
by: Obatistor | last post by:
Hello all! How are you? I would like to create a table with 300 fields and it is not possible in a simple way because access doesn't let you create a database table bigger than 255 fields. Could I do that with a simple QUERY? Or, if not, how could I do that? Any help will be appreciated. Thanks for all,
5
3756
by: Sami | last post by:
Please bear with me, and if you answer this question, please do it step by step. I am new at Access, not at all sophisticated. I am using Office XP. This will need to be read in Access for Office 2000. I am creating a database to track student athletes. I have created the following tables. The table title is to the far left, with fields under each. The common field will be the StudentID field, which is their student number assigned...
5
1585
by: DaveA | last post by:
I've got myself into a bind here. Any help would be a life saver. I created a form with 644 text boxes that serve as "days" for a schedule. Each text box has a code which is populated with an X or /. My problem is I did them all unbound. Is there a way to use code to either create a new table or append my current table that creates the unbound text fields starting with text0 and increasing by 1 until it reaches 644? Thanks in advance....
10
1570
by: Regnab | last post by:
I'm making a database to record training that employees have recieved. For each training excercise, they recieve a mark between 1 and 3. Initially, when there weren't so many training elements, I had put the employee first name, last name, section, First Aid, Forklift operation... and so on in one table. However, I now have a list of 227 Training elements that need to be completed (each recieving a mark between 1 and 3). When I tried to...
6
3861
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called "Generations"), and it allows the user to add, edit and delete the various records of the table. "Generations" table has the following fields: "IDPerson", NamePerson", "AgePerson" and "IDParent". A record contains the information about a person (his name, his...
1
1595
by: jl2886 | last post by:
I have two tables in Access, one that contains Active or Pending Policies and another table that contains Rejected policies. I have a form corresponding to the table with a certain variable called "Status" that evaluates whether a policy is Pending, Active or Rejected(a regular conditional statement in VB). The form is linked to the Active or Pending Policies Table currently, however, I would like the form information to send a policy to the...
2
1367
by: servantofone | last post by:
I'm running Access 2003 on Windows XP. This is more of a developmental/implementation type of question. I have a form which requires answers to 35 different questions. All questions are answered by checkbox and two text fields. This makes for a total of 105 fields (35*3=105) just for question answering. Since all questions require the same Yes/No answer and two text fields, is there a way I can cut down the size of my table? Should I...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10578
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10321
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9152
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7620
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6853
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3820
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2991
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.