pf » Styling Your forms with CSS and Labels
Styling Your forms with CSS and Labels
I had a comment asking about styling forms using CSS with the label tag in my last blog entry, so I thought I would write another post to show an example.
Here's how you would use the label tag and some CSS to create a stylin' form with very little markup:
<style>
label { position: absolute; text-align:right; width:130px; }
input, textarea { margin-left: 140px; }
label.check, label.radio { position:relative; text-align:left; }
</style>
<form>
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" /><br />
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" /><br />
<input type="checkbox" name="subscribe" id="subscribe" />
<label for="subscribe" class="check">Subscribe</label>
</form>
If you don't want to apply this styling to ALL forms on your website what I usually do is give my form an id, and then use the ID in the style sheet such as:
#myFormID input, #myFormID textarea { ... }
Any questions? Anyone using a different approach?
Related Entries
- Uploading Files Like GMail Attachments - October 6, 2006
- Howto Build a Form That Isn't Annoying (Part 2) - May 3, 2006
- Accessibility and Usability in Forms Outline - February 14, 2006
- Cheat Sheet Roundup - Over 30 Cheatsheets for developers - September 1, 2005
- Usando Label en formularios Tranquilidad
This is not to say that there aren't some cases where a table would work better, but I think for most cases this is a better solution.
<p> <label for="name">Your Name:</label> <input type="text" name="name" id="name" /> </p>
You might also want to use a definition list:
<dl> <dt> <label for="name">Your Name:</label> </dt> <dd> <input type="text" name="name" id="name" /> </dd>
<dt> <label for="surname">Your Surname:</label> </dt> <dd> <input type="text" name="surname" id="surname" /> </dd> </dl>
I usually use a relative unit to define widths of elements.
Check out my form template: http://afrobean.dyndns.org:81/ap/examples/form-layout.html
If you are asking your question regarding my first response to Pete's post, I have actually changed my opinion since I posted my response in favor of using <br/> tags. I've found some situations where this makes form layout much easier.
If you do want to do without <br/> or <p> tags you can do so like this using CSS: /*Use the Label as Block Element */ /*You can control spacing using padding and line-height styles - we've applied a class attribute to the <label> tag */
label.formLabel {position: relative; display:block!important; width: 100%; text-align:left;padding: 5px 0 5px 0; line-height: 15px;}
/*Form Inputs -we've already applied a class to the input */ /* Note: We'll use the margin-top attribute to control the v-position of the input within the label element. */
label input.formInput {position: relative; float:right; margin-top: -15px;}
Hope that clarifies for you.
it is rendering it as display: inline;, the simple solution would be to render it as a block and give it float'(s) and positioning if or as it is needed.
In this case, if someone speak about accessibility of a given website, its more of a brag about his own skills, then its going to help you. Becourse, what he is doing, is simply stating the obvious.
The trick to get older or just inexperianced computer users to reconise a given opjects use, is obviously to indicate its use as moch as possible, make it as obvious as possible without limiting your design to moch, almost anyone trying to learn how to create websites can allready feel this to some degree, however some who call themself "webdesigners" have become lazy and incompetent, and while they feel somthing is wrong with their site, they wont lift a finger to learn how to fix it.
alone the style'ing can be really hard to learn, and dose take some time.
However the result is well worth it, and somtimes it would guantee your page a 1th ranking on a given keyword.
Why we havent even a fully functional and reliable way of doing things yet is really slowing down my own work, and forms is one of the outdated things that could use a'little work by w3c.
Tables allways somehow seam to get the work done, almost regardless of what you want. But damn its annoying, to read the source code of bigger sites who use tables for everything, its not hard for experianced webdesigners, but it dose increase the time required to understand the site structure before we can modify it, and in last end (somtimes the costs of the finished product/website becourse of the increase in work hours)
It is so moch faster to finish a bit of work, for someone who have had his whole site made without tables to begin with, somtimes a major update can be applied in a matter of mins. (if the site structore is correct)
When looking at dedicated webmasters design patterns, we will often find that they are very similer. So if they reconize a given design pattern, it will be moch faster for them to apply a patch/major update.
That is my reason why you would dump tables, even thoug that one or two pages containing a form in a table, isent/wont be the problem.
But even with my humble skills as a grapical designer, where everything i make pretty moch end up randomly in photoshop. Im atleast trying to get things shown the way i want, and i hope that im not limited by outdated views of how the web should be.
views like having the most accessible website, wont hold in the long run, we shouldent be limited by the device that is rendering our site.
And never, ever.. will i limit my webdesign, just becourse some wannabee PC equipment trys to take the role of a real computer. Or cant live up to peoples expectations, when rendering websites and disslike.
As far as im conserned, its the device that has a problem, not the designer. And thats the pure truth, and im shure we will see devices getting their problems solved insted of people blameing it on the designers in the future.
If these new devices want to browse the web, they really need a major update in their rendering engine... Becourse, well it simply suck.
Or they should be limited to their own web, im not going to kneel as long at theres the PC, witch where here first.
I feel like jumping on my mobile phone, im shure it would be a greath feeling. Better not, i need it right now. HAHA
It worked a treat, and the html uses only the <form> tag and <label> tags, nothing more.
6250
- Mastering CFQUERYPARAM
- Google Code Search for ColdFusion
- Speaking at CFUNITED 2008
- Getting ColdFusion SQL Statements from SQL Server Trace
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
RSS
add to del.icio.us
Pete Freitag is a software engineer, and web developer located in











