Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is last-child not targeting the last article tag in this fiddle?

Tags:

html

css

article

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;}
like image 227
byronyasgur Avatar asked Jan 18 '13 21:01

byronyasgur


People also ask

Why is my last child not working?

: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.

Why is last child not working CSS?

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.

What does last child mean 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.

What is the difference between last child and last of type?

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.


1 Answers

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.

like image 183
jamesplease Avatar answered Oct 10 '22 18:10

jamesplease