How we can create a flat search input box using CSS3? Solution: See this Flat Search Box With Loading Animation Using CSS and JavaScript.
Previously I have shared some search box programs, but this box has a flat design with a preloader or loading type animation. Basically, we use HTML <input> tag to creating the input field and <input type="submit"> or <submit> or <button> tag for creating the search button or icon. And by default, these tags have no style they are in plain HTML text and layout form. But we can give style and functions using CSS and JavaScript.
Today you will learn to create a search input box with animation. Basically, there is a search box and a submit button, I have placed an input for creating the search box, a button with an icon for submitting, and another icon for loading animation. Also, I have placed a note text for giving info on how the animation will visible. When you will focus or click on the input the color will little dark, and when you will click on the search button the loading animation will appear.
So, Today I am sharing Flat Search Box With Loading Animation Using CSS and JavaScript. There I have used CSS for complete style and used JS for loading animation values declare. This program has a flat and minimal design, that making this program beautiful. You can use this program on your website after backend integration.
If you are thinking now how this search input box actually is, then see the preview given below.
Preview Of CSS JavaScript Search Input
See this video preview to getting an idea of how this search input design looks like.
Now you can see this visually, also you can see it live by pressing the button given above. If you like this, then get the source code of its.
You May Also Like:
Flat Search Box With Loading Animation Source Code
Before sharing source code, let’s talk about it. First I have created a div with ID name “search” inside that I have placed an input for the search box, a button for search button, and other div for place the loading animation. Now I have created another div to leave a text note. Also in the HTML file, I have linked other files like CSS and JS.
Now using CSS I have placed all the items in the right place, as you can see in the preview. With CSS first I gave basic values to the elements like size, position, margin, padding, etc. Also using CSS, I have created the animation effect, focus effect, and other elements styles. There I have used the font-awesome library to place the search icon and loading icon. But used the @keyframe command to create the loading animation.
JavaScript handling here loading animation run on the action and run-time functions in this program. I have fetched elements using document.getElementById command and stored in variables. After that, I created a function for adding and removing the CSS class name to animate. Also, there is a keycode command to detect the enter key and start the animation.
Left all other things you will understand after getting the codes, I can’t explain all in writing. For creating this program you have to create 3 files. First file for HTML, second file for CSS, and the third file for JavaScript. Follow the steps to creating the program without any error.
index.html
Create an HTML file named ‘index.html‘ and put these codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <!-- Code By Webdevtrick ( https://webdevtrick.com ) --> <html lang="en" > <head> <meta charset="UTF-8"> <title>Flat Search Box | Webdevtrick.com</title> <link href='https://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'> <link rel='stylesheet' href='https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css'> <link rel="stylesheet" href="style.css"> </head> <body> <div id="search"> <input id="input" placeholder="Search..."/> <button id="button"><i class="fa fa-search"></i></button> <div class="spinner"><i class="fa fa-spinner"></i></div> </div> <div class="note">Click the button or hit enter.</div> <script src="function.js"></script> </body> </html> |
style.css
Now create a CSS File named ‘style.css‘ and put these codes given here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
/* Code By Webdevtrick ( https://webdevtrick.com ) */ * { box-sizing: border-box; margin: 0; padding: 0; -webkit-font-smoothing: antialiased; } body { -webkit-box-align: center; align-items: center; background: #fd6f6f; color: #fff; display: -webkit-box; display: flex; font: 30px/1.375 "Lato", arial, sans-serif; -webkit-box-orient: vertical; -webkit-box-direction: normal; flex-flow: column nowrap; height: 100vh; -webkit-box-pack: center; justify-content: center; width: 100vw; } .note { font-size: 0.375em; font-weight: bold; text-transform: uppercase; color: #fff; letter-spacing: 1px; } #search { -webkit-box-align: center; align-items: center; background: #ff4242; border-radius: 10px; display: -webkit-box; display: flex; -webkit-box-pack: justify; justify-content: space-between; margin: 0.5em 0; padding: 0.5em 0.5em 0.5em 1em; -webkit-transition: all 0.5s; transition: all 0.5s; width: 500px; } #search:hover, #search:focus { background: #ff3030; } #search button, #search input { -webkit-appearance: none; -moz-appearance: none; appearance: none; background: transparent; border: 0; color: inherit; font: inherit; outline: 0; } #search button { cursor: pointer; padding: 0 0.25em; } #search input { -webkit-box-flex: 1; flex: 1; } #search input::-webkit-input-placeholder { color: #fff; } #search input::-moz-placeholder { color: #fff; } #search input:-ms-input-placeholder { color: #fff; } #search input::-ms-input-placeholder { color: #fff; } #search input::placeholder { color: #fff; } #search .spinner { -webkit-animation: spinner 1s infinite linear; animation: spinner 1s infinite linear; display: none; padding: 0 0.25em; } #search.loading button { display: none; } #search.loading .spinner { display: block; } @-webkit-keyframes spinner { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); } } @keyframes spinner { 0% { -webkit-transform: rotate(0deg); transform: rotate(0deg); } 100% { -webkit-transform: rotate(360deg); transform: rotate(360deg); } } |
function.js
The final step, create a JavaScript file named ‘function.js‘ and put the codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Code By Webdevtrick ( https://webdevtrick.com ) var search = document.getElementById('search'); var button = document.getElementById('button'); var input = document.getElementById('input'); function loading() { search.classList.add('loading'); setTimeout(function() { search.classList.remove('loading'); }, 1500); } button.addEventListener('click', loading); input.addEventListener('keydown', function() { if(event.keyCode == 13) loading(); }); |
That’s It. Now you have successfully created Flat Search Box With Loading Animation Using CSS and JavaScript. If you have any doubt or questions comment down below.
Thanks For Visiting, Keep Visiting.