Home > Web Front-end > CSS Tutorial > How to Add an Icon to a Search Input in Bootstrap?

How to Add an Icon to a Search Input in Bootstrap?

Patricia Arquette
Release: 2024-11-16 12:27:03
Original
297 people have browsed it

How to Add an Icon to a Search Input in Bootstrap?

Search Input with an Icon Using Bootstrap

In Bootstrap 4, you can no longer use Glyphicons for icons. Instead, you can either set the icon as a background or use a Font Awesome icon with custom positioning.

Background Image:

.form-control {
  background-image: url('https://res.cloudinary.com/dt9b7pad3/image/upload/v1502810110/angle-down-dark_dkyopo.png');
  background-position: right center 5px;
}
Copy after login

This method, however, may not always work reliably.

Font Awesome Icon with Custom Positioning:

.input-group {
  position: relative;
}

.fa-search {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
}
Copy after login

Bootstrap 5:

In Bootstrap 5, you can use the input-group component to easily add an icon to a search input.

<div>
Copy after login
Copy after login
Copy after login

Alternative Approaches:

  • Input-Group with Border Utils: Use the border classes to create a border around the input and the icon.
<div>
Copy after login
Copy after login
Copy after login
  • Input-Group with Text:
<div>
Copy after login
Copy after login
Copy after login

The above is the detailed content of How to Add an Icon to a Search Input in Bootstrap?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template