> For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt.

# Selectors

CSS selectors specify which elements a CSS rule applies to. Pedantically speaking, CSS selectors is an umbrella term that includes Selectors, Combinators, Pseudo-elements, and Pseudo-classes.

## Selectors

### Type selector

The CSS type selector matches elements by node name. In other words, it selects all elements of the given type.

#### Syntax

```css
element { property declarations }
```

#### Example usage

```css
/* To match all <input> elements */
input {
  ...
}

```

#### Using `page` to select the root node

Unlike on the Web, in Lynx, the `page` element is used to select the root node instead of `body`.

### Class selector

The CSS class selector matches elements based on the contents of their class attribute.

#### Syntax

```css
.className { property declarations }
```

#### Example usage

```css
/* All elements with class="index" */
.index{
  ...
}
```

### Multiple class selectors

To select elements using multiple class names, specify that an element must contain all these classes by concatenating the class names together.

#### Syntax

```css
.className1.className2 { property declarations }

element.className1.className2 { property declarations }
```

#### Example usage

```css
/* To match any input elements with a class that contains both "a" and "b". */
input.a.b{
  ...
}

/* element */
<input className="a b" /> /* selected */
```

### ID selector

The CSS ID selector matches an element based on the value of the element's id attribute. In order for the element to be selected, its id attribute must match exactly the value given in the selector.

#### Syntax

```css
#idName {  property declarations }
```

### Universal selector

The CSS universal selector (\*) matches elements of any type.

#### Syntax

```css
* {  property declarations }
```

## Combinators

### Descendant combinator

e descendant combinator — typically represented by a single space (" ") character — combines two selectors such that elements matched by the second selector are selected if they have an ancestor (parent, parent's parent, parent's parent's parent, etc.) element matching the first selector. Selectors that utilize a descendant combinator are called descendant selectors.

**Compared to the old selector, it no longer restricts the levels of matched elements to two levels.**

#### Syntax

```css
selector1 selector2 selector3 {
  property declarations
}
```

#### Example usage

```css
/* Match all "b" elements that are descendants of the "a" list. */
view.a .b{
   ...
}

/* element */
<view className="a" >
 <text className="b">hello</text> /* selected */
</view>
```

### Child combinator

The child combinator (`>`) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first.

#### Syntax

```css
element1 > element2 {
  property declarations
}
```

#### Example usage

```css
/* Match all text elements that are direct children of "a" view */
view.a > text {
  ...
}

/* element */
<view className="a" >
  <text>hello</text> /*selected*/
</view>
```

### Subsequent-sibling combinator

The subsequent-sibling combinator (`~`, a tilde) separates two selectors and matches all instances of the second element that follow the first element (not necessarily immediately) and share the same parent element.

#### Syntax

```css
former_element ~ target_element { property declarations }
```

#### Example usage

```css
/* Match all sibling text elements following view. */
view ~ text {
  ...
}

/* element */
<view className="a" />
<text />  /* selected */
...
<text />  /* selected */
```

### Next-sibling combinator

The next-sibling combinator (`+`) separates two selectors and matches the second element only if it immediately follows the first element, and both are children of the same parent element.

#### Syntax

```css
former_element + target_element { property declarations }
```

#### Example usage

```css
/*  Match the text element that come immediately after a view */
view + text {
  ...
}

/* element */
<view className="a" />
<text />  /*selected*/
<text />  /*not selected*/
```

## Selector list

The CSS selector list (`,`) selects all the matching nodes. A selector list is a comma-separated list of selectors.

### Syntax

```css
selector1,selector2 { property declarations }
```

### Example usage

```css
/* All matching elements are selected. */
 selector1, selector2 {
  ...
}

/* Equivalent to */

selector1 {
  ...
}

selector2 {
  ...
}
```

## Pseudo-classes

A `:` pseudo-class is a type of selector used to select elements in a specific state.

Supports

- `:active`
- `:hover`
- `:not()`
- `:root`

### `:not()`

Used to match elements that do not meet the conditions. Usage aligns with [W3C :not()](https://developer.mozilla.org/zh-CN/docs/Web/CSS/:not)

##### Example usage

```css
/* Elements that are not <view> and not <text> elements */
root :not(view):not(text) {
  font-weight: bold;
}

/* Elements that are not .crazy or .fancy
   Note that this syntax is not well supported yet. */
body :not(.crazy, .fancy) {
  font-family: sans-serif;
}
```

### `:root` \{#root-selector}

Similar to the `page` selector used to match the root node. Usage aligns with [W3C :root](https://developer.mozilla.org/zh-CN/docs/Web/CSS/:root)

### `:active`

When a node is pressed, it gets activated and selected. Releasing the finger deactivates it. Usage aligns with [W3C :active](https://developer.mozilla.org/zh-CN/docs/Web/CSS/:active)

### `:hover`

APISummary
When a pointing device hovers over a node, the node matches `:hover`. Usage aligns with [W3C `:hover`](https://developer.mozilla.org/en-US/docs/Web/CSS/:hover).

On native platforms, `:hover` is supported by Clay on macOS and Windows since Lynx 3.7. Clay on Android supports it since Lynx 3.7 in full Clay builds when the host injects pointer hover events; Clay Lite does not support it. The classic Android, iOS, and HarmonyOS renderers and Clay on iOS do not support it. Web Lynx uses the browser's `:hover` implementation.

Focus navigation with a keyboard or game controller does not generate pointer hover events and therefore does not activate `:hover`.

**Compatibility Table**

**Query:** `css.selectors.hover`

[Source](https://github.com/lynx-family/lynx-website/tree/main/packages/lynx-compat-data/css/selectors.json) · [MDN](https://developer.mozilla.org/en-US/docs/Web/CSS/:hover)

| Feature | Android | HarmonyOS | iOS | macOS | Windows | Web |
| --- | --- | --- | --- | --- | --- | --- |
| hover | No | No | No | Yes (3.7) | Yes (3.7) | Yes |

**`hover`**

The :hover pseudo-class matches a node while a pointing device is hovering over it.


## Pseudo-elements

Pseudo-elements style a specific part of an element. Support depends on the element and its implementation.

### `::placeholder`

Since Lynx SDK 3.4, [`<input>`](/next/api/elements/built-in/input.md) and [`<textarea>`](/next/api/elements/built-in/textarea.md) support `::placeholder` to style their placeholder text.

For a `<textarea>` with class `textarea` and an `<input>` with class `input`:

```css
.textarea::placeholder {
  color: orange;
}

.input::placeholder {
  color: red;
}
```

For a usage example, see [Custom placeholder style](/next/api/elements/built-in/textarea.md#custom-placeholder-style).

### `::selection`

Since Lynx SDK 3.2, Android and iOS support `::selection` to style selected text in [`<text>`](/next/api/elements/built-in/text.md). Supported properties include `background-color` for the highlight background, and `-x-handle-size` and `-x-handle-color` for the selection handles.

Apply this style to a `<text>` element with text selection enabled and flattening disabled.

```css
::selection {
  background-color: rgba(255, 255, 0, 0.15);
  -x-handle-color: orange;
  -x-handle-size: 12px;
}
```

For complete examples, see [`-x-handle-color`](/next/api/css/properties/-x-handle-color.md#examples) and [`-x-handle-size`](/next/api/css/properties/-x-handle-size.md#examples).

## Troubleshooting

### `!important` support

By default, Lynx does not support `!important`. However, **since engineVersion 3.9**, you can enable it by setting the `engineVersion` to `'3.9'` or higher in your plugin options.

When enabled, `!important` gives a declaration higher priority than non-`!important` ones. If multiple `!important` declarations apply to the same element, the winner is still determined by specificity and source order. For more details, refer to the [MDN documentation on !important](https://developer.mozilla.org/en-US/docs/Web/CSS/important).

![](https://lf-lynx.tiktok-cdns.com/obj/lynx-artifacts-oss-sg/lynx-website/assets/doc/css-priority.png)