HTML has recently been added a part attribute plus corresponding CSS selector.
What's the difference between part and class? What can I do with the part attribute that I can't archive using the class attribute?
HTML has recently been added a part attribute plus corresponding CSS selector.
What's the difference between part and class? What can I do with the part attribute that I can't archive using the class attribute?
Copyright © 2021 Jogjafile Inc.
::part()is much more limited in a sense where you can only do compound selector (selecting element that contains the parts you specified). This is also part of the design decision to not expose the layer structures inside web component.In other words, if you are comparing it with normal CSS classes, the only type that it supports is:
.class1or.class1.class2. Even if you try to use::part(parent)::part(children), it will not match.parent .children.By default, styles inside shadow DOM cannot be customised by outside of shadow tree unless you have some custom props / use JavaScript to workaround.
However, with the use of
partand::part(), you can now override the styles of your Web Component. You can specifically specify which part of your Web Component (usingpart) that is intended to be override by the user.This is intended to be used for people who are using your component, not so much for your styling inside your Web Component. Example below illustrated the behaviour: