Skip to content

Commit b36d73c

Browse files
Add example for :has() parent selection (mdn#40732)
* Add example for :has() parent selection * Update index.md * Update index.md --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 3fad044 commit b36d73c

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

files/en-us/web/css/_colon_has/index.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,30 @@ Pseudo-elements are also not valid selectors within `:has()` and pseudo-elements
3535

3636
## Examples
3737

38+
### Selecting a parent element
39+
40+
You may be looking for a "parent [combinator](/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators#combinators)", which allows you to go up the DOM tree and select the parent of a specific element. The `:has()` pseudo-class does that by using `parent:has(child)` (for any parent) or `parent:has(> child)` (for direct parent). This example shows how to style a `<section>` element when it contains a child with the `featured` class.
41+
42+
```html
43+
<section>
44+
<article class="featured">Featured content</article>
45+
<article>Regular content</article>
46+
</section>
47+
<section>
48+
<article>Regular content</article>
49+
</section>
50+
```
51+
52+
```css
53+
section:has(.featured) {
54+
border: 2px solid blue;
55+
}
56+
```
57+
58+
### Result
59+
60+
{{EmbedLiveSample('Selecting a parent element', , 200)}}
61+
3862
### With the sibling combinator
3963

4064
The `:has()` style declaration in the following example adjusts the spacing after `<h1>` headings if they are immediately followed by an `<h2>` heading.

0 commit comments

Comments
 (0)