|
| 1 | +# Selectors 4 Explainer |
| 2 | + |
| 3 | +## Author |
| 4 | + |
| 5 | + - Chris Lilley (W3C) |
| 6 | + |
| 7 | +## Participate |
| 8 | + |
| 9 | +- [Issue tracker](https://github.com/w3c/csswg-drafts/issues?q=state%3Aopen%20label%3A%22selectors-4%22) |
| 10 | + |
| 11 | +## What are Selectors |
| 12 | + |
| 13 | +Introduced with [CSS1](https://www.w3.org/TR/CSS1/#basic-concepts) in 1996, |
| 14 | +Selectors are a mature and widely deployed way to |
| 15 | +select **individual parts** of a (typically HTML) document, |
| 16 | +so that they may be **styled** or **manipulated with script**. |
| 17 | + |
| 18 | +They were further developed in [CSS2.1](https://www.w3.org/TR/CSS2/selector.html#q5.0) |
| 19 | +(W3C Recommendation, from 2011) |
| 20 | +and in [Selectors 3](https://www.w3.org/TR/selectors-3/) |
| 21 | +(W3C Recommendation, 2018). |
| 22 | + |
| 23 | +[Selectors 4](https://www.w3.org/TR/selectors-4/) |
| 24 | +is an incremental improvement to previous Selectors specifications, |
| 25 | +responding to common user needs. |
| 26 | + |
| 27 | +It is now widely deployed, stable, |
| 28 | +and is being prepared for W3C Recommendation, |
| 29 | +while less-mature Selectors have been moved to |
| 30 | +[Selectors 5](https://www.w3.org/TR/selectors-5/). |
| 31 | + |
| 32 | +However, the Selectors 4 specification |
| 33 | +is a _complete specification_ of Selectors, |
| 34 | +(including material from levels 1 through 3) |
| 35 | +not a delta. This aids readability. |
| 36 | + |
| 37 | +In view of this widespread use, |
| 38 | +this explainer does not address early-review questions |
| 39 | +such as general user need or alternatives considered; |
| 40 | +instead it motivates the particular changes found in Selectors 4. |
| 41 | + |
| 42 | +## Why `Selectors` not `CSS Selectors`? |
| 43 | + |
| 44 | +Selectors have expanded beyond their initial use in CSS; |
| 45 | +they are also widely used in the Document Object Model, |
| 46 | +to select parts of a document for manipulation by JavaScript. |
| 47 | +These are both majority uses, hence the naming of the specification. |
| 48 | + |
| 49 | +## What is new in Selectors 4 |
| 50 | + |
| 51 | +(Expanded from [Changes Since Level 3](https://drafts.csswg.org/selectors-4/#changes-level-3)) |
| 52 | + |
| 53 | +### Conditional Logic |
| 54 | + |
| 55 | +- The matches-any [`:is`](https://www.w3.org/TR/selectors-4/#matches) pseudo-class, |
| 56 | + formerly called `:matches()`, |
| 57 | + has been added to allow stylesheet authors to group functionally related elements, |
| 58 | + such as any element that is either hovered or focussed. |
| 59 | +- The specificity-adjusting [`:where()`](https://www.w3.org/TR/selectors-4/#zero-matches) pseudo-class |
| 60 | + has been added to construct zero-specificity filters, |
| 61 | + which are easy to override, |
| 62 | + replacing the many ugly hacks previously required to simulate this |
| 63 | +- The relational [`:has()`]() pseudo-class |
| 64 | + |
| 65 | +### Interactivity and Forms |
| 66 | + |
| 67 | +- Added the [`:modal`](https://www.w3.org/TR/selectors-4/#modal-state) pseudo-class, |
| 68 | + to select elements which exclude interaction with all other elements until dismissed |
| 69 | +- Added [`:open`](https://www.w3.org/TR/selectors-4/#open-state) |
| 70 | + and [`:popover-open`](https://www.w3.org/TR/selectors-4/#popover-open-state) pseudo-classes, |
| 71 | + primarily but not exclusively to use with HTML forms |
| 72 | + |
| 73 | +### Hyperlinking Support |
| 74 | + |
| 75 | +- Added [`:any-link`](https://www.w3.org/TR/selectors-4/#the-any-link-pseudo) to match |
| 76 | + any element that acts as the source anchor of a hyperlink |
| 77 | + |
| 78 | +### Clearer Expression of Authorial Intent |
| 79 | + |
| 80 | +- Expanded the sibling-counting [`:nth-child()`](https://www.w3.org/TR/selectors-4/#the-nth-child-pseudo) |
| 81 | + and negation [`:not()`](https://www.w3.org/TR/selectors-4/#negation) pseudo-classes |
| 82 | + to accept a **selector list**. |
| 83 | + |
| 84 | +### Media State |
| 85 | + |
| 86 | +- Added the `:playing`, `:paused` and `:seeking` [media playback state](https://www.w3.org/TR/selectors-4/#video-state) pseudo classes, |
| 87 | + to enable selecting on the dynamic state of video elements, |
| 88 | + functionality which previously required JavaScript |
| 89 | +- Added the `:buffered` and `:stalled` |
| 90 | + [media loading state](https://www.w3.org/TR/selectors-4/#media-loading-state) pseudo-classes, |
| 91 | + again providing functionality that previously needed JavaScript |
| 92 | +- Added the [`:fullscreen`](https://www.w3.org/TR/selectors-4/#fullscreen-state) |
| 93 | + and [`:picture-in-picture`](https://www.w3.org/TR/selectors-4/#pip-state) pseudo-classes |
| 94 | +- Added the `:muted` and `volume-locked` pseudo-classes |
| 95 | + for sound-producing elements |
| 96 | + |
| 97 | +### Internationalization Improvements |
| 98 | + |
| 99 | +- Expanded the content language |
| 100 | + [`:lang()`](https://www.w3.org/TR/selectors-4/#the-lang-pseudo) pseudo-class |
| 101 | + to accept **wildcard matching** and **lists of language codes** |
| 102 | + for greater flexibility styling related languages |
| 103 | +- Added the content language writing direction |
| 104 | + [`dir()`](https://www.w3.org/TR/selectors-4/#the-dir-pseudo) pseudo-class |
| 105 | + for convenient styling of left-to-right and righ-to-left content |
| 106 | +- Clarified case-insensitive string matching |
| 107 | + |
| 108 | +### Consolidation |
| 109 | + |
| 110 | +Selectors first introduced in |
| 111 | +[CSS Basic UI 3](https://www.w3.org/TR/css-ui-3/), |
| 112 | +have been moved to the main Selectors 4 specification. |
| 113 | + |
| 114 | +## Further reading |
| 115 | + |
| 116 | +For further information on the use of Selectors in CSS |
| 117 | +(covering all levels, but with a focus on level 4), see |
| 118 | +[CSS Selectors](https://css-tricks.com/css-selectors/) on CSS-Tricks. |
| 119 | + |
| 120 | +For selectors in the DOM, see |
| 121 | +[NodeList](https://developer.mozilla.org/en-US/docs/Web/API/NodeList) on MDN. |
| 122 | + |
| 123 | +The [Horizontal Review of Selectors 4](https://github.com/w3c/csswg-drafts/issues/13469). |
0 commit comments