4

I tried doing a stackblitz but it doesn't accept my <mat-form-field> and <mat-label> tags without errors, I imported the needed components but it won't work.

But maybe you can help me anyway if I post here the code, I'm having an issue with CSS as I don't know what to target to move the <mat-label> tag, when I use a <mat-form-field> with appearance="outline" when the label is in the center, over the input field, I can't move it, I want it to be centered, but it's at the bottom of the mat-form-field.

If I adjust the position:absolute values, the problem is, that also when the label goes above the field when a click happens, those rules will be applied as well, moving the floating label in the wrong position.

The code:

<div 
    id="test" 
    fxLayout="row" 
    fxLayoutGap="12px" 
    fxLayoutAlign="start center" 
    [class.hasFocus]="numberMatInput.value">
    <mat-form-field appearance="outline">
         <mat-label>KM</mat-label>
         <input matInput #numberMatInput [formControl]="numberInput">
    </mat-form-field>
</div>

and the CSS:

#test ::ng-deep .mat-form-field-appearance-outline {
  width: 65px;
  height: 45px;
  line-height: 1.725;
}

#test ::ng-deep .mat-form-field-appearance-outline .mat-form-field-flex {
  padding: 0px 10px 0px 10px;
}

#test ::ng-deep .mat-form-field-infix {
  padding: 3px 0 3px 0;
}

this is the wanted result :

enter image description here

so basically I need to move the label when the input field is untouched, and leave it in the same space floating above when it's focused.

to me it seems there is only one rule for both, so if I move the untouched label, the floating one will move as well.

Can someone help? thank you

2
  • The class .mat-form-field-appearance-outline doesn't seem to exist? Commented Aug 30, 2018 at 11:01
  • it exists, it s what gives the with and the height of the mat-form-field, these classes are directly from the chrome dev tool and they work in my css Commented Aug 30, 2018 at 11:03

1 Answer 1

10

Try using the following css:

#test ::ng-deep .mat-form-field-hide-placeholder .mat-form-field-label-wrapper {
   text-align: center;
   top: -25px;
}

#test ::ng-deep .mat-form-field-should-float {
   text-align: center;
}

Demo

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

9 Comments

Good job @kboul thnks anyway
Thank you @harkeshkumar. Did you face also this issue?
thank you very much! I literally found a solution 1 min before you posted, but yours seems much cleaner! I abused !important and targeted the 3 instances (label inside, label above, and label above with input not empty) as I did a change and instead to have the floating label in the center but in the left corner, could you help me apply your solution to the current styling? stackblitz.com/edit/angular-chtqjk-2qmksf
Just replace your css with mine: stackblitz.com/edit/angular-chtqjk-gbkb4f. You can check also the demo I included in the answer.
I think I prefer it on the side the label! if it's not too much complicated to push the floating label on the side, otherwise it's good like this, thank you very much for the help anyway!
|

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.