This CSS & JS code snippet helps you to create a stylish search button animation. The search button consists of a circular element and a dash. When clicked, it smoothly transforms, revealing a search bar. The animation is achieved through CSS transitions, and the JavaScript function toggles between the closed and opened states.
You can use this code on your website’s search bar for a modern and eye-catching animation.
How to Create Search Button Animation in CSS & JS
1. First of all, load the Normalize CSS by adding the following CDN link into the head tag of your HTML document.
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
2. Place the given HTML code within the <body>
of your document. This includes a container div with a circular element and a hidden search bar.
<div class="container"> <div class="closed" id="circle"> <div class="cross"></div> </div> <input class="closed" id="dash"/> </div>
3. Copy and paste the following code into your stylesheet or within a <style>
tag. This CSS handles the button’s appearance and animation effects.
* { border: 0; margin: 0; padding: 0; border-radius: 0; box-sizing: border-box !important; } html, body { background: #889; height: 100vh; width: 100%; overflow: hidden; } html .container, body .container { position: relative; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 300px; height: 300px; } html .container #circle, body .container #circle { position: relative; top: 50%; left: 50%; transform: translate(-50%, -50%); width: 50px; height: 50px; border: solid 4px #fff; background: rgba(255, 255, 255, 0); border-radius: 50%; cursor: pointer; transition: all ease 0.3s; } html .container #circle .cross, body .container #circle .cross { display: block; position: absolute; top: 50%; right: 0; z-index: 20; width: 30px; height: 30px; cursor: pointer; transform: translateY(-50%); } html .container #circle .cross:before, html .container #circle .cross:after, body .container #circle .cross:before, body .container #circle .cross:after { content: ""; position: absolute; display: block; right: 7px; height: 4px; width: 4px; border-radius: 1px; transition: all 0.25s ease; } html .container #circle .cross:before, body .container #circle .cross:before { top: 0px; background-color: #889; transform: rotate(-45deg); transform-origin: top right; transition-delay: 0.1s; } html .container #circle .cross:after, body .container #circle .cross:after { bottom: 0px; background-color: #889; transform: rotate(45deg); transform-origin: bottom right; } html .container #circle.opened, body .container #circle.opened { background: white; transform: translate(-250%, -50%); } html .container #circle.opened .cross:before, html .container #circle.opened .cross:after, body .container #circle.opened .cross:before, body .container #circle.opened .cross:after { width: 25px; right: 13px; } html .container #circle.opened .cross:before, body .container #circle.opened .cross:before { top: 5px; transition-delay: 0.25s; } html .container #circle.opened .cross:after, body .container #circle.opened .cross:after { bottom: 5px; transition-delay: 0.3s; } html .container #dash, body .container #dash { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); height: 0px; border-bottom: solid 4px #fff; border-radius: 5px; cursor: pointer; padding-left: 5px; transform-origin: left center; transition: all ease 0.3s; outline: rgba(255, 255, 255, 0); } html .container #dash.closed, body .container #dash.closed { width: 25px; transform: translate(1em, 1em) rotate(45deg); } html .container #dash.opened, body .container #dash.opened { width: 200px; height: 45px; border-radius: 0; transform: translate(-4.2em, -1.4em) rotate(0deg); }
4. Finally, insert the following JavaScript code just before the closing </body>
tag. This script enables the search button animation by toggling between the closed and opened states.
const $ = document.querySelector.bind(document); const searchItems = [$('#circle'), $('#dash')]; const search = () => searchItems.forEach(x => x.className === 'closed' ? x.className = 'opened' : x.className = 'closed'); searchItems[0].addEventListener('click', search);
That’s all! hopefully, you have successfully created a search button animation on your website. If you have any questions or suggestions, feel free to comment below.
Similar Code Snippets:
I code and create web elements for amazing people around the world. I like work with new people. New people new Experiences.
I truly enjoy what I’m doing, which makes me more passionate about web development and coding. I am always ready to do challenging tasks whether it is about creating a custom CMS from scratch or customizing an existing system.