Weird Looking: CSS Form Layout

CSS Form Layout

May 17, 2005 5:23pm (5 years, 3 months and 3 weeks ago)
tags: none
Hard-coded pixel widths or absolute positions for the labels will break really bad with different-sized fonts.

Now I don’t want my web page to be primarily about my web page… But hopefully I can save someone the time I spent figuring this one out.

The basic idea I had was to left-float the labels and right-align the input elements within row-like divs.  This results in the labels aligned to the left and the input elements to the right.  Good.

The trick after that is to get the form to collapse down to the width of its contents, the way a table does, instead of expanding to 100% the way a div does.  “display: table” works in Opera and Firefox.  In IE, left-floating it gets the effect, but this messes up Opera.  Putting both makes all three browsers work.

This gives a good effect in every browser I’ve tested, doesn’t break when fonts resize, and keeps the HTML entirely semantic.

.myForm {float: left;display:table;margin: 0px; text-align: right;}
.myForm div
{clear: both;}
.myForm label
{float: left; margin-right: 10px;}
.myForm input
{width: 300px;}
.myForm textarea
{height: 100px; width: 300px;}

<form method="post" action="" class="myForm">
<div><label for="name">Name</label><input type="text" name="name" id="name "value="" /></div>
<div><label for="email">Email</label><input type="text" name="email" id="email" value="" /></div>
<div><label for="link">Link</label><input type="text" name="link" id="link" value="" /></div>
<div><label for="comment">Comment</label><textarea name="comment" id="comment"></textarea></div>
<div><input type="submit" value="Submit" /></div>
</form>
<div style="clear: both;"></div>

Comments

May 19, 2005 3:26am
I’m fond of using <dl> and putting the labels in <dt> and controls in <dd> and using styles similar to those.  I think I usually end up going with ex-unit-based widths on the <dt> tags, though, and make sure none of my labels are never more than a given width.

The clear:both thing can be fixed in Firefox and Opera with an “.myform::after { content: ' '; display: block; clear: both; }”

I think that IE does this automatically (and incorrectly), but in this case that’s a good thing.
May 20, 2005 6:11am
Clearing the :after pseudoclass on the form didn’t help.  I had to use it on the containing <div>, which messed up the text below the form, since the left-float was still in effect.

It was broken in IE as well.  PIE says that the IE auto-clearing bug only kicks in when the block has dimensions defined.  So I set the height on the containing <div> to 1%.

It works, but it doesn’t seem all that much cleaner than the clear:both div.
May 23, 2005 12:26am
I wonder if <hr> with styles to make it clear but not display would be better than a div (semmantics are tricky, expecially with hacks)?  I guess it really depends on what you want your site to look like when you disable styles or view it in Lynx (another reason why I like definition lists for forms).
May 23, 2005 6:13am
Well, now I’ve just got the text below the form in a <p>aragraph with the clear style.  So nothing’s wrong about it (but it’s still a hack).

I like viewing my web page in Netscape 4.7 for a good laugh.
May 26, 2005 1:02am
If you use media=”screen,projection” for your stylesheet links, Netscape 4 will ignore them (even though it supports screen).  That’s a clean way to gracefully degrade Netscape 4, and at the same time support full-screen mode in Opera (which uses projection).  You can also use the a CSS @import, but I find that harder to manage.

Of course, you’ll probably also want another stylesheet for media=”print”, but that’s not a bad idea anyway (get rid of all the color and fluff, such as forms and navigation links).
Nov 16, 2005 8:14pm
Oh! Let’s hunt the guy who spammed with that hunter link!

In fact, I think we should have a national Hunt the Spammer day!
Nov 18, 2005 2:49pm
I saw the “cottontail / coyote / crow” thing and realized it was listing things to hunt.  Then I looked again and saw “kids / men / women” and was like whoa!  Wasn’t what I thought it was though :(
Nov 25, 2005 10:40am
Sorry, Masha, but that good site was just stupid.  Plus, I fail to see what it had to do with CSS Form Layout at all…  Plus, you’re using HTML and the form clearly says BBCode.  I’m starting to think that you’re not even worth having as a friend.  Sorry.
Nov 27, 2005 6:28pm
I made with the rel=”nofollow” after that last one.  I could pretty easily delete the spam right now, but that’s not really a scalable solution.  In the long run I’d like to figure out something better.  Guess I’ll go see what everyone else is doing.
Dec 21, 2005 1:27am
Obviously I am going to have to block the IP of anyone whose domain name contains a dash or ends in .info or .biz.
Dec 21, 2005 9:49pm
Hi
Where is markup too!
Dec 21, 2005 10:38pm
Hi
Please visit my site at
http://www.muujware.com/
http://muujware.com/
http://tinyurl.com/77537

Momma always said, “If you’re going to Spam, at least Spam in the correct markup language.”
Jan 11, 2007 12:04pm
Hi! Very nice site! Thanks you very much! ohO9ibrOU3

Leave a comment


Accepts BBCode with a few enhancements.