Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material Design: how to disconnect float label for select?

I need to create select field without float label but I want to have placeholder and default value.

I read docs https://material.angular.io/components/form-field/overview#floating-label and tried to do it by myself.

<mat-form-field [floatLabel]="never">
  <mat-select placeholder="All categories" [formControl]="catForm" multiple> //First opportunity for use placeholder
    <mat-option *ngFor="let category of categories" [value]="category.name">
      {{ category.name }}
    </mat-option>
  </mat-select>
  <!-- <mat-placeholder>All categories</mat-placeholder> -->//Second opportunity for use placeholder
</mat-form-field>

And anyway I get float label. That am I doing wrong?

enter image description here

like image 860
Sergei R Avatar asked Apr 01 '18 17:04

Sergei R


People also ask

What are floating labels?

Floating labels display the type of input a field requires. Every Text Field and Select should have a label, except for full-width text fields, which use the input’s placeholder attribute instead. Labels are aligned with the input line and always visible. They can be resting (when a field is inactive and empty) or floating.

How do you style a floating label in HTML?

The most important styling for a floating label is to make the label absolutely positioned inside a relative parent element. We want to be able to move our label around the input container without it disrupting the flow of elements.

How to remove labels from text field?

If you want to remove the entire label from the text field. I did this in material V5 you can use the sx prop and disable the label entirely like this

Should every input field have a label?

Ideally, every input field should have an associated label and a placeholder, if needed. However, from a design perspective, this can cause a form to look rather text-heavy. Labels-as-placeholders, sometimes known as floating labels, are a popular design choice for creating minimalist and accessible forms.


2 Answers

The correct way is that:

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

Square brackets for variables.

like image 102
Sergei R Avatar answered Dec 08 '22 23:12

Sergei R


Sergei R has the correct usage for basic inputs (input type=text) but for the dropdown (select), it simply doesn't work. Even the Angular Material docs (https://material.angular.io/components/form-field/overview#floating-label) have sample code that (when augmented for this specific scenario, floatLabel="never"), indicate that it doesn't work: https://angular-vij8al.stackblitz.io

I added the fourth example of how to get the placeholder effect without the label (but you lose the ability to use more complex text).

like image 28
Demetrios Christopher Avatar answered Dec 09 '22 00:12

Demetrios Christopher