0

I'm trying to change the last div's color. Neither last-child and last-of-type work. My code:

.blue-back {
  background-color:blue;
}

.blue-back:last-of-type {
  background-color:red;
}

.blue-back:last-child {
  background-color:red;
}
    <div class="row">
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
    </div>
TylerH
  • 20,799
  • 66
  • 75
  • 101
Gago
  • 97
  • 2
  • 8
  • 1
    You just need to make sure there are actually children within in a parent. In this case there is only one last-child called .blue-black per item – Ryan Mc Oct 30 '19 at 13:02

2 Answers2

3

It's because the last child of .blue-back is always the current item. You need to do:

.col-md-4:last-child .blue-back {
    background-color:red;
}

See a CodePen demo here: https://codepen.io/ryan_pixelers/pen/KKKXBaz

TylerH
  • 20,799
  • 66
  • 75
  • 101
Ryan Mc
  • 833
  • 1
  • 8
  • 25
1

You need to use the :last-child selector to .col-md-4 children of .row parent.

So you can use:

.row .col-md-4:last-child .blue-back {
  background-color: red;
}

or

.row > div:last-child .blue-back {
  background-color: red;
}

.blue-back {
  background-color: blue;
}

.row .col-md-4:last-child .blue-back {
  background-color: red;
}
    <div class="row">
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
      <div class="col-md-4">
        <div class="blue-back">
          <p class="mb-0 text-center text-white">Fase</p>
          <p style="font-size:29px; font-weight:600;" class="mb-0 text-center text-white font-weight-600">1</p>
        </div>
      </div>
    </div>
Αntonis Papadakis
  • 1,210
  • 1
  • 12
  • 22