Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

:scope — the selector’s reference element

The :scope pseudo-class identifies a selector’s scoping root. For example, it lets you select direct children of the element on which querySelectorAll() is called.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

:scope

:scope is a pseudo-class without arguments that represents the scoping root established by the context. In panel.querySelectorAll(), that reference element is panel, not each descendant being tested.

The selector :scope > .item selects direct children with class item; :scope .item also includes deeper descendants. In an ordinary document stylesheet without a separate scope, :scope corresponds to :root. Using this pseudo-class alone does not create a scope or isolate styles.

Examples

const panel = document.createElement('section');
panel.innerHTML = '<p class="item">A</p><div><p class="item">B</p></div>';

const direct = panel.querySelectorAll(':scope > .item');
const nested = panel.querySelectorAll(':scope .item');

console.log(JSON.stringify({
  direct: Array.from(direct, node => node.textContent),
  nested: Array.from(nested, node => node.textContent)
}));

Result

{"direct":["A"],"nested":["A","B"]}

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources