The :scope CSS pseudo-class represents elements that are a reference point, or scope, for selectors to match against.
/* Selects a scoped element */
:scope {
background-color: lime;
}
Which element(s) :scope matches depends on the context in which it is used:
- When used at the root level of a stylesheet,
:scopeis equivalent to:root, which in a regular HTML document matches the<html>element. - When used inside a
@scopeblock,:scopematches the block's defined scope root. It provides a way to apply styles to the root of the scope from inside the@scopeblock itself. - When used within a DOM API call — such as
querySelector(),querySelectorAll(),matches(), orElement.closest()—:scopematches the element on which the method was called.