Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selecting an element that has a specific child?

Tags:

css

If I wanted to do a CSS selector on a list like this:

<ul>
    <li></li>
    <li><a></a></li>
    <li><a></a></li>
    <li><a></a></li>
</ul>

and I wanted to do a li:hover effect on only the lis that contain an <a> tag, is there a way to specify that in CSS? if li:hover contains <a> then li:hover effect = X?

like image 688
proseidon Avatar asked Jan 24 '13 19:01

proseidon


People also ask

How do I select a specific child in CSS?

The :nth-child(n) selector matches every element that is the nth child of its parent. n can be a number, a keyword (odd or even), or a formula (like an + b). Tip: Look at the :nth-of-type() selector to select the element that is the nth child, of the same type (tag name), of its parent.

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 could you use a selector to choose a specific element with a specific class?

The CSS class Selector The class selector selects HTML elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the class name.

How do you select an element with a specific ID?

The CSS ID selector matches an element based on the value of the element's id attribute. In order for the element to be selected, its id attribute must match exactly the value given in the selector.


2 Answers

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

like image 62
Jon Avatar answered Oct 03 '22 17:10

Jon


You can do it in JQUERY:

$("a").parent("li").css("color","#923123");

For what you requested, it would be like this:

$("li").mouseover(function(){

   if ($(this).is(':parent'))
   {
       //this <li> has a child, supposed to be <a>
   }

});
like image 44
Ali Bassam Avatar answered Oct 03 '22 19:10

Ali Bassam