Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search input with icon before placeholder text - Material Design

Code is below. Currently the placeholder text overlaps the icon. I need the placeholder text indented to the right of the icon. Perhaps there is a better way to include an icon and placeholder text in a matInput element?

<mat-form-field>
    <mat-icon fontSet="myfont"
        fontIcon="my-icon-search"
        class="search-icon"></mat-icon>
    <input id="app_search_input"
        matInput
        type="search"
        placeholder="{{'common.search' | translate}}"
        class="search-input">
</mat-form-field>
like image 692
Scott B Avatar asked Apr 26 '18 16:04

Scott B


People also ask

How do I add a search icon to my input box?

To add icon inside the input element the <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages or in some specific area, it needs the fontawesome link inside the head tag. The fontawesome icon can be placed by using the fa prefix before the icon's name.

How do you set an input field icon in Fa Fa?

The font-awesome icon can be placed by using the fa prefix before the icon's name. Example: In this example, we will take a form where the input field is necessary. After that, we will place the font-awesome icon inside the input field. We will use the CDN link to use the font-awesome icons.

How do you enter a text box in HTML?

The <input type="text"> defines a single-line text field. The default width of the text field is 20 characters. Tip: Always add the <label> tag for best accessibility practices!


1 Answers

You can use matPrefix.

<mat-form-field>
    <mat-icon matPrefix>search</mat-icon>
    <input matInput type="search" placeholder="search here" class="search-input">
</mat-form-field>

Both matPrefix and matSuffix are available:

https://stackblitz.com/angular/byemgpqqxdx?file=app%2Finput-prefix-suffix-example.ts

like image 113
rjferguson21 Avatar answered Sep 19 '22 12:09

rjferguson21