【问题标题】:Material 2 dialog change styleMaterial 2 对话框更改样式
【发布时间】:2017-02-25 11:19:19
【问题描述】:

我正在尝试更改 md 对话框的样式。

在我的 main.scss 中,我正在导入预构建的粉色-蓝灰色主题... 然后在我的组件中导入以下内容-->

@import "@angular/material/dialog/dialog.scss";

$mat-dialog-padding: 0;
$mat-dialog-border-radius: 0.5rem;
$background: #ffffff;

@mixin mat-dialog-container {
    padding: $mat-dialog-padding;
    border-radius: $mat-dialog-border-radius;
    background: $background;
}

@include mat-dialog-container;

内边距和边框半径已正确应用于对话框窗口。 但是背景不工作......也尝试了 !important 声明。

我在单个组件中使用它... 在全球范围内应用这些样式是否也有变化?

在 chrome 开发工具中,我看到了那些应用的样式更改。背景被粉蓝灰色主题覆盖了..

希望任何人都可以提供帮助。

谢谢

【问题讨论】:

    标签: css angular-material2


    【解决方案1】:

    最好在对话框周围添加一个包装类,然后为子项添加样式。 Have a look at this article 了解更多信息。

    当您打开 Angular 对话框时,您可以添加 panelClass 属性,像这样:

    this.dialog.open(MyDialogComponent, {panelClass: 'my-panel'}).

    然后,在您的 css 中(例如,在根 styles.css 文件中),您可以添加以下内容:

    .my-panel .mat-dialog-container {
        overflow: hidden;
        border-radius: 5px;
        padding: 5px;
    }
    

    编辑 警告

    也可以将 css 添加到根 style.css 之外的另一个文件中,但是您必须在 css 中使用 ::ng-deep(例如 ::ng-deep .my-panel{ // ... })。不建议这样做,因为::ng-deepdeprecated in Angular

    EDIT2 不错的选择

    如果您使用的是 scss,那么您可以将 .my-panel-style 放在 mydialog.component.scss 文件中,方法是在 styles.scss 中使用 @mixin@import 文件。然后您可以使用@include 来加载定义的mixin。

    在您的 mydialog.component.scss 文件中

    @mixin myPanel(){
     .my-panel .mat-dialog-container {
       // css here
     }
    }
    

    在你的 style.scss 中

    @import 'path/to/mydialog.component.scss' // you don't need the .scss suffix
    
    @include myPanel();
    

    【讨论】:

    • 有没有办法将样式封装在非全局 css 中?如果您必须将内容插入应用程序的根 css,那么很难以这种方式重用对话框。
    • 是的,你可以将它添加到对话框的css文件中:例如'mydialog.component.css'
    • 它对我不起作用 :S mat.dialog-container 实际上不是比实际元素高 2 级吗?所以我想它应该在更高的地方?
    • 好的,那么我需要检查一下我是如何在我们的应用程序中做到这一点的,但要到下周才能做到这一点。如果您使用的是 sass 或更少,您可以创建一个单独的文件,并将其包含在您的主 .scss 文件中。
    • 我查看了我们的代码,我们将 css 保存在 mydialog.component.css 中,但我们使用 ::ng-deep 表示法。例如::ng-deep .my-panel{ // ... }
    【解决方案2】:

    我通过在文件 ma​​terial2-app-theme.scss

    的末尾包含这个 css 块解决了这个问题
    .mat-dialog-container {
        overflow: hidden !important;
        border-radius: 5px !important;
        padding: 5px !important;
    }
    

    【讨论】:

      【解决方案3】:

      你可以使用 css 然后在 mat dilog 中更改背景吗,我使用了颜色透明

       mat-dialog-container {
          padding: 0px !important;
          background: transparent !important;
        }
      

      【讨论】:

      • 只有代码的答案通常被认为是低质量的。您能否为您的答案添加解释。
      猜你喜欢
      • 2021-11-28
      • 2017-10-02
      • 2014-10-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2019-08-30
      • 2014-12-14
      • 2017-05-25
      相关资源
      最近更新 更多