Reputation: 771
I am using Angular 9 and Angular Material 10.2.0.
My code is:
<mat-form-field>
<input matInput type="text" name="" placeholder="title" id="title" [(ngModel)]="titleValue" >
</mat-form-field>
I followed how to change text box placeholder color and how-do-i-change-md-input-container-placeholder-color-using-css-in-angular, but I still couldn't change the color and the default is still there.
Upvotes: 4
Views: 12556
Reputation: 21
HTML File
<input [style.color]="color" type="text" class="placeholder-color" placeholder="xyz" />
CSS File
::placeholder {
color: var(--placeholder-color);
}
Any change in color variable value will also change the place-holder color.
Upvotes: 2
Reputation: 186
I was able to achieve using something like this
Use a variable for the property in your css/scss file. And make use of ElementRef to set property declared in the css/scss file during runtime.
input {
display: block;
font-size: 16px;
font-style: normal;
font-weight: 400;
color: #333333;
--placeHolder-color: #959595;
}
input::placeholder {
color: var(--placeHolder-color);
}
input::-webkit-input-placeholder { /* Edge */
color: var(--placeHolder-color);
}
input:-ms-input-placeholder { /* Internet Explorer 10-11 */
color: var(--placeHolder-color);
}
input:-moz-placeholder {
color: var(--placeHolder-color);
}
input::-moz-placeholder {
color: var(--placeHolder-color);
}
import { Directive, ElementRef, Host, Renderer2 } from '@angular/core';
import { CardNumberComponent } from 'src/app/themes/card-number/card-number.component';
@Directive({
selector: '[appInputStyle]'
})
export class InputStyleDirective {
private input: HTMLInputElement;
constructor(private el: ElementRef, private renderer: Renderer2) {
this.input = this.el.nativeElement;
}
ngOnInit() {
this.input.style.setProperty('--placeHolder-color', 'tomato');
}
}
<input type="text" appInputStyle>
Upvotes: 3
Reputation: 1627
In your global css
copy and paste the following code:
input::placeholder {
opacity: 1 !important;
color: #FFF !important;
}
Or in your case
mat-form-field input::placeholder {
opacity: 1 !important;
color: #FFF !important;
}
Upvotes: 1
Reputation: 53
Following is the cross browser solution for this:
input::-webkit-input-placeholder { /* Chrome/Opera/Safari */
color: red;
}
input::-moz-placeholder { /* Firefox 19+ */
color: red;
}
input:-ms-input-placeholder { /* IE 10+ */
color: red;
}
input:-moz-placeholder { /* Firefox 18- */
color: red;
}
<input placeholder="This is input color"/>
Upvotes: 0
Reputation: 260
I'm not sure if this will work with Angular material, but you kan give it a try, in your css add:
::-webkit-input-placeholder{
color:red
}
Upvotes: 0