What is the purpose of ~ and > in CSS? For example what does the following expression mean?
:checked ~ label ~ .content > *
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your selector means:
>is the child combinator. It selects elements that are children of a certain parent element. Unlike the space (the descendant combinator), it only selects immediately-nested elements. See this answer for an illustration on how it works.~is the general sibling combinator. It selects elements that follow after other elements within the same parent (i.e. are siblings). Unlike+(the adjacent sibling combinator), it doesn’t require an element to immediately follow another in the same parent. Compare the illustration below to this other answer which covers the+combinator.Be careful, as the
~combinator does not just select any sibling element. It only selects an element that comes after its sibling, so:checked ~ labelwill not match alabelthat occurs before the checked input element.An illustration:
What’s selected and what’s not:
Selected
This
h3orpelement is located directly inside a.contentparent element. That.contentelement follows at least onelabel, and thislabeloccurs after at least one input element that is:checked.Note that any of the radio buttons here can be checked, and the elements will match, because as mention above
~doesn’t require a label to follow it immediately. Also, given the structure, either one of the~selectors can be swapped out for a+:But this selector:
Will only match if the third radio button is checked, because it’s the only one that’s immediately followed by a
labeland the.contentelement.Not selected
This
emelement is nested within one of thepelements which is itself contained within.content. Based on the illustration here, it won’t be selected as it’s not a child of.content.Not selected
Unlike in [1], none of the
labelelements in this section follow any:checkedinput element. Therefore, nothing is selected here, because it doesn’t satisfy:checked ~ label.