最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to apply styling to shadow root element in angular - Stack Overflow

programmeradmin2浏览0评论

I am using angular 12 and I want to apply some styling to input field which is a shadow root element of group-search-field element.

I tried using ng-deep like below but it didn't work

:host ::ng-deep input {
  background: red !important;
}

I am using angular 12 and I want to apply some styling to input field which is a shadow root element of group-search-field element.

I tried using ng-deep like below but it didn't work

:host ::ng-deep input {
  background: red !important;
}
Can someone guide me for this ?

Share Improve this question edited Mar 30, 2022 at 16:38 SamD asked Mar 30, 2022 at 15:56 SamDSamD 3371 gold badge7 silver badges25 bronze badges 7
  • This answer may help you: stackoverflow./questions/46786986/… – user11913484 Commented Mar 30, 2022 at 16:55
  • @Nick tried like this .search-field ::ng-deep input { background-color: red !important; } but did not work – SamD Commented Mar 30, 2022 at 18:03
  • hard to figure out. in your example above (the image) i cannot find .search-field – user11913484 Commented Mar 30, 2022 at 18:09
  • Maybe: .search-field ::ng-deep form input { background-color: red !important; } is running fine – user11913484 Commented Mar 30, 2022 at 18:09
  • search-field is the class of group-search-field (it's not in above image but I added later) so I am using same in ng-deep reference. – SamD Commented Mar 31, 2022 at 2:47
 |  Show 2 more ments

1 Answer 1

Reset to default 3

Because of the isolation of styles, which is a feature of Shadow DOM, you cannot define a global CSS rule that will be applied in the Shadow DOM scope. In your case I think background:red does not propagate to be inherited by the elements in the shadow root that's why :host is not working.

super();
const shadowRoot = this.attachShadow({ mode: 'open' });
const sheet = new CSSStyleSheet;
sheet.replaceSync( `input { background: red !important; }`)
shadowRoot.adoptedStyleSheets = [ sheet ];

https://developer.mozilla/en-US/docs/Web/Web_Components/Using_shadow_DOM

发布评论

评论列表(0)

  1. 暂无评论