Create a fancy web form with field hints using only CSS3
CSS3 tricks have been pretty popular in the last while on various design blogs and with good reason. Like many of you, I've caught the CSS3 bug and this afternoon I took a stab at this nifty little method of adding hints to your form fields (as well as some fancy style enhancements) using CSS3. The cool part is you don't require Javascript for the form field hints. Here's a demo and run down of the code used.
Known Issues
The effect does not work when you tab through the form. It would also be better to show the hints on focus instead of on hover, which would resolve the usability issue but I've had some problems in getting the other method to work across browsers. I might stick at it and see.
In the end, this is just an experiment and I do not recommend using this method in practice.
The XHTML markup:
<form id="myForm" method="post" action="#"> <fieldset> <!-- EACH LABEL AND FORM ELEMENT IS WRAPPED IN ITS OWN DIV TO APPLY THE HOVER STYLE THAT DISPLAYS THE HIDDEN HINT --> <div class="myName"> <label>My name:</label> <!-- HINT IS HIDDEN BY DEFAULT --> <p class="hint">Please include both your first & last name</p> <input type="text" name="Name" class="textfield" /> </div> <div class="myEmail"> <label>My email address:</label> <p class="hint">Your email address will never be sold!</p> <input type="text" name="EmailAddress" class="textfield" /> </div> <!-- IT IS PROBABLY UNNECESSARY TO USE A HINT FOR A SELECT LIST SO IT'S OMITTED HERE --> <label>I'm looking for:</label> <select class="dropdown" name="Topic"> <option>Select...</option> <option>A CSS3 form like this</option> <option>A CSS3 form like this</option> <option>A CSS3 form like this</option> </select> <div class="myComments"> <label>Comments:</label> <p class="hint">Please be as specific as possible so we can serve you quickly</p> <textarea cols="60" rows="10" class="textarea"></textarea> </div> <!-- A HINT ON A SUBMIT BUTTON IS PROBABLY OVERKILL BUT THIS IS JUST TO SHOW THAT IT CAN BE DONE --> <div class="sendTip"> <p class="hint">Make sure your info is correct before sending!</p> <input type="submit" name="Submit" value="Send message" class="btn" /> </div> </fieldset> </form>
The CSS:
fieldset {
width: 450px;
margin: 0;
padding: 30px;
background: #f0f0f0;
border: 2px solid #d0d0d0;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
-moz-box-shadow: 6px 6px 6px #ddd;
-webkit-box-shadow: 6px 6px 6px #ddd;
box-shadow: 6px 6px 6px #ddd;
}
label {
display: block;
font: bold 1.2em arial,verdana,tahoma,sans-serif;
text-shadow: 2px 2px 2px #ccc;
color: #000;
margin: 0 20px 10px 0;
padding: 0;
clear: left;
}
/* APPLIES THE STYLE TO EACH FORM ELEMENT */
.textfield, .dropdown, .textarea {
width: 220px;
font: normal 1.1em arial,verdana,tahoma,sans-serif;
color: #666;
margin: 0 0 30px 0;
padding: 9px 14px;
background: #f2f2f2;
border: 2px solid #d0d0d0;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
-moz-box-shadow: 4px 4px 4px #ddd;
-webkit-box-shadow: 4px 4px 4px #ddd;
box-shadow: 4px 4px 4px #ddd;
}
/* OPTIONAL SET WIDTH FOR THE SELECT LIST */
.dropdown {
width: 250px;
}
/* OPTIONAL SET WIDTH FOR THE TEXTAREA */
.textarea {
width: 410px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
}
/* CHANGE THE STYLE WHEN THE FORM ELEMENT IS CLICKED */
input:focus, select:focus, .textarea:focus {
background: #fff;
border: 2px solid #ddd;
-moz-box-shadow: 2px 1px 1px #ddd;
-webkit-box-shadow: 2px 1px 1px #ddd;
box-shadow: 2px 1px 1px #ddd;
outline: none;
}
/* THE NAMES FOR EACH OF THE FORM ELEMENT DIVS WHICH AUTOMATICALLY HIDES THE TIPS BY DEFAULT */
div.myName p.hint, div.myEmail p.hint, div.myComments p.hint, div.sendTip p.hint {
display: none;
}
/* WHEN EACH DIV IS HOVERED, THE TIP IS DISPLAYED */
div.myName:hover > p.hint, div.myEmail:hover > p.hint, div.myComments:hover > p.hint, div.sendTip:hover > p.hint {
position: absolute;
display: block;
font: bold 0.8em arial,verdana,tahoma,sans-serif;
text-shadow: none;
color: #000;
margin: 0 0 0 265px;
padding: 10px 15px;
background: #ffff80;
border: 2px solid #f7de35;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
-moz-box-shadow: 4px 4px 4px #ddd;
-webkit-box-shadow: 4px 4px 4px #ddd;
box-shadow: 4px 4px 4px #ddd;
}
/* CHANGES THE LEFT MARGIN ON THE TEXTAREA TIP, FOR BALANCE */
div.myComments:hover > p.hint {
margin: 2px 0 0 350px;
}
/* CHANGES THE LEFT MARGIN ON THE SUBMIT BUTTON TIP, FOR BALANCE */
div.sendTip:hover > p.hint {
margin: 2px 0 0 150px;
}
.btn {
display: block;
font: bold 1.1em arial,verdana,tahoma,sans-serif;
color: #000;
text-decoration: none;
margin: 0;
padding: 9px 11px 8px 11px;
background: #f2f2f2 url(../img/submit-backgr.png) repeat-x;
border: 2px solid #d0d0d0;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
-moz-box-shadow: 4px 4px 4px #ddd;
-webkit-box-shadow: 4px 4px 4px #ddd;
box-shadow: 4px 4px 4px #ddd;
}
.btn:hover, .btn:focus {
color: #fff;
background: #f2f2f2 url(../img/submit-hover-backgr.png) repeat-x;
border: 2px solid #888;
-moz-box-shadow: 2px 2px 2px #ddd;
-webkit-box-shadow: 2px 2px 2px #ddd;
box-shadow: 2px 2px 2px #ddd;
}
Stay well.













This is so cool!!
Posted: March 1, 2010
hi
nice article n great trick! but as being new to css3 can you please explain div.myName:hover > p.hint what it means?
Secondly, [you may know this .. but still ] instead of writing border radius property for each element I write a separate class for .roundCorners and then I can apply it to any desired object. This way you can reduce your code. Just a suggestion.
Posted: March 5, 2010
Sure. Basically it means that when you hover over the div labeled with the class "myName", the paragraph labeled with the class "hint" is given the CSS properties in that class.
I would have used your suggestion but then I'd have been in a bit of a pickle with having to apply a class to an element which already has a class assigned to it.
Posted: March 5, 2010
I don't see where CSS3 is needed, here.
The :hover is available in CSS2 already and the rest can be achieved with display: none and display:block ...
Posted: March 6, 2010
That's true but because I applied a little dash of CSS3 to the mix I decided to go with the just saying CSS3 instead of CSS2 and 3. Like yourself, I think those who look at the code will know what I meant by it.
Posted: March 6, 2010
Great and very elegant. Thanks for sharing.
Posted: March 6, 2010
Great trick indeed. Good thinking on the using :hover part.
Have to agree with v-render though; having a seperate class for your border radius, or multiple classes each with another radius. I don't see where having multiple classes for an element would be a problem, nowehre in your code would that be in issue AFAICS. if it was done to prevent confusion you should've left them out entirely and focus on only the nessecary rules.
But critique is easy, I knows, thanks for the writeup ;)
Posted: March 6, 2010
I hear ya. It wasn't working for me the other way, though, so I left it as is. It's just an experiment anyway.
Posted: March 6, 2010
This is a great idea, but why not use :focus and :active instead of :hover to show the field hints?
When I am filling out a form, I use tab instead of clicking on the seperate fields, so the hints didn't pop up for me.
Also, try adding the line *:focus{ outline: none; } to your css, since browsers like chrome will try to outline it and you end up with a boxy glow around the field that you are trying to make round.
Posted: March 6, 2010
I already knew that, I just neglected to add it in. I added it to the example above but I'll do the same with the demo today.
Posted: March 6, 2010
nice idea, but it lacks some usability. i intuitively wanted to user the TAB-key to navigate through the input fields... if you do this, no hint is shown at all.
Posted: March 6, 2010
I agree, but this is just an experiment. I wouldn't use this in actual practice in its current form.
Posted: March 6, 2010
Well done, but I see you have the same troubles with :focus border as me.
In -webkit driven browsers, the border which appears when focusing an input, always appears non-rounded - doesnt matter if you try border-radius - or -webkit-border-radius.
Posted: March 6, 2010
Cool, nice info about css3. But might you want to consider those who tab through forms with the keyboard? Usually code hints appear upon focus, not just hover.
Posted: March 6, 2010
It's a good idea, Nick, but I'd have to use Javascript for that, I wager. I wouldn't recommend using this method in its current form for reasons such as usability.
Posted: March 6, 2010
Great Idea! Why not use :focus instead of or in addition to :hover though? When I use a form, I use the tab key, and don't use the mouse, so the field hints didn't show for me.
Also, be aware that browsers like chrome and safari like to put a fancy glow around active fields, but that makes them look boxy when you are trying to make them look round. You can solve this by adding *:focus{ outline: none; } to your css.
Posted: March 6, 2010
@David: I tried but it didn't work so I left it as is. I'm aware of the outline:none thing...I just didn't add it to the demo. Doing that today.
Posted: March 6, 2010
wish it was possible to do this when the input is focused, but it'd be a good enhancement with JavaScript.
Anyways great concept and script, I'll try implementing this into a contact form or something.
Posted: March 7, 2010
Thanks, Sunny. Yeah, like I've been saying to the others here, there are minor issues that prevent this from being a fully sound solution.
Posted: March 6, 2010
good little article.
Javascript on focus to display the tips? that way it would also work when tabbing through the form, but again this is no longer a pure CSS implementation anymore with that solution.
Posted: March 8, 2010
Yeah, exactly. All in good fun.
Posted: March 8, 2010
Thanks! Simple and elegant
Posted: April 4, 2010
Thats awesome! Thanks for sharing...
Posted: June 2, 2010
Really good. Thanks!
Posted: August 13, 2010