Why is last-child not targeting the last article tag in this fiddle?
http://jsfiddle.net/gunP9/
<div class="parent">
<article class="example">111</article>
<article class="example">111</article>
<article class="example">111</article>
<article class="example">111</article>
<section>content</section>
</div>
CSS
.parent .example{ background-color: red;}
.parent .example:last-child{background-color: yellow;}
:last-child will not work if the element is not the VERY LAST element. In addition to Harry's answer, I think it's crucial to add/emphasize that :last-child will not work if the element is not the VERY LAST element in a container.
Pseudo-selectors like :last-child or nth-child or nth-of-type only select based on elements not classes or attributes. Either something is the last-child or it isn't. If it's the last element in a parent it will be selected. So, in your case, it's testing for it to meet all the conditions.
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.
Both selectors work in the same way but have a slight difference i.e the last type is less specified than the last-child selector. Example: This example implements the :last-child selector. Output: After compiling the SASS source code you will get this CSS code. the :nth-last-of-type() selector.
last-child
targets, well, the last child of the parent. In this case, the last child is a section
which doesn't have the class example
, and consequently nothing matches the selector.
What you're looking for is last-of-type
, which matches elements and not classes. Take a look.
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