Searching for the ~
character isn't easy. I was looking over some CSS and found this
.check:checked ~ .content {
}
What does it mean?
Searching for the ~
character isn't easy. I was looking over some CSS and found this
.check:checked ~ .content {
}
What does it mean?
It is
General sibling combinator
and is explained in @Salaman's answer very well.What I did miss is
Adjacent sibling combinator
which is+
and is closely related to~
.example would be
.b
.a
.a
in HTMLIn example above it will mark 2nd
li
but not 4th.JSFiddle
General sibling combinator
More info
Good to also check the other combinators in the family and to get back to what is this specific one.
ul li
ul > li
ul + ul
ul ~ ul
Example checklist:
ul li
- Looking inside - Selects all theli
elements placed (anywhere) inside theul
; Descendant selectorul > li
- Looking inside - Selects only the directli
elements oful
; i.e. it will only select direct childrenli
oful
; Child Selector or Child combinator selectorul + ul
- Looking outside - Selects theul
immediately following theul
; It is not looking inside, but looking outside for the immediately following element; Adjacent Sibling Selectorul ~ ul
- Looking outside - Selects all theul
which follows theul
doesn't matter where it is, but bothul
should be having the same parent; General Sibling SelectorThe one we are looking at here is General Sibling Selector
The
~
selector is in fact the General sibling combinator (renamed to Subsequent-sibling combinator in selectors Level 4):Consider the following example:
.a ~ .b
matches the 4th and 5th list item because they:.b
elements.a
.a
in HTML source order.Likewise,
.check:checked ~ .content
matches all.content
elements that are siblings of.check:checked
and appear after it.Note that in an attribute selector (e.g.,
[data-components~=wheels]
), the tildehttps://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors