Interview Questions

What is parent-child selector?

CSS Interview Questions and Questions


(Continued from previous question...)

31. What is parent-child selector?

Parent-child selector is a selector representing the direct descendent of a parent element. Parent-child selectors are created by listing two or more tilde (~) separated selectors.

BODY ~ P {background: red; color: white}
The P element will be declared the specified style only if it directly descends from the BODY element:
<BODY> <P>Red and white paragraph </P> </BODY>

BODY ~ P ~ EM {background: red; color: white}
The EM element will be declared the specified style only if it directly descends from the P element which in its turn directly descends from the BODY element:

< <P> <EM>Red and white EM </EM> </P> </BODY>

(Continued on next question...)

Other Interview Questions