不能更改MatDialog的背景颜色

huangapple go评论66阅读模式
英文:

Can't change the background color of MatDialog

问题

我遇到了一些问题,无法更改对话框组件的背景颜色,并已经尝试了stackoverflow上类似帖子中的所有答案。

在styles.scss中,我有以下类:

.dialog-panel {
    background: red !important;
}

然后我使用这个类打开对话框:

const dialogRef = this.dialog.open(CreateOrEditTaskDialogComponent, {
    panelClass: 'dialog-panel'
});

问题是,打开对话框时它是红色的,但不到一秒钟就会恢复到默认颜色。在打开对话框后的截图中可以看到:

在红色“消失”后的对话框截图。如果你仔细看,甚至可以看到因为边框半径的缘故,在角落处有红色的背景。

看起来可能是“mat-mdc-dialog-surface mdc-dialog__surface”这个东西浮在最上面?但我在网上找不到太多相关信息。

--编辑--
我制作了一个示例来展示这个问题:https://stackblitz.com/edit/angular-kejni3?file=src%2Findex.html

英文:

I'm having some issues changing the background color of my dialog components, and have exhausted all the answers from similar posts here on stackoverflow.

In styles.scss i have the following class:

.dialog-panel {
    background: red !important;
}

And I open my using this class:

const dialogRef = this.dialog.open(CreateOrEditTaskDialogComponent, {
    panelClass: 'dialog-panel'
});

The problem is that upon opening the dialog it is red but within less than a second it fades back to default. Here is a screenshot right after opening the dialog:

And here is a screenshot of the dialog after the red had "faded". If you look closely you can even see the red background in the corners because of the border radius.

It looks like it might be "mat-mdc-dialog-surface mdc-dialog__surface" that is surfacing on top? But I could not find much about this online.

--EDIT--
I made this example that shows the issue: https://stackblitz.com/edit/angular-kejni3?file=src%2Findex.html

答案1

得分: 0

你可以像这样更改它:

.dialog-panel > mat-dialog-container{
  background-color: red;
}

这将改变对话框的背景颜色。如果你的对话框中有一个 div 并且它有颜色,你也应该自定义它的颜色。

英文:

you can change it like this:

.dialog-panel > mat-dialog-container{
  background-color: red;
}

this will change the color of the dialog. if you have a div in you dialog and it has color, you should customize its color too.

答案2

得分: 0

覆盖 Material Web 组件中负责着色对话框容器的 CSS 变量。

您可以直接在自定义面板类中执行此操作。

.custom-dialog-panel {
  --mdc-dialog-container-color: red;
}
英文:

Overwrite Material Web Component CSS var in charge of colouring dialog container.

You can do that directly in your custom panel class

.custom-dialog-panel {
  --mdc-dialog-container-color: red;
}
const dialogRef = this.dialog.open(CreateOrEditTaskDialogComponent, {
    panelClass: 'custom-dialog-panel'
});

答案3

得分: 0

你可以从 global-styles.css 中访问对话框表面属性。

.dialog-panel
  .mat-mdc-dialog-container
  .mdc-dialog__container
  .mat-mdc-dialog-surface {
  background-color: red;
}

这里 是一个可运行的示例。

英文:

You may access dialog surface property from global-styles.css

.dialog-panel
  .mat-mdc-dialog-container
  .mdc-dialog__container
  .mat-mdc-dialog-surface {
  background-color: red;
}

Here is a working example.

huangapple
  • 本文由 发表于 2023年5月13日 16:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76241891.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定