473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

sloppy form alignment, what's the best css fix?

jhardman
3,406 Expert 2GB
I recently answered a post in the asp forum where a newbie had trouble accessing his form data. I chastised him for his sloppy coding and told him to validate his code (his form and input tags were so sloppy there was no way any data was sent to the asp). But I have a question about how he had formatted his form.

He essentially had a two-column table, the first column listed the name of the input field and the second column held the actual form inputs. The big problem was that he didn't actually use a table, he used line breaks and long strings of spaces to align the form inputs and labels how he wanted:
[HTML]name:         &n bsp;   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="name"><br><br>
title: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <input type="text" name="title"><br><br>etc.[/HTML] Now there is no reason I need to go into the problems with formatting in this way, I think we all groan when we see this. The question I have is this: If I was making this same form I would use a real table, but w3c seems to say that this is about as bad. W3C says tables should be reserved for tabular data. Is a form close enough to tabular data that it can be used for this without angering the purists? And if not, is there a simple method of sequential divs that would be as easy to manipulate and set up as a bare-bones table? Would that be simple enough for a coding minimalist like me to do quickly and without hassle?

Jared
Mar 14 '07 #1
7 2609
The CSS would look a bit like this:
Expand|Select|Wrap|Line Numbers
  1.     <style>
  2.       #myForm dl {
  3.         line-height : 2em;
  4.         padding: 0;
  5.       }
  6.       #myForm dl dt {
  7.         text-align: right;
  8.         float: left;
  9.         clear : both;
  10.         width : 33%;
  11.       }
  12.  
  13.       #myForm dl dd
  14.       {
  15.          float : left;
  16.          width : 66%;
  17.          margin: 0 0 0 0.25em;
  18.       }
  19.  
  20.       .alert {
  21.         color : red;
  22.       }
  23.  
  24.       #registration {
  25.         border : solid 1px #ccc;
  26.       }
  27.     </style>
  28.  
and the form would look a bit like this:

Expand|Select|Wrap|Line Numbers
  1.     <div id="registration">
  2.       <form name="form" id="myForm" method="post" action="">
  3.         <dl>
  4.           <dt>
  5.             <label for="title"><span class="alert">*</span> Title:</label>
  6.           </dt>
  7.           <dd>
  8.             <input type="Text" name="title" id="title" size="10" maxlength="30" value="">
  9.           </dd>
  10.         </dl>
  11.         <dl>
  12.           <dt>
  13.             <label for="initials"><span class="alert">*</span> Initials:</label>
  14.           </dt>
  15.           <dd>
  16.             <input type="Text" name="initials" id="initials" size="10" maxlength="10" value="">
  17.           </dd>
  18.         </dl>
  19.         <dl>
  20.           <dt>
  21.             <label for="firstName"><span class="alert">*</span> First name:</label>
  22.           </dt>
  23.           <dd>
  24.             <input type="Text" name="firstName" id="firstName" size="24" maxlength="40" value="">
  25.           </dd>
  26.         </dl>
  27.  
  28.         <dl>
  29.           <dt>
  30.             <label for="surname"><span class="alert">*</span> Surname:</label>
  31.           </dt>
  32.           <dd>
  33.             <input type="Text" name="surname" id="surname" size="24" maxlength="40" value="">
  34.           </dd>
  35.         </dl>
  36.  
  37.         <dl>
  38.           <dt>&nbsp;</dt>
  39.           <dd>
  40.             <input type="Submit" value="Submit">
  41.             <input type="Reset" value="Reset">
  42.           </dd>
  43.         </dl>
  44.       </form>
  45.       <br style="clear : both">
  46.     </div>
Mar 14 '07 #2
drhowarddrfine
7,435 Expert 4TB
I don't see any reason to use a table for that type of input. It's easy enough using forms and <fieldset> with css to make a nice layout. The guy obviously doesn't know css or he could have just used margins and padding to create space between elements.
Mar 14 '07 #3
jhardman
3,406 Expert 2GB
The CSS would look a bit like this:
Expand|Select|Wrap|Line Numbers
  1.     <style>
  2.       #myForm dl {
  3.         line-height : 2em;
  4.         padding: 0;
  5.       }
  6.       #myForm dl dt {
  7.         text-align: right;
  8.         float: left;
  9.         clear : both;
  10.         width : 33%;
  11.       }
  12.  
  13.       #myForm dl dd
  14.       {
  15.          float : left;
  16.          width : 66%;
  17.          margin: 0 0 0 0.25em;
  18.       }
  19.  
  20.       .alert {
  21.         color : red;
  22.       }
  23.  
  24.       #registration {
  25.         border : solid 1px #ccc;
  26.       }
  27.     </style>
  28.  
Wow, I'd never thought of using a dictionary list like that. I don't see any code that would put the label to the left of the input, I thought this would put the input below the label until I tried it. What part of the code does that?

Jared
Mar 14 '07 #4
jhardman
3,406 Expert 2GB
I don't see any reason to use a table for that type of input. It's easy enough using forms and <fieldset> with css to make a nice layout. The guy obviously doesn't know css or he could have just used margins and padding to create space between elements.
I've never fieldsets, (I say with chagrine). It looks like I need to brush up some. Thanks for the suggestion.

Jared
Mar 14 '07 #5
drhowarddrfine
7,435 Expert 4TB
Fieldsets are nothing big deal. They just help keep different parts of a form more organized.
Mar 14 '07 #6
AricC
1,892 Expert 1GB
Jared,
The <dl> tag is for a definition list. I agree with Doc above Fieldsets are seldom used but can do a great job organizing forms etc... You can even style them so they don't look generic.

HTH,
Aric
Mar 14 '07 #7
jhardman
3,406 Expert 2GB
I like it. This is what I tried:
[HTML]<style>
<!--
label {
color: black;
position: relative;
left: 40px;
}

input {
position: absolute;
top: inherit;
left: 220px;
}

fieldset {
border: 0px;
padding: 5px;
}

-->
</style>[/HTML]

Is this llike what you would have done?

Jared
Mar 14 '07 #8

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

Similar topics

5
by: Clint Olsen | last post by:
I did a Google about this, and I've seen this covered quite a few times - some leading to useful tricks with offsetof() to discern alignment and others using a big-union of intrinsic types to...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
7
by: Doug Bell | last post by:
Hi Does anyone know (or point me where I can find) how to set the alignment of a DataGrid Column Header different to the alignment of the column. I am trying to show some Right aligned columns...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
5
by: pt | last post by:
Hi, i am wonderng what is faster according to accessing speed to read these data structure from the disk in c/c++ including alignment handling if we access it on little endian system 32 bits...
1
by: bobmct | last post by:
As an old-time raw HTML code I'm struggling to adapt to using css. I'm getting there... but slowly. I do have a question that I cannot find an answer for and perhaps someone here can advise? ...
1
by: Alan T | last post by:
I just put 2 text boxes on a web form but they are side by side, I have no way to reposition them.
11
by: Brian Gladman | last post by:
A lot of low level cryptographic code and some hardware cryptographic accelerators either fail completely or perform very poorly if their input, output and/or key storage areas in memory are not...
8
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc?...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.