Enhance your input fields with simple CSS tricks
We're all trying to build an effective and good looking web forms. But there are always a new challenges. If you read my previous articles on how to build a better web forms, you could have noticed there are so many details. Label positioning, context highlighting or justifying elements. But, with just a few simple CSS tricks you can make a usual, boring web forms more effective and exciting.
The example you are going to see is something that you use every day: blog comment form. Ok, so what can you do to enhance a web form? You can…
…add some borders
At least what you can do is to add borders and padding to your input fields. Two examples above shows how you can sat the border color to match your color schema. It is also a good practice to add some padding to input fields. That will make forms more clear. In this example I set the 4px padding to inputs and textarea.
#inputArea
{
font-family: Arial, Sans-Serif;
font-size: 13px;
background-color: #d6e5f4;
padding: 10px;
}
#inputArea input[type="text"], #inputArea textarea
{
font-family: Arial, Sans-Serif;
font-size: 13px;
margin-bottom: 5px;
display: block;
padding: 4px;
border: solid 1px #85b1de;
width: 300px;
}
Let me review the code above shortly. This is the code for the second example, the blue one. The entire form has a light blue background #d6ee5f4 and 10px padding. Each input element is displayed as a block, which ensures that labels are positioned above input fields.
Now, this was very simple. But you can do more. You can…
… add some background
You can also add some solid background like in the example below
#inputArea input[type="text"], #inputArea textarea
{
font-family: Arial, Sans-Serif;
font-size: 13px;
margin-bottom: 5px;
display: block;
padding: 4px;
border: solid 1px #85b1de;
width: 300px;
background-color: #EDF2F7;
}
Or you can add a soft gradient as a background. The examples above shows gray and blue gradients.
#inputArea input[type="text"], #inputArea textarea
{
font-family: Arial, Sans-Serif;
font-size: 13px;
margin-bottom: 5px;
display: block;
padding: 4px;
border: solid 1px #85b1de;
width: 300px;
background-image: url( 'blue_bg.png' );
background-repeat: repeat-x;
background-position: top;
}
The trick is simple and is contained in last three lines of the code. You add gradient image as background, set it to be repeated horizontally (repeat-x), and position it to the top of the field. Simple, eh?
But you can do even more! You can…
…add some behavior
But very simple. Make the active input field different. Like in the example below.
#inputArea input[type="text"]:focus, #inputArea textarea:focus
{
background-image: none;
background-color: #ffffff;
border: solid 1px #33677F;
}
As you can see, the code is very simple. Each time a field get the focus a different styles will be applied. It changes the background and the border. But wait! This doesn't work in Internet Explorer !! So we have to call JavaScript (or jQuery) for help. Ok, you now know that I lied. You can't do this only with CSS. But, hey, it's not my false, talk to IE guys :)
Ok, to do this, we have to change our CSS slightly.
#inputArea input, #inputArea textarea
{
font-family: Arial, Sans-Serif;
font-size: 13px;
margin-bottom: 5px;
display: block;
padding: 4px;
width: 300px;
}
We will define all styles for all inputs and texareas except for background and border.
.activeField
{
background-image: none;
background-color: #ffffff;
border: solid 1px #33677F;
}
.idle
{
border: solid 1px #85b1de;
background-image: url( 'blue_bg.png' );
background-repeat: repeat-x;
background-position: top;
}
Next, we'll define two classes that will set the styles for idle and active state. And a touch of jQuery, and viola!
$(document).ready(function(){
$("input, textarea").addClass("idle");
$("input, textarea").focus(function(){
$(this).addClass("activeField").removeClass("idle");
}).blur(function(){
$(this).removeClass("activeField").addClass("idle");
});
});
It is now working in IE. Of course, it is working in Firefox as well. This code does three things: initially, it adds "idle" class to all of the inputs and defined behavior for focus and blur events. Maybe not perfect, but it's working.
So, what else you can do?
Experiment
Yeas, that's right, experiment with different colors, border sizes and backgrounds. You can for example also add hover functionality. Try, and get rid of boring forms!
You example covers only textbox and textarea. What about combos, radio buttons, check boxes etc.?
Ramesh, this was just a simple retouching with a few lines of css code. For checkboxes and smilar controls you have to use more complex solutions or to find some scripts like http://lipidity.com/fancy-form/
Thanks for sharing your knowledge with us; with your help internet is getting more friendly for all.
Nice tricks but one thing that many people who style their input widgets forget is that not everyone has his system set up to have "black text on white ground" as default.
So if you skin one thing like the background to have some white-ish color don’t forget to set the color of the text to the color you want (maybe black). Doing just half the skinning is really problematic.
@tante: Exactly, one has to take care about their own color schema. The examples I provided present "typical" color schemas and are easy to use for a tutorial like this
Junko just keep give me more of those, please don’t stop!
I like it ;-)
I’ve pasted the #inputArea input[type="text"], #inputArea textarea into the styles tag and nothing happens. I’m using firefox 2.0 Am I missing something?
Muhammad, thanks for your support :D
John, just make sure that your form is placed inside some container with the id=inputArea. For example: <div id="inputArea"><input …></div>
Very nice. You’re a peach for sharing this. :D Thanks
Thank you for very nice tutorial. Very nice site by the way :)
Good advice. Small changes like these can really add a professional touch to forms.
thanks
Very useful techniques. I have used some of them in my previous blog design (now I am not using in my current blog)!
Very cool. I’ve been using blueprint’s form css for a similar effect.
http://code.google.com/p/blueprintcss/
where can i get the source code or sample downloads…
Janko,
some good tips. also, in jQuery there is a "toggleClass" method, that will add the class given if it’s not there, or remove it if it is there… might simplify your js a little…
Wow, it’s amazing how the addition of a little colour can really lift the form off the page! Great tutorial, I really enjoyed it. Thanks.
Thanks….Small changes like these can really make a difference.
@ice cool: I added the example and updated the article, thanks
Great Article dude. I just love your posts. Could you provide us the background image you are using in your examples ?
Keep up the good work.
Thanks.
Janko, here is another trick for enhancing usability:
http://www.dailycoding.com/Posts/default_text_fields_using_simple_jquery_trick.aspx
Why the need of JS?
You could have used
input:focus
{
background-image: none;
background-color: #ffffff;
border: solid 1px #33677F;
}
instead of all that jquery stuff.
Jeena, that works fine in Firefox, but it doesn’t work in IE. So you have to use some JS to make it work.
Helton, you can download the entire example here: http://www.jankoatwarpspeed.com/file.axd?file=input-fields.zip
Ramesh, very nice one!
Thanks Janko..
Janko, useful sharing, thanks.
Janko,
Thanks for the great post. If you get a chance checkout my post http://beckelman.net/post/2008/08/03/Messing-Around-with-jQuery.aspx for some my ideas I found that could improve the form using jQuery.
Thanks again for your great posts.
Bill
Thank you Bill. Very nice examples, jQuery really rocks!
(A)
I like it I like it a lot
Thanks for sharing your thoughts. Excellent and very useful
wow :) looks so cool, will try these css tricks to make my blog forms look good. Thanks :D
This is a fantastic tutorial. I love the small changes that can make a huge difference in the design like the gradient and the behavior techniques. You’ve just been bookmarked and Stumbled.
CSS fields are the cheese to my macaroni. – Awesome stuff thanks for posting!
I wrote about this back in April, check it out: http://techknack.net/pretty-up-your-forms-with-css/
thanks for the sharing
I like the blue borders on blue. Might consider this for our swim product.
Hej great wizzard of CSS. why did you not implement a CSS file when printing your web pages? (D)
janko… by default its applying the effect to all the inputs on my page… i dont want it to effect all the input boxes.. how can change the jquery to only effect the inputarea div controls….
@ice cool: just change jQuery selector to match only inputs inside your wrapper div, for example: $("#inputArea input, #inputArea textarea") It’s pretty much the same as css.
Patrick, I might consider it for the future, but not for now. Anyway, there is nothing "wizardish" in this tutorail, just a few tips for the beginners :)
@janko… i tried your suggestion and it worked but the only problem is that whenver my other input buttons are clicked they get the effect of this class ie… when clicked it becomes all white colored…
@ice cool: hmm, that’s strange.. did you use $("#inputArea input, #inputArea textarea") selector to add class "idle"and for focus() and blur() functions?
yeah…. the other area buttons only become when clicked… after click they are back to there normal class style… the white effect is only seen when the button is active….
@ice cool: Can you send me an example of your work so that I can debug it? It could be just a sample, no need to send me a real code.
Love it, makes it simple… as a side note, it’s spelled voila… but i don’t want to be one of *those* people… :-#
dear janko, i m trying to use it within my asp.net 2.0 project… i have a login control at the top left side of my page… and i have subscription form, for this form i m using this design style… do u want to me send u the sample code ???/
@ice cool: I can’t figure out what the bug is from what you described. If you wrapped the subscription form in inputArea div, and used jquery function like this:
$(document).ready(function(){
$("#inputArea input, #inputArea textarea").addClass("idle");
$("#inputArea input, #inputArea textarea").focus(function(){
$(this).addClass("activeField").removeClass("idle");
}).blur(function(){
$(this).removeClass("activeField").addClass("idle");
});
});
everything should work properly.
These look great and the jquery is an additional spark~!
wow really great info. about wordpress comment page area :D
i love wordpress..
simple but great looks
Great tutorial Janko.
This is one popular post.
I can see why amazing tutorial keep on it :)
Excellent review of some solid concepts!
I like the ‘at warp speed’ approach/concept too. Great post! Thumbs up!
Cool Input fields… (H)
very nice … I especially like the colors you chose.
Thanks you very much . Great tutorial for me !
Thanks for your tips. I am thinking about setting up a blog on my site and can see where these might me useful.
Hey, nice form bro.
Looks good, I’ll have to try this in my next project.
That’s a wonderful improvement. Thanks for teaching us!
Great tutorial, just what I’ve been looking for.
nice, pretty and very helpfull trick, thanks
Very nice!
Very Good!
Thank you, its a well written and needed post. I also like your site design.
Great article and great site. I’ve pretty much got comfortable with my preferred form layout now, though I think I should start looking at new ideas and maybe some fancy ajax / jquery effects.
Thanks for the inspiration!
thank you man this is helpful i will use it soon in my site http://gamezat.net/
Janko your pretty good..wanna say thank you for sharing your ideas.really helpful especially for newbies.
this is a great article,list and site.
thumbs up on SU
very nice!,.tnx for this..^^
I agree, experimenting takes you a long way into making more beautiful forms. Nice post btw.
顶之
This looks really really nice – thanks alot Janko!
GREAT tips for beginners looks to make their CSS forms look more pro! I’m a software developer and I found this to be very useful for my students in my programming class I teach.
thank you so much for sharing
pretty nice tutorial =)
Hi. Thanks fot the tut, great idea. I tried on my blog and on Firefox and Chrome it’s ok…but on IE doesn’t work. I use jQuery as you said. Some ideas?! Thanks
Excuse me, it’s my fault. My IE dosn’t show js! Thank you, your blog it’s great!
Thank you man, good AIO explanation. Very often used trick and look nice.
Selam from Sarajevo
hi.. great tutorial.. i’ve successfully implement it for my project…
however, does this trick not working with <input type="file"> ?
Thank you for this article
Comments are closed. Discuss this article on twitter.
Copyright © Janko Jovanovic 2008 - 2016