Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select the last 3 child elements

I know you can select the last child with :last-child or a certain child with :nth-child (if you know their position/number).

What I need is to select the last 3 children without knowing how many child elements there could be.

I know there is something that's called :nth-last-child but I cant really understand how it is working.

For this:

<div id="something">

    <a href="images/x.jpg"  ><a/>
    <a href="images/x.jpg"  ><a/>
    <!-- More elements here -->
    <!-- ..... -->
    <!-- I need to select these: -->
    <a href="images/x.jpg"  ><a/>
    <a href="images/x.jpg"  ><a/>
    <a href="images/x.jpg"  ><a/>

</div> 

I need something like this:

#something a:last-child{
   /* only now it selects the last <a> and the 2 <a>'s that come before*/ 
}
like image 683
Ivo Avatar asked Jan 10 '13 22:01

Ivo


People also ask

What is last child in HTML?

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 the elements except the last child?

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.


3 Answers

You can read more here about nth-last child, but this should basically do the trick of selecting the last 3 children with just CSS

#something a:nth-last-child(-n+3) {
    /*declarations*/
}

fiddle demonstration from Fabrício Matté

This will only select those rows returning a positive number for out N expression (-n+3), and since we are using nth-last-child, it's counting from last to first, so first rows from bottom gives,

f(n) = -n+3
f(1) = -1+3 = 2 <- first row from the bottom
f(2) = -2+3 = 1 <- second row from the bottom
f(3) = -3+3 = 0 <- third row from the bottom

everything else will return a negative number

like image 73
Bassam Mehanni Avatar answered Oct 14 '22 06:10

Bassam Mehanni


This is possible with CSS3 selectors and formulas:

.something a:nth-last-child(-n+3) { ... }

You could also try using jQuery (example) or adding a specific class to the last three elements in your server-side code (it does not require JavaScript to be enabled in browsers and also works on older browsers that do not support CSS3).

like image 29
praseodym Avatar answered Oct 14 '22 07:10

praseodym


The accepted answer has the correct formula, but the explanation is wrong.

So the correct CSS is (same as currently accepted answer):

#something a:nth-last-child(-n+3) {
    /*declarations*/
}

But here is the correct explanation of the math:

f(n) = -n+3
f(0) = -0+3 = 3 <- 3rd row from the bottom
f(1) = -1+3 = 2 <- 2nd row from the bottom
f(2) = -2+3 = 1 <- 1st row from the bottom
f(3) = -3+3 = 0 <- nothing
f(3) = -4+3 = -1 <- nothing
etc...
like image 36
Richard Buff Avatar answered Oct 14 '22 05:10

Richard Buff