【问题标题】:How to override styles of md-input-container in angular2?如何在 angular2 中覆盖 md-input-container 的样式?
【发布时间】:2017-12-29 16:32:33
【问题描述】:

我有以下使用角度材料设计的 md-input-container 代码:

<md-input-container>
      <input mdInput [placeholder]="'common.password' | translate" type="password" formControlName="password" (change)="onPasswordChange($event.target.value)" required validate-onBlur/>
      <md-hint *ngIf="frmLoginInfo.controls.password.pristine" class="s-text-caption s-hint">{{ 'signup.pwdRule' | translate }}</md-hint>
</md-input-container>

这个容器内部有一个 div 类 (mat-input-wrapper),其填充为 padding-bottom: 1.296875em。

如何覆盖容器的这种填充样式?

PS : 向容器中添加类并将 padding: 0px 作为 important 也不起作用。

【问题讨论】:

  • .mat-input-wrapper { padding-bottom: 0px !important;} 应该可以工作
  • 是的@sTx 虽然这会将.mat-input-wrapper所有 个实例更改为0 填充
  • 因此您应该为父元素添加idclass,就像@0mpurdy 在他的回答和目标中所说的#parent &gt; .mat-input-wrapper{....}

标签: html css angular angular-material2


【解决方案1】:

更新

Angular 关于/deep/&gt;&gt;&gt; (Thanks @DeborahK) 的官方回复

已弃用对模拟 /deep/ CSS 选择器(Shadow-Piercing 后代组合器又名 >>>)的支持,以匹配浏览器实现和 Chrome 删除的意图。 ::ng-deep 已添加为当前使用此功能的开发人员提供临时解决方法。

发件人:http://angularjs.blogspot.co.uk/2017/07/angular-43-now-available.html

Another related question


为组件添加更具体的样式 - see CSS specificity

html

<md-input-container id="forceStyle">
    <input mdInput [placeholder]="'common.password' | translate" type="password" formControlName="password" (change)="onPasswordChange($event.target.value)" required validate-onBlur/>
    <md-hint *ngIf="frmLoginInfo.controls.password.pristine" class="s-text-caption s-hint">{{ 'signup.pwdRule' | translate }}</md-hint>
</md-input-container>

css

>>> #forceStyle .mat-input-wrapper {
  padding-bottom: 0px;
}

您需要&gt;&gt;&gt; 解释here

但是 /deep/&gt;&gt;&gt; 已被弃用,如 here 所述

Live plunker example

【讨论】:

  • 感谢您的帮助,但这也不起作用..在 chrome 开发人员工具上检查元素后,我无法看到应用的样式。
  • @ShrutiAgarwal - 这个例子应该稍微改变一下:#forceStyle.mat-input-wrapper - 在我看到你的屏幕之后
  • 我已通过修复和现场演示示例更新了答案,但请注意 /deep/ 已被弃用 - 请参阅我链接的 GitHub 问题中的替代方案。
  • 我觉得不需要deep,只是一个更具体的目标
  • @ShrutiAgarwal - 确保你清除缓存,甚至再次提供应用程序,因为有时没有加载更改
【解决方案2】:

试试这个:

<div class="box-model"> 
       <md-input-container fxFlex="auto" class="custom-search-mdinput">
         <input mdInput [placeholder]="'common.password' | translate" type="password" formControlName="password" (change)="onPasswordChange($event.target.value)" required validate-onBlur/>
         <md-hint *ngIf="frmLoginInfo.controls.password.pristine" class="s-text-caption s-hint">{{ 'signup.pwdRule' | translate }}</md-hint>
        </md-input-container>
</div>

@Component({
    selector: "dashboard",
    templateUrl: "dashboard.component.html",
    styles: [`.box-model .mat-input-wrapper {
        margin: 0px !important;
        padding-bottom: 0px !important;
    }`
    ],

【讨论】:

【解决方案3】:

HTML

<div id="wrapper">
  <md-input-container>
     <input mdInput [placeholder]="'common.password' | translate" type="password" formControlName="password" (change)="onPasswordChange($event.target.value)" required validate-onBlur/>
      <md-hint *ngIf="frmLoginInfo.controls.password.pristine" class="s-text-caption s-hint">{{ 'signup.pwdRule' | translate }}</md-hint>
  </md-input-container>
</div>

CSS

#wrapper .mat-input-wrapper{padding-bottom: 0px !important;}

重要 .mat-input-wrapper 需要是 wrapper 的孩子 - 从你的例子中我不知道那个类来自哪里


在您的帖子更新后,我认为它应该可以如下工作: #forceStyle &gt; .mat-input-wrapper{padding-bottom: 0 !important} - 即使没有 important 也应该可以工作,但只是为了确定。

确保清除缓存,甚至重新启动应用,因为有时更改不会加载

【讨论】:

  • 我清除了缓存并重新启动了应用程序。没用。
  • 太奇怪了....你能给#forceStyle一个样式,边距,边框,任何东西,看看你是否能在inspect中看到它?
  • 是的,我给它添加了边框,它被添加了。
  • 现在试试#forceStyle &gt; div{padding-bottom: 0 !important}
  • 此样式未应用
【解决方案4】:

在样式中使用深度

/deep/ .mat-input-wrapper { 
  padding-bottom: 0px ;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-10
    • 2015-05-09
    • 2017-04-09
    • 1970-01-01
    • 2017-08-28
    • 2017-11-29
    • 2017-05-03
    • 2018-02-09
    相关资源
    最近更新 更多