Repository navigation
Milestone
Description
Feature Request
On discord, someone reported a general solution to resetting the few CSS properties that leak into shadow roots.
issue seemed to be solved for me, pixel perfect matching attained across websites including reddit, we just have to reset just those css properties that inherit value's by default. read here https://web.dev/learn/css/inheritance#the_initial_keyword
import "./style.css"; import ReactDOM from "react-dom/client"; import App from "./App.tsx"; const styles = ` azimuth: initial; border-collapse: initial; border-spacing: initial; caption-side: initial; color: initial; cursor: initial; direction: initial; empty-cells: initial; font-family: initial; font-size: initial; font-style: initial; font-variant: initial; font-weight: initial; font: initial; letter-spacing: initial; line-height: initial; list-style-image: initial; list-style-position: initial; list-style-type: initial; list-style: initial; orphans: initial; quotes: initial; text-align: initial; text-indent: initial; text-transform: initial; visibility: initial; white-space: initial; widows: initial; word-spacing: initial; z-index: 9999; overflow: visible; position: relative; width: 0px; height: 0px; display: block; `.trim(); export default defineContentScript({ matches: [], registration: "runtime", cssInjectionMode: "ui", async main(ctx) { const ui = await createShadowRootUi(ctx, { name: "zinc-app-root", position: "modal", zIndex: 9999, anchor: "body", append: "last", onMount: (container, shadow, host) => { console.log({ container, shadow, host }); host.style.cssText = styles; // Don't mount react app directly on <body> const wrapper = document.createElement("div"); container.append(wrapper); const root = ReactDOM.createRoot(wrapper); root.render(<App />); return { root, wrapper }; }, onRemove: (elements) => { elements?.root.unmount(); elements?.wrapper.remove(); }, }); ui.mount(); }, });
Is your feature request related to a bug?
What are the alternatives?
Manually setting the value of each style.
Additional context
Gonna get this added in v0.20 as a breaking change if it works.
Metadata
Metadata
Assignees
Labels
No labels
Activity
molvqingtai commentedon Nov 23, 2024
I have a better solution:
all: initial. It resets the styles of all elements except forfont-size rem.This is my application in an actual project, and it works well on Reddit when used with
postcss-rem-to-responsive-pixel.https://github.com/molvqingtai/WebChat/blob/a7804af09fa56e5cb3c33bc5e1b902ffd0c632ea/src/assets/styles/tailwind.css#L82
Reference:https://open-wc.org/guides/knowledge/styling/styles-piercing-shadow-dom/
aklinker1 commentedon Nov 23, 2024
Great links, thanks!
I mentioned
all: initialin discord, had tried it in the past, but it didn't work. Was gonna try it again while looking into this, I probably did something wrong. One of the links does mention a warning about it:Warning
note that setting
all: initial;will not reset CSS custom properties. If for some reason you find yourself needing to reset a specific property, you must specify it explicitly.Not sure what are considered "custom" properties in this case, but something to try and figure out.
aklinker1 commentedon Dec 11, 2024
Yeah, so
all: initialon the host element doesn't do anything, and neither does setting individual properties toinitial, likevisibility: initial.@molvqingtai In your case,
all: initialis doing litterally nothing, it's the@applyabove that's doing all the resetting:https://github.com/molvqingtai/WebChat/blob/a7804af09fa56e5cb3c33bc5e1b902ffd0c632ea/src/assets/styles/tailwind.css#L79C5-L79C64
However, I was able to find something that worked: Set
all: initialon thebodyinside the shadow root:Can confirm it properly reset's Reddit's
visibilityoverride, and it reset's Youtube's smaller font size! However, this doesn't work for Firefox. Firefox throws an error when settingshadow.adoptedStyleSheets... Will test more to find a solution for Firefox.aklinker1 commentedon Dec 11, 2024
OK, this works for Chrome and Firefox:
I could have sworn I tried this in the past...
aklinker1 commentedon Dec 11, 2024
OK, so yeah, resets everything but REM units. We can leave that up to the postcss plugins.
Also, order doesn't matter - I can add the style block before or after the content script's CSS 🤔
aklinker1 commentedon Mar 28, 2025
Released in v0.20.0
namuorg commentedon Apr 17, 2025
Is there a reason why
all: initialis set onbodyrather than:host?:hostseems like the logical choice (and is also what's used in the example that's referenced above and referenced in the docs) so I'm curious if this was an intentional decision and if there are any gotchas I should be aware of. Thanks!aklinker1 commentedon Apr 19, 2025
@namukang See #1195 (comment) and the comment right after it
namuorg commentedon Apr 19, 2025
@aklinker1 The following reset works across all sites (it's what I've been using across all my extension before adopting WXT):
(The
!importantis required to override inline styles, which is probably the reason why the previous attempt didn't work on Reddit.)For most users, they probably won't notice or care whether the reset is applied on
:hostorbody, but in case you ever revisit this, the reset being applied tobodywas a gotcha for me when I was migrating an existing extension to WXT because it's common to apply style resets to the host element and not elements inside the shadow DOM. (In my case, I was setting thefont-familyandfont-sizeon the host element and was confused why they stopped working when switching to WXT.)aklinker1 commentedon Apr 24, 2025
@namukang I need to revisit this at some point and figure out why that exact CSS didn't work for me then.
namuorg commentedon Oct 9, 2025
Note for posterity: The reset was changed from
bodyto:hostin v0.20.7: https://github.com/wxt-dev/wxt/blob/main/packages/wxt/CHANGELOG.md#-fixes-3Commit: da5cd325