If you're a developer like me, you'd know that search forms can be boring sometimes. Today, I'll show you how to spice it up a bit, make it cool and save some space while doing it. Let's get started!
Expanding Search Form Wordpress Markup
First, you'll need to call (if you haven't already) the search form in your wordpress theme. You can do so by adding this code where you want it to show.
-- php get_search_form(); --?>
This will call the template file called searchform.php which should have something like this inside:
We'll be using that code markup for our tutorial. Now, let get started with styling the form. I've used a combination of dark #333333 and light #e14d43 colors, but you can choose any combination you want since it's all CSS. the only image we're using is the search icon, which if you want, you can remove and add text instead.
Expanding Search Form HTML Markup
It's pretty much the same as above, we simply remove the PHP code and replace it with a plain url.
Expanding Search Form CSS3
I've added a little comment next to the stuff that may require explaining for beginners.
This one is for our visible button, the dark one. Here, you can change the background color #333, as well as the icon.
.toggle {
background: url("img/s-icon.png") no-repeat scroll center center #333333;
border: medium none;
color: #FFFFFF;
cursor: pointer;
display: block;
height: 40px;
position: absolute;
right: 0;
width: 50px;
}
This is the style for the search form, it's currently hidden.
#searchform {
float: right;
height: 40px;
margin: 0;
opacity: 0;
padding: 0;
visibility: hidden; /** Hide our form, for now. **/
width: 10px; /** The current width is set to 10px, when we'll hover on the form, it'll expand to 250px. **/
-webkit-transition: all 0.2s ease-in-out; /** Transitions: increase the 0.2s to slow (1s for example) the fading/sliding effect. **/;
-moz-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
}
This is where the expanding happens. When you hover on the form, the styles here will over the ones above. Therefore, expanding it from 10px to 250px and from a hidden state, to visible.
.form:hover #searchform {
display: block;
opacity: 1;
visibility: visible; /** Show our form. **/
width: 250px; /** This is the new of the form, expanding from 10px. **/
-webkit-transition: all 0.2s ease-in-out; /** Transitions: increase the 0.2s to slow (1s for example) the fading/sliding effect. **/;
-moz-transition: all 0.2s ease-in-out;
-o-transition: all 0.2s ease-in-out;
}
These 2 last classes are for the input field, where you type your search query. And the submit button, where you click to submit your search. Same deal as before, you can change the icon, background here.
#searchform #s {
background: none repeat scroll 0 0 #E14D43;
border: medium none;
color: #FFFFFF;
float: right;
height: 100%;
padding: 11px;
width: 100%;
}
#searchform #searchsubmit {
background: url("img/s-icon.png") no-repeat scroll center center #c6473f;
border: medium none;
color: #FFFFFF;
cursor: pointer;
display: block;
height: 40px;
position: absolute;
right: 0;
width: 50px;
}
Check out the live demo, download the source files if need and let me know how it goes. If you run into any difficulties, drop me a comment and I'll help you out.
Live Demo & Download Source can be found on wpfreebies.net
Original Article link: wpfreebies.net/how-to-create-an-expanding-search-form-for-wordpress/
previous post
next post