CSS Forms

Posted on January 26, 2007 in Tutorial | 2 Comments »

Everyone hates forms.

They are hard to style so that they work with your site design. They are hard to make them accessible. Yeah, yeah we have heard it all. But in reality, they are not that hard to do.

Since every other web developer/designer has their own method, I thought I would share my two cents.

When thinking through all of the HTML elements to find something that makes sense for forms, there are many options.

  • Unordered List
  • Ordered List
  • DIVs
  • Paragraphs
  • Tables (Yes some people still use tables for forms)

There are probably others. But I like to use a more uncommon element: a Definition List. It is perfect for the job. You have got the definition term, which is essentially like the label. Then you have got the definition description, which is basically the actual form element.

Let’s start with some un-styled markup:


<form action="#" method="post" name="sendToFriendForm" id="sendToFriendForm">
	<dl class="forms">
		<dt><label for="name">Your Name:</label></dt>
		<dd><input name="name" id="name" type="text" /></dd>

		<dt><label for="senderEmail">Your Email Address: <span class="required">*</span></label></dt>
		<dd><input name="senderEmail" id="senderEmail" type="text" /></dd>

		<dt><label for="recipientEmail">Friend's Email Address: <span class="required">*</span></label></dt>
		<dd><input name="recipientEmail" id="recipientEmail" type="text" /></dd>
		<dd class="help">Use commas to separate multiple email addresses.</dd>

		<dt><label for="sex">Sex</label></dt>
		<dd class="radio"><input name="sex" id="male" type="radio" /> <label for="male">Male</label> <input name="sex" id="female" type="radio" /> <label for="female">Female</label></dd>
		<dt><label for="message">Message:</label></dt>
		<dd><textarea name="message" id="message"></textarea></dd>
	</dl>
	<button type="submit">Submit »</button>
</form>

I have created the page so you can see the output. Even un-styled, this form doesn’t even look bad. But lets add some style:


	form {
		margin: 0;
		padding: 0;
	}

	dl.forms {
		float: left;
		width: 100%;
	}

	dl.forms dt {
		margin: 0;
		padding: 10px 0 0 0;
		clear: left;
		float: left;
		width: 175px;
	}

	dl.forms label { font-weight: bold; }

	dl.forms dd {
		margin: 0;
		padding: 10px 0 0 0;
	}

	dl.forms dd.help {
		clear: left;
		padding-top: 2px;
		font-size: 90%;
		color: #999;
	}

	dl.forms input, dl.forms textarea { width: 300px; }

	dl.forms dd.radio input { width: auto; }

	dl.forms textarea { height: 150px; }

	.required {
		color: #FF0000;
		font-weight: bold;
	}

I don’t think I need to go step by step through that because it is pretty simple styling. I have created the styled page for you to see.

I personally like forms that have the label and the input element on the same line, but that’s just me. It is pretty flexible, try resizing the font two times. Still very readable. You could easily do the widths in ems or percentages; I just chose to use pixels since I don’t have any parent element containing it.

Should be pretty good in all browsers, especially since we didn’t do any crazy CSS. I know it looks good in FF, IE6 and IE7. I believe it works fine in Safari and Opera.

Obviously this isn’t the be all and end all to stying forms with CSS. But it is something to start with. So, go out and make it better. But be sure to share what you have created.

Share This:
  • NewsVine
  • Technorati
  • Reddit
  • Google
  • StumbleUpon
  • Facebook
  • Digg
  • del.icio.us
  • Ma.gnolia
  • TwitThis

2 Responses

  1. Jitendra VyasJuly 29, 2008 at 5:53 am

    but is it semantic?

  2. TrevorJuly 29, 2008 at 7:57 am

    @Jitendra-
    I agree; I don’t think it is semantic as other methods. I have since revisited the way that I mark up forms, and you can read about it in Flexible and Semantic Forms with a Little jQuery.

Speak Your Mind

* Denotes Required Field

  1. Sick of filling out this form? Register or Log in now.

Who Am I?

Trevor Davis I’m Trevor Davis, a 24 year old Front-End Developer. Basically, I make web sites.

By day, I work for Matrix Group International in Alexandria, VA, and by night, I freelance.

Feel free to get in touch with me about anything.

What Have I Done?

  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In
  • Change We Can Believe In

View All My Work »

Bookmarks

  • Google Search Engine Optimization Starter Guide [PDF]

    Google has released a free 22-page Search Engine Optimization Starter Guide containing plenty of well-written, practical and straightforward advice for webmasters. If you've been looking into SEO for a while it probably won't contain anything new for you, but it's useful as a set of guidelines as to what Google considers to be good optimization practice. (psst, Google, with just a little design work it could have looked so much nicer!)

  • The importance of setting expectations

    To make your customer's experience better, be sure to set their expectations.

  • XML Sitemaps Generator

    Insert your URL and let it generate the XML sitemap for you. Very useful for static websites.

  • Train-ee ExpressionEngine Training

    Learn ExpressionEngine with books, screencasts, classroom training and free tutorials from Train-ee.com

  • web.without.words

    Weekly gallery of popular websites reconstructed by removing all words and images, replacing them with blocks.

View All My Bookmarks »