Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular material: How to right align matInput placeholder?

I have this simple code:

<body style="direction: rtl; text-align: right">
    <mat-form-field>        
        <input matInput placeholder="Wanna be rtl" />
    </mat-form-field>
</body>

BUT no matter what I'm trying to, the placeholder keep acting as left to right. Is there any way to align it to the right?

like image 536
Aharon Ohayon Avatar asked Jul 11 '18 15:07

Aharon Ohayon


People also ask

How do you align mat hint?

Hints added via the <mat-hint> hint element can be added to either side by setting the align property on <mat-hint> to either start or end . Attempting to add multiple hints to the same side will raise an error.

How do you center a field input mat?

You can put style="text-align:center;" inside input element.

Why do we use Matinput?

Matinput is an Angular directive that primarily allows input and text area elements to work with a form field. With this, you can display placeholders perfectly, add custom error messages, a clear button, specify the maximum length of the text or add prefixes and suffixes for a seamless user experience.

What is Mat label?

mat-label is similar to labels which we use in normal HTML forms. But the advantage with mat-label is that it has pre-defined CSS style classes and animations defined in it. Installation syntax: ng add @angular/material.


2 Answers

Using text-align on the form field will work: See working StackBlitz example

This solution will align both the placeholder and the input text to the right:

<body>
    <mat-form-field style="text-align: right">        
        <input matInput placeholder="Wanna be rtl" />
    </mat-form-field>
</body>

Result: enter image description here

If you want to only align the placeholder to the right and keep the input text aligned left then add style="text-align: left" to the input as shown below

<body>
    <mat-form-field style="text-align: right">        
        <input matInput placeholder="Wanna be rtl" style="text-align: left"/>
    </mat-form-field>
</body>

Result: enter image description here

like image 167
Narm Avatar answered Sep 20 '22 04:09

Narm


Use the Bidirectionality module in CDK:

import {BidiModule} from '@angular/cdk/bidi';

...

<body dir="rtl">
    <mat-form-field>        
        <input matInput placeholder="I am RTL" />
    </mat-form-field>
</body>
like image 37
G. Tranter Avatar answered Sep 17 '22 04:09

G. Tranter