How to Create a Sleek Search Bar Using HTML and Tailwind CSS
In this article, we will guide you through the process of creating a simple search bar using HTML and Tailwind CSS. We will use a basic HTML structure and style it with Tailwind CSS to create a visually appealing search bar.
HTML Structure
The HTML structure for our search bar is straightforward. We will create a div
element with a class of flex
to center our search bar horizontally and vertically. Inside this div
, we will create another div
with a class of relative
to position our search input and button.
<div class="flex justify-center items-center h-screen">
<div class="relative w-full max-w-xl">
<!-- Search input and button will go here -->
</div>
</div>
Search Input and Button
Now, let’s add our search input and button inside the relative
div
. We will use an input
element with a type of text
for our search input, and a button
element with an absolute position to place it on top of the search input.
<input type="text"
class="w-full p-4 border border-gray-100 rounded-full text-lg shadow focus:outline-none focus:ring-2 focus:ring-green-500"
placeholder="Search or type a url"
>
<button class="absolute right-2 top-1/2 transform -translate-y-1/2 bg-green-500 text-white p-2 px-4 rounded-full focus:outline-none">
<i class="fas fa-search"></i>
</button>
Video Tutorial:
Styling with Tailwind CSS
We will use Tailwind CSS to style our search bar. We have already added the necessary classes to our HTML elements. Here’s a breakdown of the classes we used:
flex
: Centers the content horizontally and vertically.justify-center
: Centers the content horizontally.items-center
: Centers the content vertically.h-screen
: Sets the height of the element to the full height of the screen.relative
: Positions the element relative to its normal position.w-full
: Sets the width of the element to 100%.max-w-xl
: Sets the maximum width of the element to xl.p-4
: Adds padding to the element.border
: Adds a border to the element.border-gray-100
: Sets the border color to gray-100.rounded-full
: Rounds the corners of the element.text-lg
: Sets the text size to large.shadow
: Adds a shadow to the element.focus:outline-none
: Removes the outline on focus.focus:ring-2
: Adds a ring on focus.focus:ring-green-500
: Sets the ring color to green-500.absolute
: Positions the element absolutely.right-2
: Sets the right position of the element to 2.top-1/2
: Sets the top position of the element to 50%.transform
: Transforms the element.-translate-y-1/2
: Translates the element vertically by 50%.bg-green-500
: Sets the background color to green-500.text-white
: Sets the text color to white.p-2
: Adds padding to the element.px-4
: Adds horizontal padding to the element.rounded-full
: Rounds the corners of the element.
Conclusion
In this article, we created a simple search bar using HTML and Tailwind CSS. We used a basic HTML structure and styled it with Tailwind CSS to create a visually appealing search bar. You can customize the search bar to fit your needs by modifying the HTML and CSS code.