> For the complete documentation index, see [llms.txt](https://docs.zerowork.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zerowork.io/using-zerowork/using-selectors/how-to-build-custom-selectors/hierarchy.md).

# Hierarchy

Understanding hierarchy is one of the most important steps when it comes to building custom selectors.&#x20;

Every web element on a website adheres to a hierarchy. See the hierarchy for the search button on the Wikipedia page:

![](https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FGKQwch3vr3QjjWz5t1gl%2FScreenshot%202022-04-06%20at%2020.44.40.png?alt=media\&token=2bf158e0-a210-4f02-80d1-b60bb4a21fb0)

For the search button on Wikipedia in the above example, the hierarchy would be:&#x20;

<mark style="background-color:yellow;">body > div > form > fieldset > button</mark>

Note that some elements are on the same level (see picture below). This means they are siblings and cannot be defined in relation to each other using `>`. They can be addressed as a list (described in a later chapter, [Lists: Incremental CSS Selectors](/using-zerowork/using-selectors/how-to-build-custom-selectors/lists-incremental-css-selectors.md)) or, more advanced, as siblings (described in the final chapter, [Addressing Siblings](/using-zerowork/using-selectors/how-to-build-custom-selectors/addressing-siblings.md)).

![](https://3220183989-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fg9LcR8XM7TGOzOCDGsCS%2Fuploads%2FrkKl7HTtKD6FYn6gUl6T%2FScreenshot%202022-04-06%20at%2020.44.40.png?alt=media\&token=5a5e7745-e3dd-42e5-9bc7-2b898ea19fe9)

### Strict and Loose Hierarchy

#### Strict hierarchy

Whenever you use `>`, the hierarchy is defined strictly. In the example above (`body > div > form > fieldset > button`), `div` must be a direct child of `body`, `form` a direct child of `div`, and so on. If this hierarchy changes, the selector will fail.

#### Loose hierarchy

Alternatively, you can use a space to define a loose hierarchy. For example:

<mark style="background-color:yellow;">body button</mark>

This means that *button* appears somewhere **below** *body*, and it doesn't matter if there are other elements **between** them.

### Build Selectors Based on Hierarchy

In some cases, to build a selector, you may need to additionally define the hierarchy. You can simply define tags that follow each other, like this:

**Strict hierarchy:** <mark style="background-color:yellow;">tag1 > tag2 > tag3 > tag4 > tag5</mark>

**Loose hierarchy:** <mark style="background-color:yellow;">tag1 tag3 tag5</mark> (tag2 and tag4 are skipped)&#x20;

**Combination of strict and loose hierarchy:** <mark style="background-color:yellow;">tag1 tag3 > tag4</mark> (tag2 is skipped but tag4 strictly follows tag3)

And for any tag inside the hierarchy you can add attributes like this:

<mark style="background-color:yellow;">tag1\[attribute='value'] > tag2\[attribute='value']</mark>

<mark style="background-color:yellow;">tag1 > tag2\[attribute='value']</mark>

<mark style="background-color:yellow;">tag1\[attribute='value'] > tag2</mark>

For the example of the search button on Wikipedia, the list of selectors we built can be extended by these ones:

**Strict hierarchy:** <mark style="background-color:yellow;">body > div > form > fieldset > button\[class\*='pure-button']</mark>

**Loose hierarchy:** <mark style="background-color:yellow;">body form button\[class\*='pure-button']</mark>

**Combination of strict and loose hierarchy:** <mark style="background-color:yellow;">body > div button\[class\*='pure-button']</mark>

### Video Tutorial: Selector Hierarchy

{% embed url="<https://youtu.be/Z8-JNTNRLrA>" %}
