Search code examples
javascriptjquerycsssearchbar

Search Bar Visible In Desktop And Expandable In Mobile Devices


Here is my code.

<style>
#searchformfix {
    width: 50%;
    border-right: 1px solid #E5E5E5;
    border-left: 1px solid #E5E5E5;
    position: relative;
    background: #fff;
    height: 40px;
    display: inline-block;
    border: 1px solid #219AEB;
    border-radius: 5px;
    margin-top: 5px;
}
#searchform {
    margin: 0px 0 0;
    padding: 0;
}
#searchform input[type="text"] {
    background: #fff;
    border: none;
    float: left;
    height: 100%;
    transition: all 600ms cubic-bezier(0.215,0.61,0.355,1) 0s;
    -moz-transition: all 300ms cubic-bezier(0.215,0.61,0.355,1) 0s;
    -webkit-transition: all 600ms cubic-bezier(0.215,0.61,0.355,1) 0s;
    -o-transition: all 600ms cubic-bezier(0.215,0.61,0.355,1) 0s;
    color: #929292;
    margin-left: 10px;
    margin-top: 10px;
    width: 80%;
}
button.fa.fa-search {
    background: #FFF;
    margin-top: 5px;
    font-size: 24px;
    float: right;
    cursor: pointer;
}
</style>
<div id='searchformfix'>
<form action='/search' id='searchform'>
<input name='q' onblur='if (this.value == &quot;&quot;) {this.value = &quot;Search Videos...&quot;;}' onfocus='if (this.value == &quot;Search Videos...&quot;) {this.value = &quot;&quot;;}' type='text' value='Search Videos...'/></form>
<button class='fa fa-search' type='submit' value='Go'></button>
</div>

JsFiddle Demo: http://jsfiddle.net/jaribhai/wncwqerj/4/

So this is a working searchbar. What I want to do is that it remains visible in desktop devices and become expandable in mobile devices like this one.

http://callmenick.com/_development/expanding-search-bar/

I think this isn't possible without the use of Jquery so someone tell me how can I get this functional. Thanks


Solution

  • One way could be to use a stylized checkbox to control the state of the search input.

    Stylize a checkbox with an associated label to show the search icon. Use media query and show or hide the search input based on the :checked state of the checkbox.

    Demo Fiddle: https://jsfiddle.net/abhitalks/ott3Lt78/

    Demo Snippet:

    * { box-sizing: border-box; padding: 0; margin: 0; }
    .srch { margin: 8px; }
    .srch > label {
      display: inline-block; text-align: center;
      width: 32px; height: 32px; line-height: 32px;
      background-color: #33d; color: #eee;
      cursor: pointer; vertical-align: middle;
    }
    .srch > label+input { display: none; }
    .srch > input[type=text] {
      display: inline-block; height: 32px; width: 240px; padding: 4px;
      border: 1px solid #33d; transition: width 0.5s;
    }
    
    @media screen and (max-width: 700px) {
      .srch > input[type=text] { 
        width: 0; border-width: 0px; padding: 0px;
      }
      .srch > input[type=checkbox]:checked + input { 
        width: 240px; border-width: 1px; padding: 4px;
      }
    }
    <div class="srch">
      <label for='chk'>&#128270;</label>
      <input id='chk' type='checkbox' />
      <input type="text" placeholder="search videos" />
    </div>