admin管理员组文章数量:1317898
I am trying to apply custom styles to a mat-icon element inside an Angular Material component, but no matter what I do, the styles won't take effect. Here's a simplified version of my code:
Html:
<mat-form-field appearance="fill" class="example-field">
<mat-label>Select an Option</mat-label>
<mat-select [formControl]="exampleControl" (selectionChange)="onOptionSelected($event.value)">
<mat-option *ngFor="let option of availableOptions" [value]="option">
<mat-icon [inline]="true">settings</mat-icon>
{{ "example.option." + option | lowercase | translate }}
</mat-option>
</mat-select>
</mat-form-field>
CSS:
:host {
::ng-deep {
mat-form-field {
mat-select {
mat-option {
mat-icon {
margin-right: 0;
}
}
}
}
}
}
I am trying to apply custom styles to a mat-icon element inside an Angular Material component, but no matter what I do, the styles won't take effect. Here's a simplified version of my code:
Html:
<mat-form-field appearance="fill" class="example-field">
<mat-label>Select an Option</mat-label>
<mat-select [formControl]="exampleControl" (selectionChange)="onOptionSelected($event.value)">
<mat-option *ngFor="let option of availableOptions" [value]="option">
<mat-icon [inline]="true">settings</mat-icon>
{{ "example.option." + option | lowercase | translate }}
</mat-option>
</mat-select>
</mat-form-field>
CSS:
:host {
::ng-deep {
mat-form-field {
mat-select {
mat-option {
mat-icon {
margin-right: 0;
}
}
}
}
}
}
Share
Improve this question
edited Jan 22 at 15:36
Naren Murali
58.8k5 gold badges44 silver badges77 bronze badges
asked Jan 22 at 15:26
Newsha NikNewsha Nik
9474 gold badges13 silver badges34 bronze badges
1 Answer
Reset to default 2The select dropdown get's attached to the root element and not inside the component. To overcome this you can use panelClass
to add a class to the dropdown root, and then style the inner contents.
HTML:
<h4>Basic mat-select</h4>
<mat-form-field>
<mat-label>Favorite Car</mat-label>
<mat-select [panelClass]="'custom-dropdown'">
@for (car of cars; track car) {
<mat-option [value]="car" [appTransform]="car">
<mat-icon [inline]="true">settings</mat-icon>
</mat-option>
}
</mat-select>
</mat-form-field>
From Global styles:
.custom-dropdown {
mat-option.mat-mdc-option {
mat-icon.mat-icon,
mat-icon.mat-pseudo-checkbox-full {
margin-right: 0;
}
}
}
Stackblitz Demo
From Component:
::ng-deep .custom-dropdown {
mat-option.mat-mdc-option {
mat-icon.mat-icon,
mat-icon.mat-pseudo-checkbox-full {
margin-right: 0;
}
}
}
Stackblitz Demo
本文标签: Unable to Apply CSS Styles to Angular Material maticon in Nested ComponentsStack Overflow
版权声明:本文标题:Unable to Apply CSS Styles to Angular Material mat-icon in Nested Components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742036409a2417318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论