CSS-10Recommended
Fence each component's styles in
Make every selector in a component file start at the component's root. Use the module prefix on each class, or wrap the file in @scope on the root class.
How to do it
A prefix such as .booking-form- keeps names apart. @scope adds a lower boundary, so a component's styles stop at a slot that holds another component. Use one of the two methods across the whole project, not both.
Examples
CSS example
/* Prefix method */
.booking-form-field { }
/* @scope method: styles stop at the slot that holds another component */
@scope (.booking-form) to (.booking-form-slot) {
:scope { }
.booking-form-label { }
}
HTML example
<form class="booking-form">
<label class="booking-form-label">Guests <input name="guests"></label>
<div class="booking-form-slot"><!-- another component, untouched --></div>
</form>
How to check
Every selector in a component file starts with that file's prefix, or the file's only top-level rule is @scope (.<prefix>).
Sources
Related rules
- APP-4 Change components through variants in Web app interface
- CSS-2 Sort styles into five layers in HTML and CSS
- CSS-13 Name classes for what the element is in HTML and CSS
- CSS-28 Check the built CSS for limits and unused selectors in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/the-cascade/scope-component-styles">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.