25

Is there any way in which I can stop the placeholder from floating as a label for the following snippet of code?

<form class="search-form">
  <mat-form-field class="example-full-width">
     <input class="toolbar-search" type="text" matInput>
     <mat-placeholder>Search</mat-placeholder>
     <mat-icon matSuffix style="font-size: 1.2em">search</mat-icon>
  </mat-form-field>
</form>

enter image description here

I have looked at the official documentation for angular/material, but it seems this feature is now deprecated?

7 Answers 7

26

assuming you are using latest stable version of material 2,
you can use floatLabel="never" to force label to not to float.

here is live working demo

this is clear in documentation https://material.angular.io/components/form-field/api

Sign up to request clarification or add additional context in comments.

3 Comments

With this option label doesn't stay when we add values. I want label to stay like floating. But should stay the same without value. Any idea how to achieve this?
use can use floatLabel="always"
No more 'never' option for angular material 18.x.x, There are now only two values for floatingLabel attribute: 'auto' or 'always'. You can have the same behavior with delete mat-label, add a placeholder value and add floatLabel="always" <mat-form-field floatLabel="always"> <input [formControl]="inputControl" [matAutocomplete]="auto" [placeholder]="Search" matInput /></mat-form-field>
9
<form class="search-form">
  <mat-form-field class="example-full-width" appearance="standard">
    <input class="toolbar-search" type="text" matInput>
    <mat-placeholder>Search</mat-placeholder>
  <mat-icon matSuffix style="font-size: 1.2em">search</mat-icon>
  </mat-form-field>
</form>

Please set the appearance of mat-form-field to standard and the placeholder will stop behaving like label.

Explanation : By default the mat-label in mat-form-field floats and the appearance of mat-form-field is "legacy". That means if a mat-label is not present with the form field then placeholder will start behaving like the label and it floats upwards. So to stop this you should manually update the appearance to "standard", so it stops behaving like label and stays as a placeholder.

Comments

4

Praveen Soni's answer above doesn't work for non-legacy versions, as per this:

Note: only the legacy appearance supports the never option. never was originally added as a way to make the floating label emulate the behavior of a standard input placeholder. However the form field now supports both floating labels and placeholders. Therefore in the non-legacy appearances the never option has been disabled in favor of just using the placeholder.

That means you should simply use placeholder instead of the label.

Comments

3

1.Use floatLabel="never" in mat-form-field

<mat-form-field floatLabel="never">
</mat-form-field>

Comments

3

just add floatLabel="never" in mat-form-field i.e.

<mat-form-field floatLabel="never">
   <input type="text" matInput placeholder="Other">
</mat-form-field>

As you can see the image below, cursor is inside input field but label didn't float.

enter image description here

Comments

1
<div class="custom_autosuggestion">
   <mat-form-field class="example-full-width" floatLabel="never">
      <input class="custom_color_title"
      aria-label="Number" matInput [formControl]="myColorControl" type="text" name=""                                                  [matAutocomplete]="auto">
      <mat-placeholder class="custom_placeholder">{{templateParse.Search_Colors_by_Name_or_Code}}</mat-placeholder>
      <mat-autocomplete #auto="matAutocomplete">
      <mat-option *ngFor="let option of filteredColorOptions | async" [value]="option">
                                                        <!-- {{option}} -->
      </mat-option>
      </mat-autocomplete>
   </mat-form-field>
</div>

1 Comment

Just add floatLabel="never" option with mat-form-field
1

According to the official doc on floating labels, the placeholder gets promoted as the label too for mat-form-field that isn't of the appearance "outline". To prevent this, you should specify an empty label like so:

  <mat-form-field class="example-full-width">
     <mat-label></mat-label> // THE EMPTY LABEL
     <input class="toolbar-search" type="text" matInput>
     <mat-placeholder>Search</mat-placeholder>
     <mat-icon matSuffix style="font-size: 1.2em">search</mat-icon>
  </mat-form-field>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.