I’m looking to make a selector which will select all elements if they have a specific child element. For example, select all <div> with a child <span>.
Possible?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Unfortunately not yet.
The CSS2 and CSS3 selector specifications do not allow for any sort of parent selection.
A Note About Specification Changes
This is a disclaimer about the accuracy of this post from this point onward. Parent selectors in CSS have been discussed for many years. As no consensus has been found, changes keep happening. I will attempt to keep this answer up-to-date, however be aware that there may be inaccuracies due to changes in the specifications.
An older "Selectors Level 4 Working Draft" described a feature which was the ability to specify the "subject" of a selector. This feature has been dropped and will not be available for CSS implementations.
The subject was going to be the element in the selector chain that would have styles applied to it.
Example HTML
This selector would style the
spanelementThis selector would style the
pelementA more recent "Selectors Level 4 Editor’s Draft" includes "The Relational Pseudo-class:
:has()":has()would allow an author to select an element based on its contents. My understanding is it was chosen to provide compatibility with jQuery’s custom:has()pseudo-selector*.In any event, continuing the example from above, to select the
pelement that contains aspanone could use:* This makes me wonder if jQuery had implemented selector subjects whether subjects would have remained in the specification.