What does the “~” (tilde/squiggle/twiddle) CSS selector mean?

后端 未结 5 1280
孤街浪徒
孤街浪徒 2020-11-21 05:13

Searching for the ~ character isn\'t easy. I was looking over some CSS and found this

.check:checked ~ .content {
}

What does

5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 05:59

    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

    .a + .b {
      background-color: #ff0000;
    }
    
    
    • 1st
    • 2nd
    • 3rd
    • 4th
    • 5th
    • Matches elements that are .b
    • Are adjacent to .a
    • After .a in HTML

    In example above it will mark 2nd li but not 4th.

       .a + .b {
         background-color: #ff0000;
       }
    • 1st
    • 2nd
    • 3rd
    • 4th
    • 5th

    JSFiddle

提交回复
热议问题