:scope CSS Pseudo Class
Description
The :scope pseudo-class in CSS represents the element that is the reference point for a set of selectors, essentially allowing you to target the “current element” in a scoped context. Unlike most selectors, which select children, descendants, or siblings relative to the document or parent elements, :scope ensures that the selection is explicitly anchored to the element you are currently styling or querying. This is particularly useful when working with JavaScript methods like querySelectorAll, where selection can sometimes unintentionally target nested elements instead of the intended container.
One of the main uses of :scope is in combination with child combinators or complex selectors. For example, if you want to select only the direct children of a container without affecting deeper nested elements, you can write:
<ul class="menu">
<li>Home</li>
<li>About
<ul>
<li>Team</li>
</ul>
</li>
<li>Contact</li>
</ul>
.menu > :scope > li {
color: blue;
}
In this example, :scope ensures that the selector targets only the top-level li elements that are direct children of .menu, preventing styling from cascading into the nested <ul> elements.
Another practical application is in modular components or shadow DOM contexts. By using :scope, you can make CSS more predictable and avoid unintentionally selecting elements outside the intended container. For instance, when styling dropdowns or accordions, :scope can isolate your CSS rules to only the top-level container without affecting nested subcomponents.
It’s important to note that :scope is not a substitute for selectors like child combinator or descendant combinator, but rather a tool to clarify the reference point of your selector. Combining :scope with these combinators gives precise control over element targeting, improving maintainability and preventing unexpected styling conflicts in larger documents.
A concise JavaScript example demonstrates its use in DOM queries:
const container = document.querySelector('.menu');
const topItems = container.querySelectorAll(':scope > li');
console.log(topItems); // Logs only the top-level li elements
This highlights the power of :scope to ensure that queries and styles are explicitly scoped to the intended element, reducing ambiguity and enhancing code readability.
Syntax
:scope {
/* ... */
}
Example
Browser Support
The following information will show you the current browser support for the CSS :scope pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.
This psuedo class is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 31st December 2025
