Skip to main content

Pseudo-Structural Classes

Pseudo-structural classes are used to target elements based on their position in the DOM tree. They allow you to select elements based on their relationship with other elements, such as their parent, sibling, or child elements.

Quick Reference

SQLCSSDescriptionSQL Alternative
CHILD(ROOT):rootThe root of the document
CHILD(2N+1):nth-child(n)The n-th child of its parentCHILD(FIRST, 2N+1)
CHILD(LAST, 2N+1):nth-last-child(n)The n-th child of its parent, counting from the last one
CHILD(2N+1) AS TYPEOF:nth-of-type(n)The n-th sibling of its typeCHILD(FIRST, 2N+1) AS TYPEOF
CHILD(LAST, 2N+1) AS TYPEOF:nth-last-of-type(n)The n-th sibling of its type, counting from the last one
CHILD():first-childThe first child of its parentCHILD(FIRST)
CHILD(LAST):last-childThe last child of its parent
CHILD() AS TYPEOF:first-of-typeThe first sibling of its typeCHILD(FIRST) AS TYPEOF
CHILD(LAST) AS TYPEOF:last-of-typeThe last sibling of its type
CHILD(ONLY):only-childThe only child of its parent
CHILD(ONLY) AS TYPEOF:only-of-typeThe only sibling of its type
CHILD(EMPTY):emptyAn element that has no children (including text nodes)

The CHILD Function

The CHILD function controls all aspects of the pseudo-structural selectors. It can be used in various flavours to target different elements based on their position in the DOM tree. However there are only three main 'overloaded' functions that can be used:

  • CHILD(<location-keyword>)
  • CHILD(<location-keyword>, <nth-expression>)
  • CHILD(<keyword>)

Arguments Quick Reference

ArgumentRequiredDefault ValueAllowed Keywords
<location-keyword>FIRSTFIRST, LAST
<nth-expression>ODD, EVEN, 2N+1 - the nth expression
<keyword>ONLY, EMPTY, ROOT

Location Keyword

The location keyword specifies the position of the element in relation to its parent. It can be one of the following:

  • FIRST: The first child of its parent.
  • LAST: The last child of its parent.

Nth Expression

The nth expression is used to target elements based on their position in the DOM tree. Please refer to the W3C documentation for more information on the nth expression.

It can be one of the following:

  • an+b: Selects every nth element.
  • ODD: Selects every odd element.
  • EVEN: Selects every even element.

Keyword

The keyword specifies the type of element to target. It can be one of the following:

  • ONLY: The only child of its parent.
  • EMPTY: An element that has no children (including text nodes).
  • ROOT: The root of the document.

The TYPEOF Function

The TYPEOF function is used to target elements based on their type. It can be used in conjunction with the CHILD function to target elements based on their position and type. In this case the the notation CHILD() AS TYPEOF is used to target the first sibling of its type.

In short, use AS TYPEOF to target the specified element type. This will handle all CSS selectors where of-type is used.

Here's an example to illustrate:

SELECT * FROM DOM WHERE
TAG('p') AND CHILD(LAST) AS TYPEOF
info

In a effort to reduce visual clutter, the TYPEOF function does not require you to specify parentheses () when used in conjunction with the CHILD function. You may specify the parentheses if you wish, but they are not required.