Unique Search Bar - MeggiTools
Run
Toggle Theme
Share Link
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Unique Search Bar</title> <style> body { display: flex; justify-content: center; align-items: center; height: 100vh; background: linear-gradient(135deg, #6D5BBA, #8D58BF); font-family: 'Arial', sans-serif; } .search-container { position: relative; width: 400px; } .search-input { width: 100%; padding: 15px 20px; border: none; border-radius: 25px; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2); outline: none; transition: all 0.3s ease; font-size: 16px; padding-right: 50px; } .search-input:focus { box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4); } .search-button { position: absolute; right: -66px; top: 50%; transform: translateY(-50%); background: #FF5722; border: none; padding: 10px; border-radius: 25px; cursor: pointer; transition: all 0.3s ease; } .search-button:hover { background: #E64A19; } .search-icon { color: white; font-size: 20px; } .suggestions { position: absolute; top: 49px; left: 5%; width: 430px; background: white; box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2); max-height: 200px; overflow-y: auto; z-index: 1000; display: none; } .suggestion-item { padding: 10px; border-bottom: 1px solid #f1f1f1; cursor: pointer; } .suggestion-item:hover { background: #f1f1f1; } .suggestion-item:last-child { border-bottom: none; } </style> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> </head> <body> <div class="search-container"> <input type="text" class="search-input" id="searchInput" placeholder="Search..."> <button type="button" class="search-button" title="Search"><i class="fas fa-search search-icon"></i></button> <div class="suggestions" id="suggestions"></div> </div> <script> const searchInput = document.getElementById('searchInput'); const suggestionsBox = document.getElementById('suggestions'); const sampleSuggestions = [ 'Apple', 'Banana', 'Cherry', 'Date', 'Elderberry', 'Fig', 'Grape', 'Honeydew' ]; searchInput.addEventListener('input', function() { const value = this.value.toLowerCase(); suggestionsBox.innerHTML = ''; if (value) { const filteredSuggestions = sampleSuggestions.filter(item => item.toLowerCase().includes(value) ); if (filteredSuggestions.length > 0) { filteredSuggestions.forEach(item => { const div = document.createElement('div'); div.classList.add('suggestion-item'); div.textContent = item; div.addEventListener('click', function() { searchInput.value = this.textContent; suggestionsBox.innerHTML = ''; suggestionsBox.style.display = 'none'; }); suggestionsBox.appendChild(div); }); suggestionsBox.style.display = 'block'; } else { suggestionsBox.style.display = 'none'; } } else { suggestionsBox.style.display = 'none'; } }); </script> </body> </html>