- `, and styles them with a top border.
```html live-sample___child
- Unordered item
-
Unordered item
- Item 1
- Item 2
- ` elements will get a red border.
## Next-sibling combinator
The **next-sibling combinator** (`+`) is placed between two CSS selectors. It matches only those elements matched by the second selector that come right after the element matched by the first selector. For example, to select all `
` elements that are immediately preceded by a `
` element: ```css p + img { /* … */ } ``` A common use case is to do something with a paragraph that follows a heading, as in the example below. Here, we select any paragraph that shares a parent element with an `
`, and immediately follows that `
`. ```html live-sample___adjacent
``` ```css live-sample___adjacent body { font-family: sans-serif; } h1 + p { font-weight: bold; background-color: #333333; color: white; padding: 0.5em; } ``` {{EmbedLiveSample("adjacent", "", "220px")}} In the previous example: 1. Try inserting another element such as an `A heading
Veggies es bonus vobis, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo melon azuki bean garlic.
Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.
` in-between the `
` and the `
`. You will find that the paragraph is no longer matched by the selector and so does not get the background and foreground color applied when the element is adjacent. 2. Now modify the `h1 + p` selector so that the special style is applied to the first paragraph once more. ## Subsequent-sibling combinator If you want to select siblings of an element even if they are not directly adjacent, then you can use the **subsequent-sibling combinator** (`~`). To select all `
` elements that come _anywhere_ after `
` elements, we'd do this: ```css p ~ img { /* … */ } ``` In the example below, we are selecting all `
` elements that come after the `
`, and even though there is a `
` in the document as well, the `` that comes after it is selected. ```html live-sample___general
``` ```css live-sample___general body { font-family: sans-serif; } h1 ~ p { font-weight: bold; background-color: #333333; color: white; padding: 0.5em; } ``` {{EmbedLiveSample("general", "", "220px")}} ## Combining combinators with selectors You can combine any of the selectors that we discovered in previous lessons with combinators in order to select part of your document. For example, to select list items with a `class` of `a` which are direct children of a `A heading
I am a paragraph.
I am a divI am another paragraph.
- `, try the following:
```css
ul > li[class="a"] {
}
```
Take care, however, when creating big lists of selectors that select very specific parts of your document. It will be hard to reuse the CSS rules since you have made the selector very specific to the location of that element in the markup.
It is often better to create a simple class and apply that to the element in question. That said, your knowledge of combinators will be very useful if you need to style something in your document and are unable to access the HTML, perhaps due to it being generated by a {{Glossary("CMS")}}.
## Summary
That's it for selectors, for now. Next, we'll give you some tests that you can use to check how well you've understood and retained the information we've provided on CSS selectors.
{{PreviousMenuNext("Learn_web_development/Core/Styling_basics/Pseudo_classes_and_elements", "Learn_web_development/Core/Styling_basics/Test_your_skills/Selectors", "Learn_web_development/Core/Styling_basics")}}