How to Add a Search Icon to an Input Field in Angular Material?

To add a search icon to an input field in Angular, you can take the help of the matPrefix and matSuffix attributes. The matPrefix places the search icon at the starting of the input field while the matSuffix places the search icon at the end of the input field.

Let’s say you want to put the search icon at the end of the input. Then you can simply add the matSuffix attribute to the <mat-icon>. It will automatically move the search icon at the end of the input field:

<mat-form-field appearance="fill">
    <mat-label>Search Something</mat-label>
    <input type="text" matInput placeholder="Type something..">
    <mat-icon matSuffix>search</mat-icon>
</mat-form-field>

This will produce the following result:

Add a search icon at the end of input field in angular material

Similarly, if you want to place the search icon at the beginning of the input field, you have to add the matPrefix attribute to the <mat-icon>.

This is how you can do it in your template file:

<mat-form-field appearance="fill">
    <mat-label>Search Something</mat-label>
    <mat-icon matPrefix>search</mat-icon>
    <input type="text" matInput placeholder="Type something..">
</mat-form-field>

The above code will produce the following result:

Add a search icon at the beginning of input field in angular material

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts