I have a few posts to apply specific styles to a specific or all child elements.
How to apply style only to first child and/or only to children other than the first child?
But, CSS has yet to come up with a parent selector.
By parent selector, I mean you can just literally select any element’s parent element in CSS without JavaScript.
There is a Parent Selector concept in SassScript but it’s not quite like what developers are looking for.
Here’s another post I wrote for SCSS parent selector.
:has – a new hope
The Selectors Level 4 Draft mentions a :has() pseudo-class that will have the feature developers are looking for.
:has example
The following selector matches only a elements that has an img element child:
a:has(> img){
...
}
The problem is :has pseudo-class is not supported in a single browser as per now.
So, till it gets a full support, we have to use libraries like jQuery or JavaScript to achieve the same goal.
:has child examples nth-child parent selector