Components can be customized through the use of CSS parts to target individual components.
CSS parts offer a low-level way to customize individual components. Again, this is done with pure CSS — no preprocessor required. Our components use a shadow DOM to encapsulate their styles and behaviors. As a result, you can’t simply target their internals with the usual CSS selectors. Instead, components expose “parts” that can be targeted with the CSS part selector , or ::part().
Here’s an example that modifies the tag component with custom styling.
At first glance, this approach might seem a bit verbose or even limiting, but it comes with a few important advantages:
Customizations can be made to components with explicit selectors, such as ::part(icon), rather than implicit selectors, such as .button > div > span + .icon, that are much more fragile.
The internal structure of a component will likely change as it evolves. By exposing CSS parts through an API, the internals can be reworked without fear of breaking customizations as long as its parts remain intact.
It encourages us to think more about how components are designed and how customizations should be allowed before users can take advantage of them. Once we opt a part into the component’s API, it’s guaranteed to be supported and can’t be removed until a major version of the library is released.
Most (but not all) components expose parts. You can find them in each component’s API documentation under the “CSS Parts” section.