From mdn pseudo classes :scope
The :scope CSS pseudo-class represents elements that are a reference point, or scope, for selectors to match against.
To my understanding a "custom" pseudo class for :scope can
- be created and used from javascript using the DOM Api
- can not be used in style rules
Is my understanding correct?
In the code exmple below i want to style links that are children of iamscoped with the use of a pseudo class
ul:first-child { font-size: 25px; color: red} /** <-- this works */
a:iamscoped { font-size: 35px; color: red} /** <-- this does not */
But to my understanding this is not possible and not intended since the scope will be created from javascript but does not exist yet for the style rendering.
function colorScope() {
const parentNode = document.getElementById("iamscoped");
// scope is now created using DOM Api
const spans = parentNode.querySelectorAll(":scope span");
spans.forEach((el) => {
el.style.color = "red";
el.style.fontSize = "2em"
});
}
a {color: black}
ul li:first-child { font-size: 20px; color: blue}
/** scope does not exist yet */
span:iamscoped { font-size: 35px; color: red}
span[xxuuas] {color: green; font-weight: bold; font-size:30px}
<div id="iamscoped">
<ul><li><span>scoped 1st list-item-child</span></li>
<li><span>scoped</span></li>
</ul>
<span xxuuas>I am super-special</span>
</div>
<div id="unsocped">
<ul>
<li><span>unscoped 1st list-item</span></li>
<li><span>unscoped span</span></li>
</ul>
<span xxuuas>I am super-special</span>
</div>
<button onclick="colorScope()">color scoped</button>
Is my understanding correct?
- The
scopepseudo class can only be used via DOM API - Can not be used inside a stylesheet
- Regarding
<span xxuuas>andspan[xxuuas] {...}this is somthing else - maybe named a scope identifier?
:scopeis the same as:root, as there is currently no way to explicitly establish a scoped element." So, no: (as yet) this exists in CSS for compatibility withdocument.querySelector()(and similar), but doesn't seem possible, or perhaps even relevant, within a stylesheet.