DEV Community

Cover image for How to build a search bar component with Tailwind CSS and Flowbite
Zoltán Szőgyényi for Themesberg

Posted on • Originally published at flowbite.com

How to build a search bar component with Tailwind CSS and Flowbite

One of my favourite front-end stacks to work with when building website pages is Tailwind CSS and Flowbite because of the utility-first approach and the large collection of interactive components from Flowbite.

Flowbite is the most popular component library built based on Tailwind CSS including interactive elements such as navbar, modals, datepickers, buttons, and more.

Tailwind CSS is the most popular utility-first CSS framework that helps you develop websites much easier without having to leave your HTML.

Today I want to show you how you can build a pretty common component called a search bar that is often found on websites such as blogs, listing directories, applications, and more.

It can be used to allow the users to search pages and content based on a word or multiple words that will later be used in a database query.

Here's how it will look in the end:

Tailwind CSS Search Bar

Without further ado, let's get started!

Tailwind CSS Search Bar

First of all we need to make sure that the HTML semantics are correct so we will use a form tag and an input tag for starters:

<form>   
    <label for="voice-search" class="sr-only">Search</label>
    <input type="text" id="voice-search" required>
    <button type="submit">Search</button>
</form>
Enter fullscreen mode Exit fullscreen mode

There is not too much going on and it looks pretty simple, but we will soon style the component and also add some more elements.

Notice that we also added a label tag but we also hide it. This helps screen readers understand what the input stands for.

Now let's wrap input component into a div and add an icon inside with some Tailwind CSS classes to show a search icon at the start of the input:

<form class="flex items-center">   
    <label for="voice-search" class="sr-only">Search</label>
    <div class="relative w-full">
        <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
            <svg class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg>
        </div>
        <input type="text" id="voice-search" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5" placeholder="Search Mockups, Logos, Design Templates..." required>
    </div>
    <button type="submit">Search</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Looking much better! Now let's also style the search button and add an icon inside it:

<form class="flex items-center">   
    <label for="voice-search" class="sr-only">Search</label>
    <div class="relative w-full">
        <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
            <svg class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg>
        </div>
        <input type="text" id="voice-search" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5" placeholder="Search Mockups, Logos, Design Templates..." required>
    </div>
    <button type="submit" class="inline-flex items-center py-2.5 px-3 ml-2 text-sm font-medium text-white bg-blue-700 rounded-lg border border-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"><svg class="mr-2 -ml-1 w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>Search</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Maybe you have noticed but we also added a voice icon that you may use to active voice search. Add the following code:

<form class="flex items-center">   
    <label for="voice-search" class="sr-only">Search</label>
    <div class="relative w-full">
        <div class="flex absolute inset-y-0 left-0 items-center pl-3 pointer-events-none">
            <svg class="w-5 h-5 text-gray-500 dark:text-gray-400" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M8 4a4 4 0 100 8 4 4 0 000-8zM2 8a6 6 0 1110.89 3.476l4.817 4.817a1 1 0 01-1.414 1.414l-4.816-4.816A6 6 0 012 8z" clip-rule="evenodd"></path></svg>
        </div>
        <input type="text" id="voice-search" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full pl-10 p-2.5  dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" placeholder="Search Mockups, Logos, Design Templates..." required>
        <button type="button" class="flex absolute inset-y-0 right-0 items-center pr-3">
            <svg class="w-4 h-4 text-gray-500 dark:text-gray-400 hover:text-gray-900 dark:hover:text-white" fill="currentColor" viewBox="0 0 20 20" xmlns="http://www.w3.org/2000/svg"><path fill-rule="evenodd" d="M7 4a3 3 0 016 0v4a3 3 0 11-6 0V4zm4 10.93A7.001 7.001 0 0017 8a1 1 0 10-2 0A5 5 0 015 8a1 1 0 00-2 0 7.001 7.001 0 006 6.93V17H6a1 1 0 100 2h8a1 1 0 100-2h-3v-2.07z" clip-rule="evenodd"></path></svg>
        </button>
    </div>
    <button type="submit" class="inline-flex items-center py-2.5 px-3 ml-2 text-sm font-medium text-white bg-blue-700 rounded-lg border border-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800"><svg class="mr-2 -ml-1 w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"></path></svg>Search</button>
</form>
Enter fullscreen mode Exit fullscreen mode

Notice that we also added the dark variant classes. Here's how the search bar looks in dark mode:

Tailwind CSS Search Bar Dark Mode

Awesome, that's about it! You can now use this component with any front-end and back-end technology like React, Vue, Laravel as it is a simple input.

This Tailwind CSS search bar component is taken as an example from the Flowbite library and you can check out the whole collection of search bar variants on the official documentation:

Tailwind CSS Search Bar - Flowbite

Tailwind CSS Search Bar Dropdown - Flowbite

Tailwind CSS Simple Search - Flowbite

Tailwind CSS Location Search - Flowbite

Tailwind CSS Voice Search - Flowbite

Useful links:

Top comments (0)