What is shadow root
This is a special indicator that a Shadow DOM exists. These have existed for years, but developers have never been given APIs into it until recently. Chrome has had this functionality for a while, other browsers are still catching up. It can be toggled in the DevTools Settings under the "Elements" section. Uncheck the "Show User Agent Shadow DOM". This will at least hide away any Shadow DOMs created internally (like select elements.) I am unsure right away if it affects user-created ones, such as custom elements.
These come up in things like iframes as well, where you have a separate DOM tree nested inside of another.
The Shadow DOM is simply saying that some part of the page, has its own DOM within it. Styles and scripting can be scoped within that element so what runs in it only executes in that boundary.
This is one of the primary pieces needed for Web Components to work. Which is a new technology allowing developers to build their own encapsulated components that developers can use just like any other HTML element.
What is DOMElement.shadowRoot?
It's a container for the "internal DOM" (i.e. the shadow DOM) of native elements such as <input>
or <button>
(or custom elements made with the web components APIs).
When you inspect <input>
you only see the <input>
itself:
But you can turn on a flag in the Chrome Dev Tools to enable the visibility of the shadow DOM:
You can now see how the <input>
is "implemented" by the browser. For example the placeholder is contained within a <div>
element:
Why is #shadow-root in my document and why is it overriding my CSS
A lot of chrome plugins automatically create this shadow root in your inspector. For example, ever since I downloaded Vimium, I've had a shadowroot div at the bottom of any page I've opened in chrome. It's nothing to worry about.
Adding CSS to shadow-root element wont work
To make your styles work in a Shadow DOM, you should use :host([selector])
:
:host(.test ) {
border: 1px solid red;
}
The :host
selector allows to select the shadow host (the element containing the shadow tree). As a general rule, local styles work only inside the shadow tree, and document styles work outside of it. But there are few exceptions. A shadow host is a DOM node that contains a shadow root. It is a regular element node within the parent page that hosts the scoped shadow subtree. Any child nodes that reside under the shadow host are still selectable, with the exception of the shadow root.
Cypress shadow-root inside of shadow-root
If you're using Cypress v10, then the configuration is in the file cypress.config.js
and the format for setting global shadow enabling is
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
...
},
...
includeShadowDom: true
})
Or use test-specific configuration
it('tests some shadow dom elements', {includeShadowDom: true}, () => {
...
})
Related Topics
Highlight the Navigation Menu for the Current Page
Get Rid of Space Underneath Inline-Block Image
How to Make Div's Percentage Width Relative to Parent Div and Not Viewport
Difference Between Position:Sticky and Position:Fixed
Why Does Vw Include the Scrollbar as Part of the Viewport
Html5 - Mp4 Video Does Not Play in IE9
Create a Slanted Edge to a Div
Do I Need a "/" at the End of an <Img> or <Br> Tag, etc.
Is It Bad to Use Uppercase Letters for HTML Tags
Issue with CSS Media Queries(Scrollbar)
How to Set a Stroke-Width:1 on Only Certain Sides of Svg Shapes
How to Pass a Parameter to HTML
Making a Div That Covers the Entire Page
How to Add a Font Awesome Icon to Input Field
Disable Form Autofill in Chrome Without Disabling Autocomplete