简体   繁体   English

如何在角度材料中使用 css 更改 md-input-container 占位符颜色?

[英]How do I change md-input-container placeholder color using css in angular material?

How do I change md-input-container placeholder color using css in Angular Material?如何在 Angular Material 中使用 css 更改 md-input-container 占位符颜色? As screenshot below I have phone no.作为下面的截图,我有电话号码。 and password textfield.和密码文本字段。 Phone no.电话号码。 textfield has Phone No. and password has Password placeholder name.文本字段有电话号码,密码有密码占位符名称。

在此处输入图片说明

in the last version of angular you can remove the placeholder in the input and add a mat-placeholder in the mat-form-field and custom the css with a class在 angular 的最新版本中,您可以删除输入中的占位符并在mat-form-field 中添加一个mat-placeholder并使用类自定义 css

html : html :

<mat-form-field>
    <input matInput type="text">
    <mat-placeholder class="placeholder">Search</mat-placeholder>
</mat-form-field>

css: css:

.mat-focused .placeholder {
    color: #00D318;
}

Placeholder is depicted as a <label> in Angular Material .占位符在Angular Material 中被描述为<label> So you actually need to style the label not the placeholder.所以你实际上需要设置标签而不是占位符的样式。

As soon as you click (focus) on the input this <label> which is looking as a placeholder slides up and converted into a form <label> .一旦您单击(聚焦)输入,这个看起来像占位符的<label>就会向上滑动并转换为表单<label>

So you just need to apply this CSS:所以你只需要应用这个 CSS:

/* When the input is not focused */
md-input-container label {
  color: red;
}

/* When the input is focused */
md-input-container.md-input-focused label {
  color: blue;
}

Have a look at this Plunkr Demo .看看这个Plunkr 演示

As @Wenacary told in this post: How to change Angular 5 Material input placeholder? 正如@Wenacary在这篇文章中所说: 如何更改Angular 5 Material输入占位符?

In the last version of angular you can remove the placeholder in the input and add a mat-placeholder in the mat-form-field and custom the css with a class 在角度的最后一个版本中,您可以删除输入中的占位符,并在mat-form-field中添加mat-placeholder并使用类自定义css

html : HTML:

 <mat-form-field> <input matInput type="text"> <mat-placeholder class="placeholder">Search</mat-placeholder> </mat-form-field> 

css: CSS:

 .mat-focused .placeholder { color: #00D318; } 

In Angular 4+在 Angular 4+

First you will need to turn ViewEncapsulation off to style Material Elements.首先,您需要关闭 ViewEncapsulation 以设置 Material Elements 样式。 Be warned this is subverting the Angular emulated-shadow DOM default and you should proceed with caution ( https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html ).请注意,这会破坏 Angular 模拟阴影 DOM 的默认设置,您应该谨慎行事( https://blog.thoughtram.io/angular/2015/06/29/shadow-dom-strategies-in-angular2.html )。

In dummy.component.ts:在 dummy.component.ts 中:

@Component({
 ...,
 encapsulation: ViewEncapsulation.None,
})

Then give your < mat-form-field > element a unique class in dummy.component.html:然后在 dummy.component.html 中给你的 < mat-form-field > 元素一个唯一的类:

<mat-form-field class="dummy-input-field" floatPlaceholder="always">
  <input placeholder="Dummy"/>
</mat-form-field>

Finally in dummy.component.css apply the styling:最后在 dummy.component.css 中应用样式:

.dummy-input-field .mat-input-placeholder {
  color: red;
}

Similarly, if you'd like to dynamically change color if the field is focused:同样,如果您想在字段聚焦时动态更改颜色:

.dummy-input-field.mat-focused .mat-input-placeholder {
  color: red;
}

For the newer versions of material which have a mat prefix instead of md prefix, you can do this in 2 ways:对于具有mat前缀而不是md前缀的较新版本的材料,您可以通过两种方式执行此操作:

way 1: using view encapsulation set to none and then writing the styles in the components css file, like @user2245995 pointed out in the answer above.方式1:使用视图封装设置为none,然后在组件css文件中写入样式,如@user2245995在上面的答案中指出的那样。 Although this is the way angular suggests, please be advised that the styles you write here will propagate to all the child/parent components and effect other elements there.尽管这是 angular 建议的方式,但请注意,您在此处编写的样式将传播到所有子/父组件并影响那里的其他元素。

way 2: We can use the shadow piercing descendant combinators ie /deep/ or ::ng-deep or >>> Below is an example方式 2:我们可以使用阴影穿透后代组合器,/deep/::ng-deep>>>下面是一个例子

/deep/ label.mat-input-placeholder {
  color: #fff;   // choose the color you want
}

Although this method is specified in the angular docs as of now, they have mentioned that this method will soon be deprecated.尽管目前在 angular 文档中指定了此方法,但他们提到此方法将很快被弃用。 read more: https://angular.io/guide/component-styles#!#-deep-阅读更多: https : //angular.io/guide/component-styles#!#-deep-

.container {
  .mat-form-field-outline,
  .mat-form-field-empty.mat-form-field-label,
  .mat-form-field-label,
  .mat-form-field-underline,
  .mat-input-element,
  ::placeholder {
    color: $white !important;
  }
}

The code above gives me the results below.上面的代码给了我下面的结果。 I am overriding the form-field outline, label-empty, label, underline, input element, placeholder text .我正在覆盖form-field大纲、标签空、标签、下划线、输入元素、占位符文本

I'm using Angular 8.2.2 and Angular Material 8.2.2我正在使用 Angular 8.2.2 和 Angular Material 8.2.2 3 个输入字段的图像,带有全白边框、占位符文本和标签

I tried to be as deterministic as possible for the color of a mat input and I dare to share the result here, hoping it will help some others (the placeholder color customization need is handled, as asked in the question):我试图尽可能确定垫子输入的颜色,我敢在这里分享结果,希望它会帮助其他人(处理占位符颜色自定义需求,如问题中所问):

CSS custom properties used使用的 CSS 自定义属性

Note: The colors are considered different when the focus is here or not, that is why we have 2 blocs in the following:注意:当焦点在这里与否时,颜色被认为是不同的,这就是为什么我们在下面有 2 个块:

    --clear-button-color: lightblue;
    --asterisk-color: lightgreen;
    --label-color: springgreen;
    --underline-color: blue;
    --input-color: lightgray;

    --clear-button-focused-color: blue;
    --asterisk-focused-color: green;
    --label-focused-color: pink;
    --underline-focused-color: yellow;
    --input-focused-color: gray;
    --placeholder-focused-color: magenta;
    --caret-focused-color: blue;

SCSS styling SCSS 样式

    .mat-form-field {
        &.mat-focused {
            > .mat-form-field-wrapper {
                > .mat-form-field-flex {
                    > .mat-form-field-infix {
                        > .mat-input-element {
                            color: var(--input-focused-color);
                            caret-color: var(--caret-focused-color);

                            &::placeholder {
                                color: var(--placeholder-focused-color);
                            }
                        }
                        > .mat-form-field-label-wrapper {
                            > .mat-form-field-label {
                                > mat-label {
                                    color: var(--label-focused-color);
                                }

                                > .mat-placeholder-required {
                                    color: var(--asterisk-focused-color);
                                }
                            }
                        }
                    }
                    > .mat-form-field-suffix {
                        > .mat-focus-indicator {
                            > .mat-button-wrapper {
                                > .mat-icon {
                                    color: var(--clear-button-focused-color);
                                }
                            }
                        }
                    }
                }
                > .mat-form-field-underline {
                    > .mat-form-field-ripple {
                        background-color: var(--underline-focused-color);
                    }

                    background-color: var(--underline-focused-color);
                }
            }
        }

        > .mat-form-field-wrapper {
            > .mat-form-field-flex {
                > .mat-form-field-infix {
                    > .mat-input-element {
                        color: var(--input-color);

                        &::placeholder {
                            color: var(--placeholder-color);
                        }
                    }
                    > .mat-form-field-label-wrapper {
                        > .mat-form-field-label {
                            > mat-label {
                                color: var(--label-color);
                            }

                            > .mat-placeholder-required {
                                color: var(--asterisk-color);
                            }
                        }
                    }
                }
                > .mat-form-field-suffix {
                    > .mat-focus-indicator {
                        > .mat-button-wrapper {
                            > .mat-icon {
                                color: var(--clear-button-color);
                            }
                        }
                    }
                }
            }
            > .mat-form-field-underline {
                > .mat-form-field-ripple {
                    background-color: var(--underline-color);
                }

                background-color: var(--underline-color);
            }
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM