Form inputs are a crucial aspect of web development, and when it comes to Bootstrap 5, the framework provides a wide range of pre-built components to streamline this process. One such component is the form input with an icon, which adds a touch of visual appeal and enhances user experience. Let's delve into the intricacies of form input with icon in Bootstrap 5.

Before we dive into the specifics, it's essential to understand that Bootstrap 5 uses a utility-first approach, which means that instead of predefined components, it offers a set of utilities that can be combined to build complex components. This approach provides more flexibility and customization options.

Understanding Form Input with Icon in Bootstrap 5
In Bootstrap 5, a form input with an icon is essentially a combination of a form input and an icon. The icon can be used to provide visual feedback or to indicate the type of input expected. For instance, a search icon in a form input field suggests that the user should type a search query.

Bootstrap 5 uses the Font Awesome library for icons by default. However, you can replace it with any other icon font library of your choice. To use an icon with a form input, you need to combine the input utility classes with the icon utility classes.
Adding an Icon to the Left of a Form Input

To add an icon to the left of a form input, you can use the 'me-2' utility class to create some space between the icon and the input, and the 'bi' class to specify the icon. Here's an example:
<div class="input-group mb-3">
<span class="input-group-text">
<i class="bi bi-search me-2"></i>
</span>
<input type="text" class="form-control" placeholder="Search">
</div>
In this example, the 'bi bi-search' class is used to specify the search icon from the Font Awesome library. The 'me-2' class adds some margin to the right of the icon, creating space between the icon and the input field.

Adding an Icon to the Right of a Form Input
Adding an icon to the right of a form input is similar to adding one to the left. You just need to use the 'ms-2' utility class instead of 'me-2' to create some space between the icon and the input. Here's an example:
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Username">
<span class="input-group-text">
<i class="bi bi-person me-2"></i>
</span>
</div>

In this example, the 'bi bi-person' class is used to specify the person icon from the Font Awesome library. The 'ms-2' class adds some margin to the left of the icon, creating space between the icon and the input field.
Customizing Form Input with Icon in Bootstrap 5




















While the default form input with icon in Bootstrap 5 is quite versatile, you might want to customize it to fit your specific needs. This could involve changing the color, size, or style of the icon, or adding additional functionality to the input field.
To customize the icon, you can use the icon utility classes provided by Bootstrap 5. For example, to change the color of an icon, you can use the 'text-primary', 'text-secondary', etc. classes. To change the size of an icon, you can use the 'fs-2', 'fs-3', etc. classes. Here's an example:
<div class="input-group mb-3">
<span class="input-group-text text-primary fs-3">
<i class="bi bi-search me-2"></i>
</span>
<input type="text" class="form-control" placeholder="Search">
</div>
In this example, the 'text-primary' class is used to change the color of the icon to blue, and the 'fs-3' class is used to increase the size of the icon.
Adding Functionality to Form Input with Icon
To add functionality to a form input with icon, you can use JavaScript or jQuery. For example, you might want to add a click event to the icon that triggers a search function. Here's an example using jQuery:
<div class="input-group mb-3">
<input type="text" class="form-control" id="searchInput" placeholder="Search">
<span class="input-group-text">
<i class="bi bi-search me-2" id="searchIcon"></i>
</span>
</div>
In this example, the 'id' attribute is used to give the input field and the icon unique identifiers. Then, in your JavaScript or jQuery code, you can add a click event listener to the icon that triggers a search function:
$('#searchIcon').click(function() {
var searchQuery = $('#searchInput').val();
// Perform search function with searchQuery
});
In conclusion, form input with icon in Bootstrap 5 is a powerful tool that can enhance the user experience and provide visual feedback. With a bit of creativity and customization, you can create form inputs that are not only functional but also aesthetically pleasing. So, go ahead and explore the possibilities of form input with icon in Bootstrap 5!