How to create a cool and usable CSS3 search box

In this new article, you’ll learn how create a cool and usable CSS3 search box using the HTML5 placeholder attribute. For the browsers that don’t support this new HTML5 attribute, a fallback is created using Modernizr’s feature detection.


View demo

Structure

The form element is used as the wrapper, while the two inputs are used as search field and search button respectively.

The HTML code

<form id="searchbox" action="">
    <input id="search" type="text" placeholder="Type here">
    <input id="submit" type="submit" value="Search">
</form>

You may notice the placeholder attribute, but just ignore it for now, as we will talk later about it.

The reason why there are so many id’s (instead of CSS3 advanced selectors as input[type="text"] or input[type="submit"]) is because I wanted this to degrade gracefully for older browsers.

The CSS

Form wrapper

#searchbox
{
	background: #eaf8fc;
	background-image: -moz-linear-gradient(#fff, #d4e8ec);
	background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #d4e8ec),color-stop(1, #fff));
	
	-moz-border-radius: 35px;
	border-radius: 35px;
	
	border-width: 1px;
	border-style: solid;
	border-color: #c4d9df #a4c3ca #83afb7;            
	width: 500px;
	height: 35px;
	padding: 10px;
	margin: 100px auto 50px;
	overflow: hidden; /* Clear floats */
}

Below you can see the current result:

Inputs

Quick tip:

When adding float: left to an element, there’s no need to add also display: block. The last one it’s implied.

#search, #submit
{
	float: left;
}

#search
{
	padding: 5px 9px;
	height: 23px;
	width: 380px;
	border: 1px solid #a4c3ca;
	font: normal 13px 'trebuchet MS', arial, helvetica;
	background: #f1f1f1;
	
	-moz-border-radius: 50px 3px 3px 50px;
	 border-radius: 50px 3px 3px 50px;
	 -moz-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);
	 -webkit-box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);
	 box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25) inset, 0 1px 0 rgba(255, 255, 255, 1);            
}

/* ----------------------- */

#submit
{		
	background: #6cbb6b;
	background-image: -moz-linear-gradient(#95d788, #6cbb6b);
	background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #6cbb6b),color-stop(1, #95d788));
	
	-moz-border-radius: 3px 50px 50px 3px;
	border-radius: 3px 50px 50px 3px;
	
	border-width: 1px;
	border-style: solid;
	border-color: #7eba7c #578e57 #447d43;
	
	 -moz-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
	 -webkit-box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;
	 box-shadow: 0 0 1px rgba(0, 0, 0, 0.3), 0 1px 0 rgba(255, 255, 255, 0.3) inset;   		

	height: 35px;
	margin: 0 0 0 10px;
        padding: 0;
	width: 90px;
	cursor: pointer;
	font: bold 14px Arial, Helvetica;
	color: #23441e;
	
	text-shadow: 0 1px 0 rgba(255,255,255,0.5);
}

#submit:hover
{		
	background: #95d788;
	background-image: -moz-linear-gradient(#6cbb6b, #95d788);
	background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #95d788),color-stop(1, #6cbb6b));
}	

#submit:active
{		
	background: #95d788;
	outline: none;
   
	 -moz-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;
	 -webkit-box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;
	 box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5) inset;		
}

#submit::-moz-focus-inner
{
       border: 0;  /* Small centering fix for Firefox */
}		

HTML5 placeholder attribute

This new HTML5 attribute shows a text in a field as long as the field is empty and not focused, then hides the text. You surely have seen this technique before with JavaScript!

Browser support:

  • Firefox 3.7+
  • Safari 4.0+
  • Chrome 4.0+
  • Opera 11+
  • IE10+

Opera 11 supports it, but you cannot style it (yet). As for the others above, here’s how you can style it:

#search::-webkit-input-placeholder {
   color: #9c9c9c;
   font-style: italic;
}

#search:-moz-placeholder {
   color: #9c9c9c;
   font-style: italic;
}  

#search:-ms-placeholder {
   color: #9c9c9c;
   font-style: italic;
}   

Fallback support

For web browsers that are not supporting the this new HTML5 attribute, I made a fallback.

I used Modernizr to detect native support for the HTML5 placeholder attribute. Even if this could have been done by writing a short function, I love Modernizr as it also enables you to use more semantic elements from the HTML5 specs.

#search.placeholder {
   color: #9c9c9c !important;
   font-style: italic;
} 
$(document).ready(function() {           
	if (!Modernizr.input.placeholder)
	{		
		var placeholderText = $('#search').attr('placeholder');
		
		$('#search').attr('value',placeholderText);
		$('#search').addClass('placeholder');
		
		$('#search').focus(function() {				
			if( ($('#search').val() == placeholderText) )
			{
				$('#search').attr('value','');
				$('#search').removeClass('placeholder');
			}
		});
		
		$('#search').blur(function() {				
			if ( ($('#search').val() == placeholderText) || (($('#search').val() == '')) )                      
			{	
				$('#search').addClass('placeholder');					  
				$('#search').attr('value',placeholderText);
			}
		});
	}                
});

View demo

Chrome’s inset box-shadow bug

There is a bug on Chrome when both border-radius and inset box-shadow are used. Anyway, there is good news about that. Paul Irish announced last month that Chrome’s inset box-shadow bug is fixed.

Later update

The demo was updated, thanks for pointing this out Atul.

So, if you’re using Chrome beta 10.0.648.119 or a greater version, this should work just perfect!

Conclusion

This example it’s mostly about progressive enhancement.

Regarding the CSS, as you will notice, this example will degrade gracefully for other old browsers. Now, regarding the HTML5 placeholder attribute, if native support is missing, then the Javascript code will do it for you.

85 thoughts on “How to create a cool and usable CSS3 search box

      • sir can ask a favor you make the search bar but can you add how to add a search profile like something i wanna search like that how to do that? i do not have any clue thou >.<

  1. Thanks for the excellent write-up, Catalin. I guess the Chrome’s inset box-shadow bug is back in version 10.0.648.119 beta

    • Thanks for your comment Atul!

      I made some “investigations” and I updated the article. Now the example is working also on Chrome beta 10.0.648.119.

      Good remark!

    • Skilleo, I’m aware I need to improve my Javascript skills but this article isn’t about how to write optimized JS code.

      Thanks for your comment! ;)

  2. Thank you for sharing this nice technique!

    Catalin, can I translate this article into Russian and post in on the Russian collective IT-blog with the link to original?

    PS. By the way, hello from Chisinau, Moldova :)

  3. I have read quite a few tutorials about form coding and nothing comes closer to this one. Great job! Will definitely implement it on my projects.

    • Jake, you should encounter no problems when implementing this search for your Google custom search.

      In order to do that, you just need to replace/update some ID’s and classes and that should do it.

      If you still need some help, just send me an email.

      Thanks for your comment!

  4. Hey,

    just wanted to say thanks for this super easy to follow tutorial. Really impressed with your helpful explanations covering all the bases (this is just what I need right now).

    Thank you!

  5. I wanted to ask, is this search bar fully functional? I was wondering if I need to create a separate page like the contact form tutorial you gave. So yeah, hope you reply soon!

    • The above search box isn’t functional, is just CSS3. For example you could integrate it further with something like Google custom search.

      • But if I didn’t want to use the Google CSE, how do I make it work myself? I know the action=”" is suppose to lead to somewhere… but where? And even if i know where it leads to, what sort of script (maybe php script?) am I supposed to put on the page it leads to? :/

        • Moogie, if you want to create your own search then you should use a search.php for example, where you must make some queries to your database to return the results. To find out more, just google for php search tuts.

          Good luck!

    • ah i´ve forgot it. the sintax for the webkit has changed. i used
      background-image: -webkit-linear-gradient(#fff, #d4e8ec); insted of
      background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #d4e8ec),color-stop(1, #fff)); and it worked perfect. of course just whit new browsers jeje.

  6. Go to google and download
    border-radius.htc

    then add this line in the css
    behavior: url(border-radius.htc);

    the roundness will be fixed.. in all.. browsers.. even Internet explorer 5..
    Otherwise.. thank you.

  7. Just an incredible job..
    Your tutorials are really really very nice and easy to follow..

    Your website has turned out to be a very good recourse finding place for new and aspiring web-designers like us..

    Thank you, thankyou very much…

  8. Hi,When I study your demo,I have some problems.In your css3-search-box.html,I found that “html,body{height:100%}”,so I was confuse about the height of 100% ,could you give me a hand?

  9. Thax but, i want to use same technology in ASP.Net where i want to use Asp:button and text box insted html input there i m getting problem, i shall be thankfull to you if you suggest me that how can i use them in Asp.Net

  10. Hello, whenever I change the height, everything is no longer centered nicely.. I am changing the height to 26px, but the actual search input is no longer in the middle. What must i do?
    Thanks.

      • Thanks a lot! By the way, I have set my margin-top property to 0, but it still doesn’t go to the top of the screen, why is that? Thanks.

        Basically, I have tried to set the margin-top property of the form to 0, but it still isn’t at the absolute top. Thanks again!

  11. Red, I have seen some of your tutorials. So i would like to find out how to resolve a bug with my CSS. Most browsers show the nav bar, but Explorer,
    which shows just a piece of it. I was thinking just to get rid of the URL bg image, but I wonder if you can help me.

    background-image: -webkit-gradient(linear,left bottom,left top,color-stop(0, #95d788),color-stop(1, #6cbb6b
    I was wondering if should I try your webkit-gradient. Though what exactly is this?

    thank you!

  12. the page im building, when zooming in and out, it causes the search box to change. how can i keep it unchanging/static when zooming?

  13. Catalin the search box you created here looks really cool !
    And I love to use CSS3 & HTML5 for my website.
    It would be really cool if you make a Google like auto suggest Search Box! I would love to see that !!!!

    Thanks!!

  14. Hi,i’m newbie,i found a little problem in the #searchform of DEMO 1 that i can’t center the inner content without set the text-align attr.

  15. Just came here using google, am in the middle of developing a responsive wp theme, going to try this method right away. bookmarked, Thanks :)

  16. Hi, thanks for the nice coding and beautiful search box! I am having trouble with this line of code though:
    $(document).ready(function() {

    I have it placed here in my document:
    21 $searchform->display();
    22 $(document).ready(function() {
    23 if (!Modernizr.input.placeholder)
    24 { (etc.)
    I get this error message: Parse error: syntax error, unexpected ‘(‘, expecting T_VARIABLE or ‘$’ in D:\Hosting\6516962\html\calendar\searchform.php on line 22.
    Should I be changing document to my document name? I have tried many different adjustments in this code bit, (removing different “(“, but could not solve. Would you be available to help?

  17. How do I make it ‘Search’, It doesn’t really do anything, What am I doing wrong, can you please tell me what to do?

    What do I do to make it search a page for content??

  18. Thanks for the great tutorial! I am caught up on one thing, however.

    Let me forewarn you, I have been out of the web design field for a couple years and I just recently got a job where I am coding again… so I am a bit rusty…

    In terms of the javascript at the bottom of the tut, where is that supposed to go? In the header or in the body? Within the html? I am a bit confused as I have usually worked with only html/css. Thanks in advance for the help!

  19. i manage to create a search box using this code problem is that when i try seraching its not returning any results can anybody help me ?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>