<p>Everyone hates forms.</p>

<p>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.</p>

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

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

<ul><li>Unordered List</li>
<li>Ordered List</li>
<li>DIVs</li>
<li>Paragraphs</li>
<li>Tables (Yes some people still use tables for forms)</li>
</ul><p>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.</p>

<p>Let's start with some un-styled markup:</p>

<div class="codeblock"><code>
&lt;form action="#" method="post" name="sendToFriendForm" id="sendToFriendForm"&gt;<br /> &lt;dl class="forms"&gt;<br />  &lt;dt&gt;&lt;label for="name"&gt;Your Name:&lt;/label&gt;&lt;/dt&gt;<br />  &lt;dd&gt;&lt;input name="name" id="name" type="text" /&gt;&lt;/dd&gt;<br /><br />  &lt;dt&gt;&lt;label for="senderEmail"&gt;Your Email Address: &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/dt&gt;<br />  &lt;dd&gt;&lt;input name="senderEmail" id="senderEmail" type="text" /&gt;&lt;/dd&gt;<br /><br />  &lt;dt&gt;&lt;label for="recipientEmail"&gt;Friend's Email Address: &lt;span class="required"&gt;*&lt;/span&gt;&lt;/label&gt;&lt;/dt&gt;<br />  &lt;dd&gt;&lt;input name="recipientEmail" id="recipientEmail" type="text" /&gt;&lt;/dd&gt;<br />  &lt;dd class="help"&gt;Use commas to separate multiple email addresses.&lt;/dd&gt;<br /><br />  &lt;dt&gt;&lt;label for="sex"&gt;Sex&lt;/label&gt;&lt;/dt&gt;<br />  &lt;dd class="radio"&gt;&lt;input name="sex" id="male" type="radio" /&gt; &lt;label for="male"&gt;Male&lt;/label&gt; &lt;input name="sex" id="female" type="radio" /&gt; &lt;label for="female"&gt;Female&lt;/label&gt;&lt;/dd&gt;<br />  &lt;dt&gt;&lt;label for="message"&gt;Message:&lt;/label&gt;&lt;/dt&gt;<br />  &lt;dd&gt;&lt;textarea name="message" id="message"&gt;&lt;/textarea&gt;&lt;/dd&gt;<br /> &lt;/dl&gt;<br /> &lt;button type="submit"&gt;Submit &amp;raquo;&lt;/button&gt;<br />&lt;/form&gt; 

</code></div>

<p>I have created the page so you can see <a href="//assets.trevor-davis.com/play/cssForms/">the output</a>. Even un-styled, this form doesn't even look bad. But lets add some style:</p>

<div class="codeblock"><code>
form {<br />  margin: 0;<br />  padding: 0;<br /> }<br /><br /> dl.forms {<br />  float: left;<br />  width: 100%;<br /> }<br /><br /> dl.forms dt {<br />  margin: 0;<br />  padding: 10px 0 0 0;<br />  clear: left;<br />  float: left;<br />  width: 175px;<br /> }<br /><br /> dl.forms label { font-weight: bold; }<br /><br /> dl.forms dd {<br />  margin: 0;<br />  padding: 10px 0 0 0;<br /> }<br /><br /> dl.forms dd.help {<br />  clear: left;<br />  padding-top: 2px;<br />  font-size: 90%;<br />  color: #999;<br /> }<br /><br /> dl.forms input, dl.forms textarea { width: 300px; }<br /><br /> dl.forms dd.radio input { width: auto; }<br /><br /> dl.forms textarea { height: 150px; }<br /><br /> .required {<br />  color: #FF0000;<br />  font-weight: bold;<br /> } 

</code></div>

<p>I don't think I need to go step by step through that because it is pretty simple styling. I have <a href="//assets.trevor-davis.com/play/cssforms/final/">created the styled page</a> for you to see.</p>

<p>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.</p>

<p>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.</p>

<p>Obviously this isn't the be all and end all to styling 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.</p>