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.
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.
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.
The element+element selector is used to select an element that is directly after another specific element.
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.
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.
: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.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With