Skip to content

Commit 5729654

Browse files
committed
docs: Use CSS selectors with caution.
1 parent 5785f24 commit 5729654

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,37 @@ context.emitter.on("destroy", () => {
120120
});
121121
```
122122

123+
### Use CSS selectors with caution.
124+
125+
In netless-app, any custom CSS you add will eventually be mounted into the global page context. If you use overly broad selectors (e.g., div, html, *, etc.), you will likely cause style pollution that affects other unrelated elements on the page.
126+
127+
To avoid this problem, follow these principles:
128+
129+
#### Avoid global wildcard or tag selectors
130+
Do not directly define styles using selectors like div, span, ul, html, body, or *. Doing so will override all matching elements across the entire page.
131+
132+
#### Use named scoping (recommended)
133+
Define a unique root class name for your component or application (e.g., .my-whiteboard-extension) and nest all your styles under that class. This ensures that styles only apply to the area you intend.
134+
135+
```css
136+
/* Good */
137+
.my-whiteboard-extension .toolbar {
138+
background: #f5f5f5;
139+
}
140+
141+
/* Bad */
142+
.toolbar {
143+
background: #f5f5f5;
144+
}
145+
```
146+
#### Prefer CSS Modules or CSS-in-JS
147+
If your project supports them, consider using CSS Modules, styled‑components, or similar solutions. They automatically generate locally scoped class names, fundamentally avoiding conflicts.
148+
149+
#### For dynamically inserted styles, use Shadow DOM or scoping utilities
150+
If you cannot avoid global styles, leverage @scope (experimental in modern browsers), the scoped attribute (deprecated), or a third‑party scoping library to limit the impact.
151+
152+
Following these guidelines will help you present your custom styles correctly while maintaining style stability across the entire page.
153+
123154
## License
124155

125156
The MIT license.

0 commit comments

Comments
 (0)