Skip to content

bug(MatFormFieldModule): Label is outside the bounds of the form field #27431

Open
@GeorgianStan

Description

@GeorgianStan

The label associated with a MatFormField is not positioned correctly.

Case 1 - matPrefix
As visible in the following image, the label is outside the bounds of the form field.

1

Inside form-field.ts - _updateOutlineLabelOffset() the HTML element is moved on the x-axis:

floatingLabel.style.transform = `var( --mat-mdc-form-field-label-transform, {FLOATING_LABEL_DEFAULT_DOCKED_TRANSFORM} translateX(${labelHorizontalOffset}))`;

This is also visible in DevTools.
This transformation rule looks valid because it has a prefix icon.
But in addition, if the element is moved on the x-axis, its width should be reduced by a value equal to the offset to fit the form field bounds.

width: calc(100% - calc(1 * (51.97072219848633px + var(--mat-mdc-form-field-label-offset-x, 0px))));

2

Case 2 - matSuffix
For matSuffix the fix would be easier, it should only update its width with the offset value.

4

However here it seems that the offset variable is missing from the scss file.
var

Adding the CSS variable to the above file and updating the width should help with this issue.

Reproduction

StackBlitz link: https://stackblitz.com/edit/rudguk-yo1ggv?file=src%2Fexample%2Fform-field-appearance-example.html

Expected Behavior

I expect the label to fit into the borders of the wrapping element.

Actual Behavior

The label goes outside the borders of the wrapping element.

Environment

  • Angular: latest
  • CDK/Material: latest
  • Browser(s): Any
  • Operating System (e.g. Windows, macOS, Ubuntu): Any

Metadata

Metadata

Assignees

Labels

P4A relatively minor issue that is not relevant to core functionsarea: material/form-field

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions