0

I want to paint the last one div but I couldn't!

div:first-child {
  background-image: linear-gradient(purple, violet);
}

div:last-child {
  background-image: linear-gradient(45deg, yellow, black);
}
<div>first div</div>
<div>second div</div>
<div class="box">third div</div>
<div id="color">fourth div</div>
<div>fifth div</div>

I found a link but they didn't help me:

:last-child doesn't target last div :last-child not working as expected?

Is it possible to solve the problem without class? (That one will be a class named parent and the second class will be a child that will be inside the div of the parent class?)

j08691
  • 204,283
  • 31
  • 260
  • 272
Rexsaa Gater
  • 13
  • 1
  • 4

1 Answers1

0

You have to make sure that those divs are really first and last children (take a look at elements panel in dev tools). For example, wrap them in <main> element.

div:first-child {
    background-image: linear-gradient(purple,violet);
}

div:last-child{
    background-image: linear-gradient(45deg,yellow,black);
}
 <main>
    <div>first div</div>
    <div>second div</div>
    <div class="box">third div</div>
    <div id="color">fourth div</div>
    <div>fifth div</div>
 </main>
marzelin
  • 10,790
  • 2
  • 30
  • 49