# Hierarchy

Understanding hierarchy is one of the most important steps when it comes to building custom selectors. Do not skip this section!

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 these elements are items in a list and siblings to each other. They cannot be defined in a hierarchical way as shown in the example above (with >). They can be either defined as a list (you will learn about it later in chapter [lists-incremental-css-selectors](https://docs.zerowork.io/using-zerowork/using-selectors/how-to-build-custom-selectors/lists-incremental-css-selectors "mention")) or - which is a bit more advanced - as siblings (you will learn about it in the final chapter of this guide [addressing-siblings](https://docs.zerowork.io/using-zerowork/using-selectors/how-to-build-custom-selectors/addressing-siblings "mention")).

![](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 in a strict way - in the example above (*body > div > form > fieldset > button*), *body* MUST be followed by *div* and if not the selector will fail.&#x20;

#### Loose hierarchy

Alternatively, you can use space to define a loose hierarchy, for example:&#x20;

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

This means that *body* is followed by *button* somewhere below and it doesn't matter if there are other elements between *body* and *button*.

### 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> (did you note how tag2 and tag4 are skipped?)&#x20;

**Combination of strict and loose hierarchy:** <mark style="background-color:yellow;">tag1 tag3 > tag4</mark> (note how 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, and they will work just as well:

**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>" %}
