CSS stands for , which basically means things later on in the page take precedence over things earlier (with some ). This also applies to how we select elements - from parent to child, with no way to select parents, . cascading stylesheets major caveats until now In the specification, CSS introduces a new selector called , which finally lets us select parents. What that means is we'll be able to target a CSS element which specific children within it. This is already supported in , and is also in . The full support table is shown below: CSS Selectors 4 :has() has Safari Chrome 105 With support increasing, in this article, I will focus on how CSS parent selection works, and how you can do it today where support is available. In the meantime, if you require full support in all browsers, you can also until native CSS support is available. implement this polyfill How Parent Selectors work in CSS In CSS, if we want to select something, we use selectors that the DOM. For example, selecting a tag within a tag looks like this: descend p div div p { color: red; } Until now, we couldn't really select the tags which had tags within them, though, and this meant we had to resort to Javascript. The main reason this wasn't implemented in CSS is because it's quite an expensive operation to do. CSS is relatively fast to parse, but selecting parent tags requires a relatively significant larger amount of processing. div p Using the selector, we can now select elements that have a children, or any normal combination of selectors. For example, selecting a with a child now looks like this: :has div p div p /* Makes the div color: red; */ div:has(p) { color: red; } This will make any with a child red. div p Combining parent selection with other selectors Just like any other CSS selector, we can combine this for specific circumstances. For example, if you want to select only tags which have direct children: div span div:has(> span) { color: red; } As the vocabulary of suggests, it is not just limited to parent selection. For example, below we can select a which a sibling : :has span :has div span:has(+ div) { color: red; } Or even, selecting an element which does have a child, by using the selector. For example, the following will select any div which does have a child: not :not() not p div:not(:has(p)) { color: red; } Selecting elements which only contain text in CSS One very common problem in CSS is that the tag does not select elements which contain any text - so sometimes an element can contain one space, and will not apply. The selector gives us the power to select elements that only contain text nodes, and no other child elements. :empty :empty :has Although this is not the perfect solution for simply elements with spaces (as this will select any element with just text and no additional HTML DOM elements) - it does give us the ability to select DOM elements with only text nodes, which was not previously possible. We can achieve this with the following code: :empty div:not(:has(*)) { background: green; } Conclusion With the addition of selector support in Chrome 105, parent selection is quickly becoming a reality that we will soon be able to use on real-life projects. As of now, with Safari support, it's easy to test and see how it will work in the future. This has the additional benefit of letting us cut back on Javascript solutions to parent selection which is quite common in many applications and products. :has() You can even start using today, if you also implement , and then remove the polyfill once native support comes to Chrome and other browsers. :has this polyfill I hope you've enjoyed this article. . To learn more about CSS, click here Also Published Here