Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selector for not immediate child element

Tags:

html

css

Can anyone tell me what's the selector for finding an element which is within a parent, but not an immediate child. Example:

<body class="main">
  <p class="text">Hello</p> <!-- don't select this one -->
  <elem class="something">
    <elem id="link">
      <elem class="otherclass">
        <p class="text">Hello</p> <!-- but this one -->
      </elem>
    </elem>
  </elem>
</body>

so I want to find .text via .main without knowing the number of elements in between and with pure CSS.

Thanks

like image 648
supersize Avatar asked Sep 03 '14 12:09

supersize


People also ask

How do I select not the first child in CSS?

Method 1: Use :not(:first-child) to not Select First Child. The “:not(:first-child)” selector defines the rule that does not select the first child of the parent tag. This selector will help us to exclude applied CSS styling for the first element.

How do you select all child elements except first?

By using the :not(:first-child) selector, you remove that problem. You can use this selector on every HTML element. Another common use case is if you use an unordered list <ul> for your menu.

Which CSS selector is used to select an element that has no children?

The :empty CSS pseudo-class represents any element that has no children.

Which selector is used to select the element that is the immediate child of the specified tag?

The :first-child selector allows you to target the first element immediately inside another element.


1 Answers

You could use the following selector:

.main > * .text

Which will select all .text elements that are descendants of .main, but not immediate children.

like image 79
Josh Crozier Avatar answered Sep 29 '22 12:09

Josh Crozier