Re-create Google's Search Input Field and Submit Button

Introduction

Most likely, you have used Google to search something so you're probably familiar with their search form. In this tutorial, I'll show you how to re-create it with some lightweight HTML, CSS and JQuery combination. Google has an ingenious way of adding subtle usability enhancements. In their search input field, you may or may not have noticed that the "X" delete button appears as you type something and disappears if the field is empty. With a few lines of JQuery, you can implement the same effect. I'll also show you how to re-create Google's new search button with some simple CSS settings. We'll put it all together and you'll have a minimal Google search form clone that you can use on your own website.

Demo Download

Google Search Clone

HTML

We'll need to mark-up the input text field, the "X" text delete button and the submit button. We combine all three of the elements and place them inside a div container.

<div id="searchContainer">
	<form>
		<input id="field" name="field" type="text" />
		<div id="delete"><span id="x">x</span></div>
		<input id="submit" name="submit" type="submit" value="Search" />
	</form>
</div>

CSS

Now, let's clone the look of Google's search form.

/* div container containing the form  */
#searchContainer {
	margin:20px;
}

/* Style the search input field. */
#field {
	float:left; 
	width:300px; 
	height:27px; 
	line-height:27px;
	text-indent:10px; 
	font-family:arial, sans-serif; 
	font-size:1em; 
	color:#333; 
	background: #fff; 
	border:solid 1px #d9d9d9; 
	border-top:solid 1px #c0c0c0; 
	border-right:none;
}

/* Style the "X" text button next to the search input field */
#delete {
	float:left; 
	width:16px;
	height:29px; 
	line-height:27px; 
	margin-right:15px; 
	padding:0 10px 0 10px;
	font-family: "Lucida Sans", "Lucida Sans Unicode",sans-serif;
	font-size:22px; 
	background: #fff;  
	border:solid 1px #d9d9d9; 
	border-top:solid 1px #c0c0c0; 
	border-left:none;
}
/* Set default state of "X" and hide it */
#delete #x {
	color:#A1B9ED; 
	cursor:pointer;
	display:none;
}
/* Set the hover state of "X" */
#delete #x:hover {
	color:#36c;
}
/* Syle the search button. Settings of line-height, font-size, text-indent used to hide submit value in IE */
#submit {
	cursor:pointer; 
	width:70px; 
	height: 31px; 
	line-height:0; 
	font-size:0; 
	text-indent:-999px;
	color: transparent;  
	background: url(ico-search.png) no-repeat #4d90fe center; 
	border: 1px solid #3079ED; 
	-moz-border-radius: 2px; 
	-webkit-border-radius: 2px; 
}
/* Style the search button hover state */
#submit:hover {
	background: url(ico-search.png) no-repeat center #357AE8; 
	border: 1px solid #2F5BB7;
}
/* Clear floats */
.fclear {clear:both}

Javascript

Here we have two parts. First, we'll need to emulate the appearance and disappearance of the "X" button. On the event of "keyup" on the search input field, make the "X" button appear or fade in. Also check if the search input field is empty on "keyup". If it is, fade the "X" button out. On the second part, we'll need to have an on click event when the user clicks on the "X" button. When the "X" button is clicked, we'll have to clear any text in the search input field and also hide the "X" button.

$(document).ready(function() {
	// if text input field value is not empty show the "X" button
	$("#field").keyup(function() {
		$("#x").fadeIn();
		if ($.trim($("#field").val()) == "") {
			$("#x").fadeOut();
		}
	});
	// on click of "X", delete input field value and hide "X"
	$("#x").click(function() {
		$("#field").val("");
		$(this).hide();
	});
});

Conclusion

Now, you have created a minimalistic search form that looks just like Google's search form but with a lot less code. Also, customization is easy if you want to use it on your own website or your client's website. Just change the background and border colors of the search button to match the color scheme of the website. Well, I hope you found the tutorial useful and I look forward to writing more simple and short tutorials on Queness.

About the Author

Syung Hong is an interactive web designer with a decade experience in his field. He is the owner of divology.com, a web portal for design resources, inspirations and entertainment.

Show Some Love, Spread This Post!

10 comments

Pera Sat, 21st April 2012 Nice I will use it many pages.... great.
Reply
Tariq Suhail Tue, 17th April 2012 Tnx alot
Reply
zemgo Sat, 14th April 2012 how to put that in the middle?
Reply
widyana Sat, 21st January 2012 it is cools :)
Reply
Amrendra Tue, 20th March 2012 I want to open text feilds and submit button in under reply option how can write a code for this ? please suggest me as soon as posible sir.
thank's
kubakoumak Sun, 30th October 2011 Here's missing the active phase of a button. But thanks - it's nice.
Reply
dimas Wed, 31st August 2011 i wasn't looking for this, but it fall to me like ring on my finger

thanks
Reply
eGord0n Wed, 31st August 2011 Thank you very much!
Reply
yo Wed, 17th August 2011 This is exactly what I was looking for, thanks a lot for sharing:)
Reply
hamid Wed, 27th July 2011 nice
tnx
Reply

Leave a comment

Have something to say? Drop a comment! No HTML tags are allowed in the comment textfield.

Advertisement