27. July 2008 by Janko in Tutorials | tags: , ,

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!

If you liked this article why don't you share it:

Enhance your input fields with simple CSS tricks (via @jankowarpspeed) Share this on StumbleUpon Share this on delicious Share this on Digg Share this on Dzone Share this on DesignBump Send this to friend

Comments

Pingbacks and trackbacks

  1. Trackback from DotNetKicks.com Enhance your input fields with simple CSS tricks
  2. Trackback from Design Bump Story on DesignBump.com
  3. Pingback from pablofabregat.com.ar Pablo Fabregat » Un poco de estilo para nuestros formularios
  4. Pingback from fuzzlinks.com FuzzLinks.com » Janko At Warp Speed - Enhance your input fields with simple CSS tricks
  5. Pingback from lrwv.com Enhance your input fields with simple CSS tricks | Top 1K + Popular Website Links for Web Designer, Techie & Office Worker - Web App
  6. Pingback from anidandesign.com links for 2008-07-28 | Anidan Design : eco-friendly web & graphic design
  7. Pingback from jryj.com Input Field Formatting Tips With CSS my.bookmarks
  8. Pingback from fatihhayrioglu.com Fatih HayrioÄŸlu'nun not defteri » 29 Temmuz 2008 web’den seçme haberler » CSS'de, karşılaşılan, BaÄŸlantı, İnternette, kolaylaÅŸtıran, Firefox, jQuery, açılır, menüde, seçili
  9. Pingback from hamazleg.co.il שיפור טפסים בעזרת CSS איכותי יותר… | בלוג עיצוב | המזלג
  10. Pingback from vincenthomedev.wordpress.com Enhance your input fields with simple CSS tricks « vincenthome’s Software Development
  11. Trackback from pleaseit.com Janko At Warp Speed - Enhance your input fields with simple CSS tricks
  12. Pingback from crazyleafdesign.com Best Design Resources of July 2008 | CrazyLeaf Design Blog
  13. Pingback from brunosilva.net » Bruno Silva - Text label “inside” password input (safe without javascript)
  14. Pingback from indicegeek.com Mejorar la apariencia de tus Formularios
  15. Pingback from gelund.com gelund.art.music.politics» Blog Archive » Better looking web forms
  16. Pingback from 8bitsofcoffee.wordpress.com Links Week-1 « 8-bits of Coffee
  17. Pingback from socialcmsbuzz.com 40 CSS Web Form Style Tutorials For Web Developers | Social CMS Buzz
  18. Pingback from cnet.ro Janko ÅŸi aspectul formularelor web | CNET.ro
  19. Trackback from SAMUEL BRAN MODIFICANDO LA APARIENCIA DE LOS FORMULARIOS.
  20. Pingback from white-fusion.com 14 Great Coding Resources | White Fusion
  21. Pingback from mattiasgeniar.be 6 Hints To Pimp Your Forms With Fancy CSS | All your blog are belong to us
  22. Trackback from vBharat.com » Enhance your input fields with simple CSS tricks From vBharat.com » Enhance your input fields with simple CSS tricks
  23. Pingback from funnyweblink.com links for 2008-11-05 | Funny Web Pages
  24. Pingback from speckyboy.com 20+ Resources and Tutorials for Creative Forms using CSS : Speckyboy Design Magazine
  25. Pingback from guidesigner.net Best Tips, Resources and Tutorials for making of Creative Forms using CSS | guidesigner.net
  26. Pingback from answerspluto.com list of urls 4 « Answers Pluto
  27. Pingback from millionaire.websitesuperhero.com Enhance your input fields with simple CSS tricks
  28. Pingback from 1stwebdesigner.com 51 Form Element Resources and Tutorials Using CSS And Javascript | Graphic and Web Design Blog -Resources And Tutorials
  29. Pingback from huibit05.com 51 Form Element Resources and Tutorials Using CSS And Javascript | huibit05.com
  30. Pingback from pc-aras.com 51 Form Element Resources and Tutorials Using CSS And Javascript | pc-aras
  31. Pingback from neurosoftware.ro 51 Form Element Resources and Tutorials Using CSS And Javascript - Programming Blog
  32. Pingback from portlak.com Fısıltılar @ Portlak.com
  33. Pingback from macfoo.wordpress.com Hide borders for an input box « Come get your GEEK on!
  34. Pingback from smashingbuzz.com 25 Ultimate Useful Form Tutorials Using CSS and Javascript | Smashing Buzz
  35. Pingback from vocidalweb.it 25 splendidi tutorial per creare form usando CSS e Javascript | Voci dal Web

Add comment

   
        Country flag
biuquote
Loading