Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select the last element within an element regardless of type in CSS?

I want to select the last nested element regardless of the element's type.

Example:

<section>
    <div></div>
    <p></p>
    <div></div> <-- I want this element
</section>
<section>
    <div></div>
    <p></p>
    <a></a>     <-- I want this element
</section>
<section>
    <div></div>
    <p></p>
    <ul>        <-- I want this element
        <li></li>
    </ul>
</section>

How do I get these elements? :last-child doesn't seem to do the trick and :last-of.type is also not helpful because I have to specify which type of element I want.

I want the last nested element of the section elements.

like image 350
Tywele Avatar asked Sep 07 '16 10:09

Tywele


People also ask

How do you target the last element in CSS?

The :last-child selector allows you to target the last element directly inside its containing element. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

How do you select all but last element in CSS?

When designing and developing web applications, sometimes we need to select all the child elements of an element except the last element. To select all the children of an element except the last child, use :not and :last-child pseudo classes.

How do you select an element after an element in CSS?

The element+element selector is used to select an element that is directly after another specific element.

What is the last of type selector in CSS?

The :last-of-type selector allows you to target the last occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

How do I select the last element of a specific class?

To select the last element of a specific class, you can use the CSS :last-of-type pseudo-class. In this snippet, we'll show examples with the HTML <article> and <p> elements.

What does last of type mean in CSS?

:last-of-type. The :last-of-type selector allows you to target the last occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

What is the last of type in CSS pseudo-class?

The :last-of-type CSS pseudo-class represents the last element of its type among a group of sibling elements. /* Selects any <p> that is the last element of its type among its siblings */ p:last-of-type { color: lime; }. Note: As originally defined, the selected element had to have a parent.


2 Answers

Use this:

section > *:last-child {
    // your custom css styles
}

Explanation:

This works because when you use:

> it select the immediate children

* this selects all the the elements

nth-child ensures that all the immediate children will be targetted (if you use nth-of-type that is not the case.

like image 179
kukkuz Avatar answered Oct 19 '22 09:10

kukkuz


you can use section >*:last-child where * is the universal selector and > is the child selector, meaning only will apply to the direct descendants of section

section {
  color: blue
}
section > *:last-child {
  color: red
}
<section>
  <div>blue</div>
  <p>blue</p>
  <div>red</div>
  <!-- I want this element -->

</section>
<section>
  <div>blue</div>
  <p>blue</p>
  <a>red</a>
  <!-- I want this element -->

</section>
<section>
  <div>blue</div>
  <p>blue</p>
  <ul>
    <!-- I want this element -->

    <li>red</li>
  </ul>
</section>
like image 30
dippas Avatar answered Oct 19 '22 07:10

dippas