I have this code and I want to change the background colour of the fifth last row. I know I need to use nth-last-child.
Someone can tell me the code?
.grid-container {
display: grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: auto auto auto auto;
background-color: lime;
}
3
Answers
I am assuming your HTML structure looks like this :
Try this CSS with your structure:
You can target the four cells in the 5th row from the bottom.
They have nth-last-child numbers 17, 18, 19, 20.
Here’s a simple example:
I do not know a way of targeting the row as such as there doesn’t seem to be such a concept in grid (ie if you have a gap the gaps will not be colored).
I am baffled by your question of styling the "fifth last row" when the part of CSS that you included indicates only four rows. I am guessing that you want to target excess/additional rows.
You could use the
:nth-child()
pseudo-class to style the main 16 grid cells and the additional grid cells would have the grid container background color.I’d also like to say that your
grid-template-rows
declaration is unnecessary because it declares the default behavior.Also, since you want multiple columns of the same width, it would be easier to use the
repeat()
function.