Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select element based on child class

Is there a way in CSS to select an element which has a sub-element with given class?

I want to apply a style to a <ul> list that has <li> with a particular class.

like image 544
akonsu Avatar asked Sep 07 '10 20:09

akonsu


People also ask

How do you select an element of a child in CSS?

The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.

Which is the correct way to select child element?

The element > element selector selects those elements which are the children of the specific parent. The operand on the left side of > is the parent and the operand on the right is the children element. Example: Match all <p> element that are child of only <div> element.

How do I apply CSS to child element based on parent?

It's easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinator ( > ), which is placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.

How do you select an element with class?

To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.


3 Answers

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

like image 164
David Thomas Avatar answered Sep 25 '22 05:09

David Thomas


As far as I know, this is unfortunately not possible with CSS.

If you can use Javascript though, jQuery has a nice way of doing this using the closest() method. The documentation for it even lists an example very similar to yours: selecting a ul that has an li of a particular class.

$("li.some-class").closest("ul");

Forgive me if this is not the best way to do it in jQuery. I'm actually new to jQuery and this is one of the methods I've learned so far, and it seemed fitting.

like image 30
Jorge Israel Peña Avatar answered Sep 24 '22 05:09

Jorge Israel Peña


No. The CSS Cascade does not allow for this kind of selector.

The best solution in this case would be either to modify the DOM with JavaScript or to change up the resultant HTML to add classes to the ul tags.

like image 39
Jeff Rupert Avatar answered Sep 23 '22 05:09

Jeff Rupert