---
title: "sibling selector"
url: https://weworkworldwide.com/tutorials/sibling-selector/
description: "If the target element/s of a CSS style is actually the sibling/s of another element, we can use Sibling Selector in order to find those siblings of the spe"
date: 2026-09-14T09:00:04+00:00
source: https://weworkworldwide.com/llms.txt
---

# sibling selector

``` line-numbers
```

If the target element/s of a CSS style is actually the sibling/s of another element, we can use *Sibling Selector* in order to find those siblings of the specified element and apply the style that we want.

– Adjacent Sibling Selector:

``` line-numbers
```

Example: .class-name+ p { }, #id+ ul { }, div+ p{ } etc. The target of this type of selector is only the *NEXT sibling* element after the specified element.

For example “.class-name + p” means the <p> element that is the sibling of another element that has a class attribute’s value of “class-name”.

Note: this <p> element should be the *NEXT sibling*.

Example:

``` line-numbers
<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<style type="text/css">
h2+ul{
border:double;
}
</style>
</head>
<body>

<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<h2>Un-ordered list</h2>
<ul>
<li>Black</li>
<li>white</li>
<li>Blue</li>
<li>
<ul>
<li>cat</li>
<li>dog</li>
<li>donkey</li>
</ul>
</li>
</ul>

</body>
</html>
```

In the example above, the first <ul> element does not have double border because this element comes BEFORE <h2> element. But the <ul> element that comes after <h2> element has double border (this is what we call NEXT sibling).

– General Sibling Selector:

``` line-numbers
Example: .class-name ~ p { }, #id~ul { }, p~h1 { } etc. the target of this type of selector is all those elements that are NEXT sibling of the specified element (Basically they have to have the same parent as the specified element)
```

Example:

``` line-numbers
<!DOCTYPE html>
<html>
<head>
<title>CSS is fun :)</title>
<style type="text/css">
h2~ul{
border:double;
}
</style>
</head>
<body>

<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
<h2>Un-ordered list</h2>
<ul>
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
<ul>
<li>Black</li>
<li>white</li>
<li>Blue</li>
<li>
<ul>
<li>cat</li>
<li>dog</li>
<li>donkey</li>
</ul>
</li>
</ul>

</body>
</html>
```
