In this tutorial, we will be creating a Pure CSS and Vanilla javascript Expandable search bar from scratch with a cool animation effect There you go you have created a nice and elegant expandable search bar with minimal code and some basic Vanilla Javascript code without any frameworks or third-party libraries.
There are many code snippets available online or on many other blogs and websites, but everyone cannot optimize your blog or website, so you need some optimized code snippets. So now checkout out the code snippet for your blog and website that will give you all features for your desired code. Now grab the ready-to-use code and paste it where you want.
Table of Contents
Features:
- Light Weight.
- Pure JavaScript.
- Cross Browser.
- No JQuery Files.
- Fully Customizable.
- Responsive.
How To Add Simple Hidden Search Widget With Toggle Using Pure Vanilla JavaScript?
There are a few easy and understandable steps to achieve your desired functionality that we are gonna share below. Follow each step perfectly.
CSS:
<style type="text/css"> * { margin: 0; padding: 0; box-sizing: border-box; } body { display: flex; align-items: center; justify-content: center; height: 100vh; background-image: linear-gradient(90deg, #00d2ff,#3a7bd5 ) } .search { position: relative; height: 50px; } .search .input { border-radius: 5px 5px 5px 5px; background-color: white; border: 0; font-size: 18px; padding: 15px; height: 50px; width: 50px; transition: width 0.3s ease-in; } .btn { border-radius: 5px; font-size: 24px; background-color: white; border:none; position: absolute; left: 0; top: 0; cursor: pointer; height: 50px; width: 50px; transition: transform 0.3s ease-in; } .btn:focus, .input:focus { outline:none; } .search.active .input { width: 200px; } .search.active .btn { transform: translateX(190px) } </style>
HTML:
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.2/css/all.min.css" integrity="sha512-HK5fgLBL+xu6dm/Ii3z4xhlSUyZgTT9tuc/hSrtw6uzJOvgRr2a9jyxxT1ely+B+xFAmJKVSTbpM/CuL7qxO8w==" crossorigin="anonymous" /> <div class="search"> <input type="text" class="input" placeholder='search'> <button class="btn"><i class="fas fa-search"></i></button> </div>
JavaScript:
<script type="text/javascript"> const search = document.querySelector('.search'); const btn = document.querySelector('.btn'); const input = document.querySelector('.input'); btn.addEventListener('click', e => { search.classList.toggle('active') input.focus(); }) </script>
Customization:
No need to customize it. Just copy-paste. Rest edit the code as per comments and need.
Troubleshooting the Errors:
Do it with concentration and patience. Check your all steps again and all codes or scripts. If you find any error you can contact us anytime via comment or better via email, We are always here to help you.
Final Words:
That’s all we have. We hope that you liked this article. If you have any problem with this code in your template then feel free to contact us with a full explanation of your problem. We will reply to you as time allows us If you have any doubts or problems please comment below. We are happy to help you! If you liked this article, Don’t forget to share this with your friends so they can also take benefit from it and leave.
Be the first to write a comment.