CSS Portal

:scope CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

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

<div class="light-theme">
<section class="card">
<h2>Light Card</h2>
<p>This card is scoped to the light theme.</p>
</section>
</div>

<div class="dark-theme">
<section class="card">
<h2>Dark Card</h2>
<p>This card is scoped to the dark theme.</p>
</section>
</div>
/* Styling for the light theme block */
@scope (.light-theme) {
/* :scope refers to the .light-theme div itself */
:scope {
background-color: #f0f0f0;
padding: 20px;
border-radius: 8px;
}

/* Targets the h2 inside the light-theme scope */
h2 {
color: #333;
}
}

/* Styling for the dark theme block */
@scope (.dark-theme) {
/* :scope refers to the .dark-theme div itself */
:scope {
background-color: #333;
color: white;
padding: 20px;
border-radius: 8px;
}

/* Targets the h2 inside the dark-theme scope */
h2 {
color: #ffcc00;
}
}

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
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!