Back
Discover how to create an engaging search bar with a placeholder that animates seamlessly, guiding users intuitively.
<div class="search-bar-container"> <input type="text" class="search-input" placeholder="Search here..."> <button class="search-button"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="#5b5b5b" stroke-width="2"> <path stroke-linecap="round" stroke-linejoin="round" d="M10 18a8 8 0 100-16 8 8 0 000 16z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35" /> </svg> </button> </div>
.search-bar-container { display: flex; align-items: center; border: 1px solid #ccc; border-radius: 25px; padding: 5px 10px; max-width: 300px; transition: box-shadow 0.3s ease; } .search-bar-container:hover { box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); }Here, the container is set to a flexible box display, aligning its child elements center along the y-axis. Padding and border-radius provide a rounded shape, and the hover effect offers slight shadowing to give a lifted appearance.
.search-input { border: none; outline: none; flex: 1; padding: 5px; font-size: 16px; transition: all 0.3s ease; } .search-input::placeholder { color: transparent; transition: color 0.3s ease; } .search-input:focus::placeholder { color: #bbb; }The input field is given a seamless, borderless appearance to maintain focus on the user interaction with content. The flexible property 'flex: 1' allows it to grow and shrink to fit available space uniformly. The real magic is in the placeholder styling, where color transitions create an animated effect upon user focus.
.search-button { background: none; border: none; cursor: pointer; padding: 0; display: flex; justify-content: center; align-items: center; } .search-button svg { height: 20px; width: 20px; }The button is styled cleanly with no background to keep it minimal, focusing user attention on the search interaction. The SVG icon within it acts as an essential visual cue. Flex properties position it desirably within its area, ensuring ease of usability.
<div class="search-bar-container"> <input type="text" class="search-input" placeholder="Search here..."> <button class="search-button"> <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="#5b5b5b" stroke-width="2"> <path stroke-linecap="round" stroke-linejoin="round" d="M10 18a8 8 0 100-16 8 8 0 000 16z" /> <path stroke-linecap="round" stroke-linejoin="round" d="M21 21l-4.35-4.35" /> </svg> </button> </div> <style> .search-bar-container { display: flex; align-items: center; border: 1px solid #ccc; border-radius: 25px; padding: 5px 10px; max-width: 300px; transition: box-shadow 0.3s ease; } .search-bar-container:hover { box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); } .search-input { border: none; outline: none; flex: 1; padding: 5px; font-size: 16px; transition: all 0.3s ease; } .search-input::placeholder { color: transparent; transition: color 0.3s ease; } .search-input:focus::placeholder { color: #bbb; } .search-button { background: none; border: none; cursor: pointer; padding: 0; display: flex; justify-content: center; align-items: center; } .search-button svg { height: 20px; width: 20px; } </style>Thank you for reading this article.
Comments