Yogesh Chauhan's Blog

Is there a CSS parent selector?

in CSS on July 22, 2021

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?

Few more :nth-child examples in CSS

How to apply style to a specific child element using CSS?

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.

Ampersand (Parent Selector) in SCSS (Sass)

: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.


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to apply style to a specific child element using CSS?CSSHow to uninstall Cocoapods from the Mac OS?MiscellaneousABS and NEG functions in Envision BasicEnvision BasicDifference between :where and :is in CSSCSSPostgreSQL transactions using the BEGIN, COMMIT, and ROLLBACK statements.PostgresSolution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software”MiscellaneousContent Blocks in SCSS (Sass)SCSSHow to create rotating texts using JavaScript and CSS?CSSSQL Right JoinSQL/MySQLPostgreSQL BETWEENPostgresHow to disable scrolling on html body on menu click using JavaScript?JavaScriptWordPress: How to setup and get values from an ACF options page?WordPress