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

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

RSS feed for comments on this post.

  1. tsquez says:

    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.

    ReplyReply

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

  2. biorezonans says:

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

    ReplyReply

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

  3. birkof says:

    Awsome tutorial …thanks a lot dude for sharing this

    ReplyReply

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

  4. Anthony Brewitt says:

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

    ReplyReply

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

  5. Me says:

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

    ReplyReply

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

  6. JG says:

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

    ReplyReply

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

  7. Asif Shabbir says:

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

    ReplyReply

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

  8. Muhammad Imran says:

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

    ReplyReply

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

  9. Bapun says:

    Thanks for the lovely tutorial. Btw is this possible to customize adsense search in this way?

    ReplyReply

    Comment made on July 6, 2009 at 7:17 pm

  10. Mike Jolley says:

    @Bapun: Unsure – google are strict with that kind of thing.

    ReplyReply

    Comment made on July 7, 2009 at 5:30 pm

  11. xBids says:

    Great article! I have been stuck searching online for how to make a css search box and now I have figured it out! Thanks again!

    ReplyReply

    Comment made on July 14, 2009 at 4:47 am

  12. Sedran says:

    Thanks a lot! :) I must use them :)

    ReplyReply

    Comment made on August 9, 2009 at 9:33 am

  13. fernando says:

    Hey, thanks but where can i find images search1.gif and search2.gif????

    please can you send me…

    ReplyReply

    Comment made on September 11, 2009 at 11:47 pm

  14. Travel says:

    This looks really good, thanks for the code.

    ReplyReply

    Comment made on November 22, 2009 at 6:34 pm

  15. John says:

    I need the graphics to this form ?.

    ReplyReply

    Comment made on November 27, 2009 at 7:17 pm

  16. Mike Jolley says:

    @John: Copy them from the source :)

    ReplyReply

    Comment made on November 27, 2009 at 9:32 pm

  17. Rahuman says:

    It’s fantastic, I love it….

    ReplyReply

    Comment made on December 23, 2009 at 5:02 am

  18. value my car says:

    Excellent tutorial! This is going to come is super handy for a project I’m working on. You da boss!

    ReplyReply

    Comment made on February 15, 2010 at 8:31 am

  19. Pazador says:

    GRACIAAAAAAAAAAAAS
    TE ADORO MI REY :D

    ReplyReply

    Comment made on February 19, 2010 at 3:07 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
  • Latest Tweet - More

    • @skjreilly no problem :)
  • Out of the blue - More

    • Switched: From Shared to VPS

      It’s been about two weeks now since I made the transition from a shared reseller hosting account to a VPS (Virtual Private Server) account – impressions so far, excellent performance but fiddly to configure.

      The reason I wanted to change from shared hosting was the fact the server was always being hacked (even though ALL my scripts were secure), there was frequent downtime, support blamed me for problems every time, and it was slow as hell.

      Those used to a shared hosting environment would probably not know where to start when faced with configuring a VPS. Luckily, a lot of it was pre-configured when I received my account – certainly some of the major security holes were patched. I was not satisfied with those however. As a victim of hacking in the past (previous host swears it was not there fault, something I don’t believe) I took extra care to secure it as a much I could – configuring brute force detection, the firewall, installing mod security (excellent rules for that here: http://www.atomicorp.com/wiki/index.php/Atomic_ModSecurity_Rules) and going though multiple guides (like this one: http://www.webhostingtalk.com/showthread.php?t=468168) with a fine-tooth comb.

      The result? My pages are loading at least 6 times faster, I have had no down time (or at least have not noticed any), and I feel in control and happy. No longer am I at the mercy of shared hosts :)

      If your interested, I chose ServInt as my provider as they offered a great deal, as well as being a managed service (so I’m not on my own if I screw things up). I was tempted by the bells and whistles of Media Temple, but felt the ServInt service was better value.

    • Download Monitor 3.2.2 Maintenance Release

      Download Monitor has received some more love and has been updated. Here’s the change log from the new version:

      • Small bugfix in uploader.php – cat ID
      • Changed stats graph calculation – thanks lggemini
      • Changes to headers in download.php to avoid caching
      • File Browser fixes – $root was clashing with something….
      • exclude_cat works in all sections of download_page now
      • Removed hardcoding of /uploads/
      • Added action to download.php – should be able to use it to stop a download if you want – maybe limiting downloads per day or something? Whatever you want…
      • Made it so if you post new file on ‘edit’ screen, the post date is updated.
      • Fixed the ‘blank meta’ section which blanks out custom field values when nothing is set.
      • Moved ‘allow_url_fopen’ check.
      • Someone said downloads don’t work with spaces in the name. They do! Wasting my time sonny…
      • All work and no play make jolley a dull boy
      • Had to rename capabilities so they work. Apologies if you have to set this up again! Cheers to Mark Dingemanse.
      • {category_ID} custom format tag added. Useful if you want to send someone to its category on the DL page I guess. Also added {category_other} so when no category is set “other” is shown – this is because the download page can show an ‘other’ section if you want it to.
      • You can now manually edit the post date on the edit download screen.

      If you have edited capabilities for download monitor user permissions, you’ll have to again sorry! This is because I named them too long. Also, you should check your forced downloads still work because there was a logic error meaning they may not have been forced after-all…

      Enjoy.

    • Mahousive update to Download Monitor (3.2)

      Today I completed the update for the Wordpress Download Monitor Plugin – many tweaks, fixes, and features added. There were no changes to the database structure so people upgrading should be fine. Here is the list from the change log:

      • {user} tag added for custom formats
      • ‘autop’ option fix
      • Download page buttons applied with CSS so they are easier to customise/translate.
      • Fix for pagination bug after editing a download
      • Category output fix on edit downloads screen
      • Category urls on download page use ID rather than name to prevent errors when cats have the same names.
      • exclude_cat added to download_page shortcode
      • Localised ‘hits’ ‘date’ ‘title’ on download page
      • Option to disable the download logging
      • Read file ‘chunked’ some people found large files were corrupted so this should help (fingers crossed)
      • Added show_tags option to download page – displays x amount of tags on the download page.
      • File Browser root setting and download.php logic/mime types modified thanks to Jim Isaacs (jidd.jimisaacs.com)
      • Interface Improvements
      • Bulk edit categories, custom fields, tags, member only downloads
      • Added roles for download monitor admin – should be able to use with a role manager plugin if you want anyone other than admin to access the admin section e.g. http://wordpress.org/extend/plugins/capsman/
      • Change redirect after add
      • Edit Cat names/parents
      • Dedicated tags and thumbnails fields (they still use meta table though)

      And yes, those category link bugs are fixed at long last, and you can edit category names finally. Phew!

    • Wordpress Spam Stopper Plugin Updated

      Spam stopper has been updated to v3.1 – and most of it has been recoded. Here’s the full list of changes:

      • Added changelog to readme.
      • Email validation bug squashed
      • Cached comments now work; if user forgets to fill in antispam or makes a mistake (and the JS does not catch it) the users comment will not be lost.
      • Redone entire code to make it more efficient
      • Admin section added for changing the antispam question
      • Form ID and honeypot trap added to form
      • Fully localized

      You can get the plugin from wordpress.org: http://wordpress.org/extend/plugins/spam-stopper/

      For support, please keep my comments clean and post on either the wordpress forums or my forum.

      To help support spam-stopper you can make a donation (buy me a coffee, or several) or rate it on wordpress.org. Thanks!