Css Selector - Element With a Given Child

CSS selector - element with a given child

Is it possible to select an element if it contains a specific child element?

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
<p><span>lorem</span> ipsum dolor sit amet</p>
<p>consecteture edipsing elit</p>

This selector would style the span element

p span {
color: red;
}

This selector would style the p element

!p span {
color: red;
}

A 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 p element that contains a span one could use:

p:has(span) {
color: red;
}

* This makes me wonder if jQuery had implemented selector subjects whether subjects would have remained in the specification.

Selecting an element that has a specific child?

No, CSS does not allow you to select elements based on their descendants.

CSS select element with particular child element attribute

I tried your complex selector in CSS, plain JS and using the jQuery lib... Guess who wins!

// Checking if JS handles that selector....

// Let's have the selector in a variable, just to make sure the same is tried in both cases...
let ourSelector = ".wp-block-image figure.alignright:has(img:not([src*='triangle']))"

// JS querySelector
try{
document.querySelector(ourSelector).style.border = "3px solid blue";
}
catch(error){
console.log(error.message);
}

// jQuery! (--WORKS--)
$(ourSelector).css("border", "3px solid blue");
/* This rule applies */
.wp-block-image figure.alignright img:not([src*="triangle"]) {
border: 3px solid red;
}


/* This one not */
.wp-block-image figure.alignright:has(img:not([src*="triangle"])) {
opacity: 0.2;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="wp-block-image">
<figure class="alignright">
<img width="387" height="500" src="https://via.placeholder.com/300x300.jpg?text=triangle" />
</figure>
<figure class="alignright">
<img width="387" height="500" src="https://via.placeholder.com/300x300?text=square" />
</figure>
</div>

CSS selector for foo that contains bar ?

No, what you are looking for would be called a parent selector. CSS has none; they have been proposed multiple times but I know of no existing or forthcoming standard including them. You are correct that you would need to use something like jQuery or use additional class annotations to achieve the effect you want.

Here are some similar questions with similar results:

  • Is there a CSS parent selector?
  • CSS Parent/Ancestor Selector
  • Complex CSS selector for parent of active child

CSS selector for a child element whose parent element has a certain class

To select strong elements that are descendants of an element with class commandBar, use the descendant combinator along with a class selector:

.commandBar strong

In order to only select direct children strong elements, use the child combinator, >:

.commandBar > strong

Depending on your markup, you may also want to specify the element type that has the class .commandBar (in this case, div):

div.commandBar strong

CSS select an element with no certain child element

css :not() not supported to select except "has element" but you can do it with jQuery

$('p').not(":has(img)").css('background-color', 'yellow')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script><p>aa</p><p>aa</p><p>img<img src="" /></p><p>aa</p>

Select element based on child class

This isn't possible with css, since you're working against the cascade by selecting an ancestor based on a descendant.

The best I can offer and suggest is a jQuery approach:

$(document).ready(
function() {
$('.givenClassName').parent().addClass('something');
}
);

This finds the element with the givenClassName and then selects its parent element and adds the class something to that element.

@Blaenk suggests an alternate approach, which is more versatile (his approach doesn't require the ancestor element to be the parent of the element you're selecting by).

Obviously other JS libraries, and JS all by itself, can achieve the same effect, though I can't offer particular advice, since I'm still only just familiarising myself with JS and mostly with jQuery (why yes, I am ashamed of myself...).

Select an element without selecting descendants

Use the CSS child combinator >:

.settings > ul > li > i {
opacity: 0.5;
}
<div class="settings">
<ul>
<li>
<i>Select this element</i>
<ul>
<li>
<i>but not this one</i>
<span></span>
</li>
<li>
<i>or not this one</i>
<span></span>
</li>
</ul>
</li>
</ul>
</div>

Css Selector for elements with a specific class and parent

The > selector indicates a direct parent. Your above code .sub caption > h1 is attempting to select any h1 element that is a direct child of a caption element, where the <caption> element is a child of an element with the class .sub. It is not working because you don't have a parent of <caption> with the class of sub.

You can specify that your target elements must contain specific classes by writing the element selector immediately followed by the class selector (without a space). For example, h1.sub targets any <h1> element that has the class sub. It won't target <h1> elements that don't have the .sub class, nor elements that are not <h1> elements but do have the .sub class.

It's also important to note that <caption> is only valid as a direct child of <table>.

Essentially, what you're looking for is the following: