Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I select all children of an element except the last 3 child?

How to apply CSS margin-bottom: 30px; to first 3 element and not last 3 element below HTML?

<div class="container">
   <div class="card"></div>
   <div class="card"></div>
   <div class="card"></div>
   <div class="card"></div>
   <div class="card"></div>
   <div class="card"></div>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use a pseudo element called nth-child(). This picks particular childs. In this case, you would use

.card:nth-child(-n+3) {
  margin-bottom: 30px;
}
.card:nth-child(-n+3) {
  color:red;
}
<div class="container">
   <div class="card">7</div>
   <div class="card">6</div>
   <div class="card">5</div>
   <div class="card">4</div>
   <div class="card">3</div>
   <div class="card">2</div>
   <div class="card">1</div>
</div>

You use n as a variable. At n = 1, nth-child will be 2, n = 2, nth-child will be 1 and n = 3 nth-child will be 0. It won’t go on to have negative a nth-child.

Let me know if it works!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading