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.

See it live

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

input fields

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

input fields

#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.

 input fields 

input fields

#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.

input fields

#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!

More articles in Blog archive or elsewhere
Advertisement

78 Comment(s)

Ramesh Soni

Ramesh Soni 28 Jul 2008 #

You example covers only textbox and textarea. What about combos, radio buttons, check boxes etc.?

Janko

Janko 28 Jul 2008 #

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/

elcodigodebarras

elcodigodebarras 28 Jul 2008 #

Thanks for sharing your knowledge with us; with your help internet is getting more friendly for all.

tante

tante 28 Jul 2008 #

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.

Janko

Janko 28 Jul 2008 #

@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

Muhammad Mosa

Muhammad Mosa 29 Jul 2008 #

Junko just keep give me more of those, please don't stop!
I like it Wink

John

John 29 Jul 2008 #

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?

Janko

Janko 29 Jul 2008 #

Muhammad, thanks for your support Laughing

John, just make sure that your form is placed inside some container with the id=inputArea. For example: <div id="inputArea"><input ...></div>

Romeo

Romeo 29 Jul 2008 #

Very nice. You're a peach for sharing this. Laughing Thanks

Bash Bosh

Bash Bosh 29 Jul 2008 #

Thank you for very nice tutorial. Very nice site by the way Smile

Colin Robertson

Colin Robertson 29 Jul 2008 #

Good advice. Small changes like these can really add a professional touch to forms.

thanks

Veera

Veera 29 Jul 2008 #

Very useful techniques. I have used some of them in my previous blog design (now I am not using in my current blog)!

jrummell

jrummell 29 Jul 2008 #

Very cool.  I've been using blueprint's form css for a similar effect.
http://code.google.com/p/blueprintcss/

ice cool

ice cool 29 Jul 2008 #

where can i get the source code or sample downloads...

dave

dave 29 Jul 2008 #

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

John McCollum

John McCollum 29 Jul 2008 #

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.

George

George 29 Jul 2008 #

Thanks....Small changes like these can really make a difference.

Janko

Janko 30 Jul 2008 #

@ice cool: I added the example and updated the article, thanks

Helton Valentini

Helton Valentini 01 Aug 2008 #

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.

Ramesh Soni

Ramesh Soni 01 Aug 2008 #

Janko, here is another trick for enhancing usability:
www.dailycoding.com/.../...imple_jquery_trick.aspx

Jeena

Jeena 01 Aug 2008 #

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.

Janko

Janko 01 Aug 2008 #

Jeena, that works fine in Firefox, but it doesn't work in IE. So you have to use some JS to make it work.

Janko

Janko 02 Aug 2008 #

Helton, you can download the entire example here: www.jankoatwarpspeed.com/file.axd

Ramesh, very nice one!

Ramesh Soni

Ramesh Soni 03 Aug 2008 #

Thanks Janko..

HC

HC 04 Aug 2008 #

Janko, useful sharing, thanks.

Bill Beckelman

Bill Beckelman 04 Aug 2008 #

Janko,

Thanks for the great post. If you get a chance checkout my post beckelman.net/.../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

Janko

Janko 05 Aug 2008 #

Thank you Bill. Very nice examples, jQuery really rocks!

Eneza

Eneza 05 Aug 2008 #

Innocent

I like it I like it a lot

Narayana Moorthy

Narayana Moorthy 05 Aug 2008 #

Thanks for sharing your thoughts. Excellent and very useful

thenetguruz

thenetguruz 05 Aug 2008 #

wow Smile looks so cool, will try these css tricks to make my blog forms look good. Thanks Laughing

evpstud

evpstud 05 Aug 2008 #

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.

Affordable Web Design

Affordable Web Design 08 Aug 2008 #

CSS fields are the cheese to my macaroni. - Awesome stuff thanks for posting!

Andrew

Andrew 08 Aug 2008 #

I wrote about this back in April, check it out: techknack.net/pretty-up-your-forms-with-css/

dj_rov

dj_rov 09 Aug 2008 #

thanks for the sharing

Tom

Tom 13 Aug 2008 #

I like the blue borders on blue. Might consider this for our swim product.

Patrick Kwinten

Patrick Kwinten 13 Aug 2008 #

Hej great wizzard of CSS. why did you not implement a CSS file when printing your web pages? (D)

ice cool

ice cool 13 Aug 2008 #

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

Janko

Janko 13 Aug 2008 #

@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 Smile

ice cool

ice cool 13 Aug 2008 #

@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...

Janko

Janko 13 Aug 2008 #

@ice cool: hmm, that's strange.. did you use $("#inputArea input, #inputArea textarea") selector to add class "idle"and for focus() and blur() functions?

ice cool

ice cool 14 Aug 2008 #

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

Janko

Janko 14 Aug 2008 #

@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.

Scott Waite

Scott Waite 15 Aug 2008 #

Love it, makes it simple... as a side note, it's spelled voila... but i don't want to be one of *those* people... Sealed

ice cool

ice cool 17 Aug 2008 #

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 ???/

Janko

Janko 17 Aug 2008 #

@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.

Mepho

Mepho 18 Aug 2008 #

These look great and the jquery is an additional spark~!

Save Youtube

Save Youtube 19 Aug 2008 #

wow really great info. about wordpress comment page  area Laughing

i love wordpress..

amik

amik 20 Aug 2008 #

simple but great looks

Samuel Bran

Samuel Bran 24 Aug 2008 #

Great tutorial Janko.

Amr Elsehemy

Amr Elsehemy 25 Aug 2008 #

This is one popular post.
I can see why amazing  tutorial keep on it Smile

Wayne

Wayne 27 Aug 2008 #

Excellent review of some solid concepts!

I like the 'at warp speed' approach/concept too.  Great post! Thumbs up!

Julius Bacosa

Julius Bacosa 27 Aug 2008 #

Cool Input fields... Cool

Sava

Sava 04 Sep 2008 #

very nice ... I especially like the colors you chose.

Danh ba web 2.0

Danh ba web 2.0 15 Sep 2008 #

Thanks you very much . Great tutorial for me !

tommy

tommy 21 Sep 2008 #

Thanks for your tips. I am thinking about setting up a blog on my site and can see where these might me useful.

loreto

loreto 23 Oct 2008 #

Hey, nice form bro.

John

John 07 Nov 2008 #

Looks good, I'll have to try this in my next project.

VJPQ Directory

VJPQ Directory 17 Nov 2008 #

That's a wonderful improvement. Thanks for teaching us!

Rene

Rene 22 Nov 2008 #

Great tutorial, just what I've been looking for.

Leandro

Leandro 11 Dec 2008 #

nice, pretty and very helpfull trick, thanks

yoyocaso

yoyocaso 25 Dec 2008 #

Very nice!

Vaibhav Gupta

Vaibhav Gupta 30 Dec 2008 #

Very Good!

Jamiel Sharief

Jamiel Sharief 02 Feb 2009 #

Thank you, its a well written and  needed post. I also like your site design.

Web Design Manchester

Web Design Manchester 02 Feb 2009 #

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!

العاب

العاب 08 Feb 2009 #

thank you man this is helpful i will use it soon in my site http://gamezat.net/

software developement

software developement 10 Feb 2009 #

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

사 라

사 라 23 Feb 2009 #

very nice!,.tnx for this..^^

Scarf*oo

Scarf*oo 05 Mar 2009 #

I agree, experimenting takes you a long way into making more beautiful forms. Nice post btw.

linY

linY 10 Mar 2009 #

顶之

Scott Mackie

Scott Mackie 14 Mar 2009 #

This looks really really nice - thanks alot Janko!

Affordable Software

Affordable Software 15 Mar 2009 #

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.

ahmadmarafa

ahmadmarafa 29 Apr 2009 #

thank you so much for sharing

Robert

Robert 03 Jun 2009 #

pretty nice tutorial =)

liliansi

liliansi 07 Jul 2009 #

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

liliansi

liliansi 07 Jul 2009 #

Excuse me, it's my fault. My IE dosn't show js! Thank you, your blog it's great!

Haris

Haris 25 Sep 2009 #

Thank you man, good AIO explanation. Very often used trick and look nice.
Selam from Sarajevo

shawn

shawn 30 Oct 2009 #

hi.. great tutorial.. i've successfully implement it for my project...

however, does this trick not working with <input type="file"> ?

Nardu

Nardu 07 Nov 2009 #

Thank you for this article

Comments are closed
via Ad Packs
9292