CSS Selectors 'nth-of-type' and '~'

yeahch

ch

Posted on October 1, 2020

CSS Selectors 'nth-of-type' and '~'

Description

p:nth-of-type(2)
/* Selects every <p> element that is the second <p> element of its parent */

p ~ ul
/* Selects every <ul> element that are preceded by a <p> element */
Enter fullscreen mode Exit fullscreen mode

I often use those 2 selectors in CSS stylesheet.
But I did not know about this correctly.
So I would like to share about my fault here with all of us.

div:nth-of-type(2n){
  background-color: red;
}
.c ~ .c {
  background-color:blue;
}
.d ~ .c {
  background-color:green;
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

💖 💪 🙅 🚩
yeahch
ch

Posted on October 1, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related