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

Display first element

If I have:

But with result only for first .first. The first .second is still with display none.

.first {
  display: none;
}

.first:first-of-type {
  display: block;
}

.second {
  display: none;
}

.second:first-child {
  display: block;
}
<div class="first">first</div> //I want to display only this
<div class="ro">ro</div>
<div class="first">first</div> //I want to hide this
<div class="ro">ro</div>
<div class="first">first</div> //I want to hide this
<div class="ro">ro</div>
<div class="first">first</div> //I want to hide this
<div class="ro">ro</div>
<div class="second">second</div> //I want to display only this
<div class="eo">eo</div>
<div class="second">second</div> //I want to hide this
<div class="eo">eo</div>

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

>Solution :

You should use the subsequent-sibling combinator ~:

.first {
    display: block;
}
.first ~ .first {
    display: none;
}
.second {
    display: block;
}
.second ~ .second {
    display: none;
}

With the formula .first ~ .first you’re instructing to apply display: none to all elements with class .first preceded by another element with class .first.

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