Let's crack this
Name | How to represent? | Example code for CSS | Example code for HTML | Usage |
---|---|---|---|---|
Descendant Selector (SPACE) | element element |
|
|
Here, 1st, 2nd and 3rd <p> tags are inside of the <div> tag (It is called Descendant). So as per the CSS style will apply for the "p" tags inside the "div" tag. So the style will apply for 1,2 and 3 <P> tags. It'll also work for Class and ID. |
Child Selector (>) | element > element |
|
|
The child selector (>) selects all elements that are the children of
a specified element. Here it select all the "p" tags that are child
of the "div" tag. 1st, 2nd and 4th
"p" tags are the child, so style will apply on these tags. 3rd "p" tag is not a child of div, it's descendant; so it'll exclude. |
Adjacent Sibling Selector (+) | element + element |
|
|
The + selector is used to select an element that is directly after another specific element. Here 2 nd and 5 th are the next door neighbor of the "div" tag, so style will apply on these two "p" tag. |
General Sibling Selector (~) | element ~ element |
|
|
The general sibling selector (~) selects all elements that are next siblings of a specified element. Here It'll select all the "p" tags that are after the "div" tag. The style will apply on 3rd and 4th "p" tag. |