Previously I wrote a tutorial on mobile navigation, today I'm going to share a CSS tutorial on how to make an expandable search form that is suitable for mobile and responsive designs. This trick uses native CSS properties — no Javascript or extra markups required. It is a simple and effective way to design a compact search form.
The Purpose
On mobile display, every pixel counts. To minimize the space required to display the search form, it is displayed in a compact form and then expand to full width on :focus. This way more space can be used for other interface elements or content area. Check out the search form on Web Designer Wall and Best Web Gallery for a live demo. When you click on the input field, it expands to full width.
On Best Web Gallery, I use jQuery to fade in the search form when the search button is clicked. I use focus() function to set the input to :focus stage so the user doesn't have to click again.
Let's Start: The HTML Code
Below is the sample HTML form. It uses HTML5 search input tag.
<form>
<input type="search" placeholder="Search">
</form>
Reseting The Default Webkit Search Input
By default, Webkit browsers style the search input like the screenshot shown below:
To remove the default style so it looks like a regular text field, add the following CSS:
input[type=search] {
-webkit-appearance: textfield;
-webkit-box-sizing: content-box;
font-family: inherit;
font-size: 100%;
}
input::-webkit-search-decoration,
input::-webkit-search-cancel-button {
display: none;
}
Styling The Search Input (view demo)
I'm not going to go through each CSS line because it is straight forward. Note that the search input width is set at 55px and it will expand to 130px on :focus stage. The transition property is the trick in making it animated. Box-shadow is used to make the glowing effect on the input.
If you need help on the border-radius or box-shadow CSS3 properties, read the "The Basics of CSS3" article.
Example B (view demo)
In Demo B, the search input is further minimized - only the search icon is shown without the placeholder text. Note that I changed the padding and width property on the search input to display a perfect rounded button. I used color:transparent to make the text invisible.
Browser Compitability
It works on all major browsers such as Chrome, Firefox, Safari, and IE8+. It doesn't work on IE7 or older because IE does not recognize search input and :focus pseudo-class is not supported.
thanks alot, i was looking for it for 2 days :)
Nice tut but I’m more of a fan of having UI elements being set. Besides, I don’t think you save that much space that you can actually use for other elements.
Doesn’t work on IE7. Any idea on what percentage of users are on IE7? Does it make sense to just ignore those visitors?
Thanks for the demo! I was looking for something like it :)
Very useful, I am working to set up this search feature in our new WordPress theme which is responsive design. This help me a lot.
Nice blog and nice post, The topic here i found is really effective.
Thanks for posting this. There are always little things to think about when designing for mobile platforms. This helps.
This works excellent for having different styled search bars based on screen width, I def advise to use the CSS above to remove the default style.. I was not able to get it working without it.
The great thing about these expandable search bars is not just that they save space but they also act as great light weight feature additions to small or large sites that add that extra feel of quality. Some of the sites that we’ve built on http://www.reynoldsdigital.com use expandable search bars so if you want to see some examples check it out.
Thank you very much. very useful
Nice! Very useful :)
Thanks for the demo! I was looking for something like it :)
Thank you very much. very usefu
Thanks for posting this. There are always little things to think about when designing for mobile platforms. This helps.
Great…Thanks you
new one is good!
This is what i was looking for a long time, Thanks for a great demo.
Good one, surly going to use this for my next website :)
Thanks.
Some posts really matters because they are valueable, I have found your post very valueable.
That was exciting. I want to have some one for expanding mobile search form.
Nice! Question: how to make it expand leftward and not to the right? Thanks!
Figured it out, with an assist from Paul O’Brien. Essentially it just involves floating it to the right. Too simple for my sludgy brain. :)