Styling forms using lists

by Dave Reeder 23. January 2010 19:40
form

Styling forms can be a little tricky trying to align labels and input boxes and getting it looking professional, but by using lists we can layout our forms in an easy, semantically correct way.

The key to doing this, is to use our <li> </li> tags as if they are a "block" which we will float out labels and list items in.

Here is the form we will style:



<form action="post" id="contactform">
  <ul>
    <li><label for="yourname">Your Name:</label><input type="text" name="yourname" id="yourname" /></li>
    <li><label for="youremail">Your Email:</label><input type="text" name="youremail" id="youremail" /></li>
    <li><label for="emailsubject">Subject:</label><input type="text" name="emailsubject" id="emailsubject" /></li>
    <li><label for="emailmessage">Message:</label>
    <li><textarea name="emailmessage" cols="12" rows="6" id="emailmessage"></textarea></li>
  </ul>
</form>

We will put our list around all the form elements to get it ready for styling:

<form action="post" id="contactform">
  <ul>
    <li><label for="yourname">Your Name:</label><input type="text" name="yourname" id="yourname" /></li>
    <li><label for="youremail">Your Email:</label><input type="text" name="youremail" id="youremail" /></li>
    <li><label for="emailsubject">Subject:</label><input type="text" name="emailsubject" id="emailsubject" /></li>
    <li><label for="emailmessage">Message:</label>
    <li><textarea name="emailmessage" cols="12" rows="6"  id="emailmessage"></textarea></li>
  </ul>
</form>

And our CSS:

form, 
form ul {width: 500px;}
form ul li {display: block; height: 30px; width: 500px; margin: 0; padding: 0}
form ul li label {width: 240px; float: left}
form ul li input,
form ul li textarea {width: 250px; float: right}

There you have it, your form elements all line up nicely and all it took was a little extra HTML and CSS.

View the demo here

Tags: , , , , , ,

Comments

1/28/2010 6:04:55 AM #

Very nice,..
I was just browsing for relevant blog posts for my project research and I happened to discover yours. Thanks for the excellent information!

regards

Write Hype

2/3/2010 4:46:15 PM #

Thanks for this. I don't have any ideas on how to design list box. Thanks for this simple but helpful tutorial.

paper writing

2/10/2010 11:08:02 PM #

This is great. I am building up my site and was putting forms in and it was coming out all misaligned. I was thinking of using tables but didnt like that idea. This should work out better.

Yan Gilbert

2/13/2010 4:22:14 PM #

I found your blog on Google and read a few of your other posts. I just added you to my rss. Keep up the good work. Look forward to reading more from you in the future.

get pregnant fast

2/19/2010 2:08:40 AM #

Amazing site, where did you come up with the knowledge in this write-up? I'm glad I found it though, ill be checking back soon to see what other articles you have.

Kiesha Healey

Comments are closed