Skip to content

Adding focus to form fields

You can highlight the form field a user is in. It’s a small visual cue as to where the cursor is in the form. Here’s a simple example.

a form with the focus in the Name field

The cursor is in the Name field in the image. The cursor position is highlighted with a dark border to indicate which form field is in focus. This highlight is created with a CSS pseudo property called :focus. Text boxes are input fields. In this example, the CSS rule is applied to the input selector. A border is added to input fields when they are in the :focus state. Here’s how the rule looks.

input:focus {
border: 2px solid #333;
}

This rule only works for input fields. Other types of form elements such as checkboxes would need separate CSS rules of a similar kind to create an outline when receiving focus.

Stumble it!  


4 Comments

  1. background-color is a way to show focus that can be subtle, yet very clear.

    Tuesday, February 2, 2010 at 9:14 am | Permalink
  2. vdebolt wrote:

    Excellent suggestion.

    Tuesday, February 2, 2010 at 12:49 pm | Permalink
  3. Lovely trick – but of course(!?) no good in Internet Explorer versions below IE8.

    Anybody else setting the declaration for IE8 to emulate IE7? I am – so as to remove yet another another layer of IE quirks.

    Oh well I guess at least it’s some more progressive enhancement for non-IE users. It shall be used, thanks. [hat tip to Laura Carlson's WEB DESIGN UPDATE for continually feeding me tips-n-tricks.]

    Friday, February 5, 2010 at 1:58 pm | Permalink
  4. vdebolt wrote:

    You’re so right. :focus won’t work for an input element on IE<8.

    The entire web design community owes a big thank you to Laura Carlson, I’d certainly second that.

    Friday, February 5, 2010 at 3:24 pm | Permalink

One Trackback/Pingback

  1. Cascading Style Sheets Reference Links | kabayview.com on Monday, October 10, 2011 at 6:53 am

    [...] Adding Focus to Form Fields – Virginia DeBolt [...]

What can you contribute?

Your email is never published nor shared. Required fields are marked *
*
*

CommentLuv badge