Full width, Responsive Search Box using CSS Tricks
Search boxes are a common need, but creating them can be trickier than expected. The below is a full width responsive search box using a few CSS tricks. Here’s the running example:
demo:
download demo
Here’s a summary:
<style>
/*import font awesome css icon library*/
@import url("https://netdna.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.css");
input,
select,
textarea {
background: none repeat scroll 0 0 #fafafa;
border: 1px solid #eeeeee;
color: #5e5e5e;
display: block;
font-family: arial, sans-serif;
font-size: inherit;
padding: 10px;
width: 100%;
box-sizing: border-box;
font-size: 16px;
margin: 0;
height: 40px;
}
#searchtext {
overflow: hidden;
}
a.search-submit-button {
background: none repeat scroll 0 0 #fafafa;
border-bottom: 1px solid #eeeeee;
border-right: 1px solid #eeeeee;
border-top: 1px solid #eeeeee;
color: #5e5e5e !important;
display: block;
float: right;
font-family: inherit;
font-size: 20px;
padding: 8px 10px;
text-align: center;
width: 45px;
box-sizing: border-box;
height: 40px;
}
#form-container {
/* width: 60%; */
}
</style>
<div>
<div id="form-container">
<!--<input type="submit" id="searchsubmit" value="" />-->
<div id="searchtext"><input id="s" name="s" type="text" value="Search Something..." /></div>
</div>
</div>
A little explanation…
- both elements have box-sizing: border-box (Without this there may be alignment issues)
- input has margin: 0 (To remove browser default margin on inputs)
- both have a height assigned (Without this some browsers will align them differently)
- the anchor is placed before the "searchtext" div and a float: right is applied to the anchor to make the arrangement possible and still allow the input to stretch 100% to it's container
- Font Awesome used as a search icon instead of images for flexibility