Creating an accessible yet sexy search box with CSS

Create a Search BoxSearch boxes: integral parts of many websites but often neglected in terms of markup and style. When considering both accessibility and semantics, marking up a a search box can be a little awkward depending on the look you are trying to achieve.

In this post I will suggest some methods of marking up your search boxes, and show a neat way of styling it using css and a few images.

First things first

Lets think about what is needed. For a search box we need:

  1. A form element
  2. A text input
  3. A submit button

Also, ideally we need a label element for accessibility reasons so that users know what purpose the input element has (you can read more about labels and accessibility at 456 Berea St).

Therefore, here is the markup we could use for our form:

<form method="post" action="page.html">
<div>
<label for="search">Search:</label>
<input type="text" name="search" id="search" />
<input type="image" src="img/search.gif" alt="Search" />
</div>
</form>

This is pretty much the way most people would do it, but Im going to challenge this for the following reasons:

  1. We rarely want the label visible after styling
  2. Hiding the label with display:none can cause accessibility issues (link)
  3. Other methods of hiding the label can also cause issues (link)
  4. I want a method that does not require hiding the label…

Therefore I have come up with the following; this may have drawbacks which I’m hoping people will discuss in the article comments, any here goes:

<form method="post" action="page.html" id="searchform">
<div>
<input type="text" name="search" id="search" />
<label for="search"><input type="image" alt="Search" src="img/search3.gif" title="Search" /></label>
</div>
</form>

My reasoning behind this is, we still get the association between label and form element, the input has alt text of ‘Search’ which means this is what (in theory) the screen reader should pick up. Because this label doubles as the submit button we also don’t have to hide it. This method is also pretty easy to style. Lets get on to that now.

Styling the form

To style my form I will use 3 images: left curve, a gradient and the submit button. I could use a 4th image and tile it for the top and bottom borders, but for this Im just going to use css borders on the input box.

Now for the css:

#searchform div {
    /* This div will have the left image as a background */
    background: url(search1.gif) no-repeat left top;
    padding: 0 10px;
    margin: 0;
    line-height: 1;
}
#searchform #search {
    /* Im going to apply a top and bottom border to this input so that it fits with my images and give it the gradient background */
    border-top:2px solid #999;
    border-bottom:2px solid #999;
    border-left:0;
    border-right:0;
    background: #fff url(search2.gif) repeat-x top;
    padding: 3px 2px 2px 0;
    height: 15px;
}
#searchform input {
    /* Some reset styles to make my form elements play nice */
    vertical-align: top;
    margin: 0 !important;
    line-height: 1;
    outline:0 !important;
}

Internet explorer plays up a bit regarding input positioning but this can be overcome with a conditional comment to add a pixel of padding to the input box:

<!--[if lte IE 7]>
<style type="text/css" media="screen">
#searchform #search {
    position:relative;
    margin-top:-1px !important;
}
</style>
<![endif]-->

Heres what it looks like: (note: i’ve added the css directly in the markup for this demo :) )

Mission accomplished?

It looks nice, but how accessible and semantic do you think the code is? I personally think its better than simply hiding the label but I could be wrong. Discuss below!

Found this post useful? Why not buy me a coffee!

Related Entries

Popular Entries

8 Responses to “Creating an accessible yet sexy search box with CSS”

RSS feed for comments on this post.

  • 1 - tsquez says: Reply to this comment

    Gravatar

    Just wanted to say thanks for the tutorial. Been looking around for something like for a while now and this is bt far the best and easiest I have found. Once again thank you.

    Comment made on October 11, 2008 at 6:21 am

  • 2 - biorezonans says: Reply to this comment

    Gravatar

    thank u for your share ı recently learning css and everything useful for me…

    Comment made on October 17, 2008 at 3:04 pm

  • 3 - birkof says: Reply to this comment

    Gravatar

    Awsome tutorial …thanks a lot dude for sharing this

    Comment made on November 8, 2008 at 3:27 pm

  • 4 - Anthony Brewitt says: Reply to this comment

    Gravatar

    I was just searching to add a pretty search box to my blog, good work Mike.

    Comment made on February 3, 2009 at 3:31 pm

  • 5 - Me says: Reply to this comment

    Gravatar

    this has though one con. the button shows an image and thus does not depend on a css file

    Comment made on March 14, 2009 at 2:45 am

  • 6 - JG says: Reply to this comment

    Gravatar

    Nice tutorial, very easy to understand, big help!!!

    Comment made on March 19, 2009 at 6:50 pm

  • 7 - Asif Shabbir says: Reply to this comment

    Gravatar

    Yes very pretty. I can add it in over site. Thanks for posting. A nice box.

    Comment made on May 7, 2009 at 12:13 pm

  • 8 - Muhammad Imran says: Reply to this comment

    Gravatar

    This is great. Thanks for the trick. I will definetly add it to my site.

    Comment made on May 8, 2009 at 6:28 am

Leave a Reply

Why ask?

About this site

Blue Anvil is the online web design journal & portfolio of , a web designer from Norfolk, England. Read More »
ThemeSlice
  • Featured work - More

    • Beefjack
    • Integrity
    • theotaku.com
    • Manic Melon
  • Latest Tweet - More

    • Dropped yoghurt all down my pyjamas. Bugger. Working in my boxers. Ill get dressed some time. Soon.
  • Out of the blue - More

    • Wordpress 2.8 Memory Usage

      With the release of wordpress 2.8 some people are experiencing out of memory php errors along the lines of:

      Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 228968 bytes)

      You may also be seeing pages fail to load fully (for example the wordpress admin dashboard) if php error messages are hidden. I’ve already had one case where we thought certain plugins were to blame but in fact it was just out of memory. A possible fix (which worked in the previously mentioned case) is to add:

      @ini_set('memory_limit','64M');

      to your wp-config file. Has anyone else encoutered this error?

    • jQuery Curvy Corners 1.x and 2.x updated and moved to Google Code

      I have updated the jQuery Curvy Corners plugins (both versions) with jQuery 1.3.2 support and other enhancements. The beta 2 version is looking good and is working in all version of IE, Opera, and Firefox (as far as I can tell).

      You can grab the latest files from Google Code here. Enjoy.

    • I’m too nice: Wordpress Download Monitor plugin page add-on now included with Download Monitor version 3.1.

      It was going to be a paid add-on, but today I had a change of heart and bundled it with the newest version of download monitor. The add-on lets you make a download page using a shortcode; it lists your downloads/categories with full sorting, pagination, and search functionality. Not bad eh? See the documentation topic to see full instructions for usage, or see my download page to see it in action.

      And if you use it, please consider making a donation to ensure the continued development of the plugin!

    • 2 Announcements: New Support forum, and feedback wanted for new download page add-on

      First, I’ve implemented a support forum to Blue Anvil mainly for plugin support and ideas which can be found here. Hopefully this will make supporting my plugins easier. Feel free to add to the discussions (there is also a general web design forum too).

      Secondly, I’ve added a demo of the new download page add-on I’m making for Download Monitor. This will be a paid add-on and it would be cool to get any feedback or suggestions from anyone who would like such a feature. My download page is here. Please leave feedback on the forum or in the comments.