问题描述
我正在尝试使我的材质对话框在右上角有一个X按钮,但是我在定位上遇到了问题.
I am trying to get my material dialog to have an X button at the top right, but I am having problems with the positioning.
component.ts
component.ts
this.d.open(loginComponent, {
width: '300px',
height: '',
panelClass: 'dialogC',
});
component.html
component.html
<mat-dialog-content>
<button mat-button class="close-icon" [mat-dialog-close]="true">
<mat-icon>close</mat-icon>
</button>
<h2 mat-dialog-title>Login</h2>
style.scss
style.scss
.dialogC {
position: relative !important;
}
.close-icon {
position: absolute;
top: 0;
right: 0;
transform: translate(50%, -50%);
}
X只是左对齐,而不是右对齐.有建议吗?
The X is just aligned to the left instead of top right. Suggestions?
更新,这是添加flex之后出现的问题:
推荐答案
<button class="close" mat-button (click)="onNoClick()">X</button>
<h1 mat-dialog-title>Login</h1>
<div mat-dialog-content>
...
...
</div>
CSS:(请以全局( styles.css )形式提供,或提供ViewEncapsulation.NONE
,否则这些样式将不受影响.)
CSS: (Please give it in global (styles.css) or give ViewEncapsulation.NONE
or else these styles wont affect.)
.cdk-overlay-pane.my-dialog {
position: relative!important;
}
.close.mat-button {
position: absolute;
top: 0;
right: 0;
padding: 5px;
line-height: 14px;
min-width: auto;
}
请注意,在CSS中,我们现在有了一个新的类 .my-dialog
Note that in the CSS We now have a new class out of thin air .my-dialog
因此,请在dialogRef
中像下面的panelClass
所述,
So, please mention that as panelClass
in dialogRef
like below,
this.dialog.open(DialogComponent, {
width: '250px',
panelClass: 'my-dialog',
..
..
这会给我带来以下结果,
This yields me the following result,
这篇关于角8材质对话框关闭按钮,X右上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!