The Wayback Machine - https://web.archive.org/web/20240118183412/https://www.geeksforgeeks.org/how-to-add-icon-in-form-input-field-in-tailwind-css/
Open In App
Related Articles

How to Add Icon in Form Input Field in Tailwind CSS ?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

User interface components are essential to current web development since they improve the user experience. To give visual clues and enhance the usability of numerous components, including input fields, icons, in particular, are frequently utilized. In this article, we will see how to effectively include Tailwind CSS icons into input elements to improve the visual appeal and usability of your web forms, along with understanding different techniques to implement them.

Approaches

The Tailwind CSS icons can utilized with the given following approaches with the Form Input:

  • Using Inline SVG Icons
  • Using Icon Fonts

We will explore both approaches, along with understanding them with relevant suitable illustrations.

Implementing inline SVG icons to Form input 

In this method, input elements are enhanced with Tailwind CSS using inline SVG icons. Since SVG (Scalable Vector Graphics) icons are easily customizable, scaleable, and styleable using CSS, they are a flexible option for icons. By directly integrating the SVG code into the HTML, we can use Tailwind CSS classes to modify the SVG’s look and behavior.

Syntax:

<svg class="h-5 w-5 text-gray-400"
         fill="none"
         stroke="currentColor">
         <!-- Your SVG icon code here -->
</svg>

Example: This example uses the SVG icons with the Form Input.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0" />
    <link href=
          rel="stylesheet" />
    <title>
          Tailwind CSS Icon in Input
      </title>
</head>
  
<body class="bg-gray-100 
             min-h-screen 
             flex items-center 
             justify-center">
    <div class="relative">
        <input type="text" 
               class="pl-10 pr-4 py-2 border rounded-lg" 
               placeholder="Enter your email" />
        <div class="absolute inset-y-0 left-0 pl-3 
                    flex items-center 
                    pointer-events-none">
            <svg class="h-5 w-5 text-gray-400" 
                 fill="none" 
                 stroke="currentColor">
                <path stroke-linecap="round" 
                      stroke-linejoin="round" 
                      stroke-width="2"
                      d="M12 19l9 2-9-18-9 18 9-2zm0 0v-8">
                  </path>
            </svg>
        </div>
    </div>
</body>
  
</html>


Output:

a

Utilizing Icon Fonts with Form Input

In this method, we make use of icon font libraries, in particular the Font Awesome library, to include icons into input elements using Tailwind CSS. Collections of symbols or glyphs that are used as font characters are known as icon fonts. Including the Font Awesome CSS gives us access to a large number of insertable icons.

Syntax:

<i class="fas fa-envelope text-gray-400"></i>

Example: This example uses the Font-awesome icons with the Form Input.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" 
          content="width=device-width, 
                   initial-scale=1.0" />
    <link href=
          rel="stylesheet" />
    <link href=
          rel="stylesheet" />
    <title>
          Tailwind CSS Icon in Input
      </title>
</head>
  
<body class="bg-gray-100 
             min-h-screen 
             flex items-center 
             justify-center">
    <div class="relative">
        <input type="text"
               class="pl-10 pr-4 py-2 border rounded-lg" 
               placeholder="Enter your email" />
        <div class="absolute inset-y-0 left-0 pl-3 
                    flex items-center 
                    pointer-events-none">
            <i class="fas fa-envelope text-gray-400"></i>
        </div>
    </div>
</body>
  
</html>


Output:
a


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 14 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials