Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
513 views
in Technique[技术] by (71.8m points)

css - “?”(波浪号/波浪形/旋转)CSS选择器是什么意思?(What does the “~” (tilde/squiggle/twiddle) CSS selector mean?)

Searching for the ~ character isn't easy.

(搜索~字符并不容易。)

I was looking over some CSS and found this

(我查看了一些CSS,发现了这个)

.check:checked ~ .content {
}

What does it mean?

(这是什么意思?)

  ask by Akshat translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The ~ selector is in fact the General sibling combinator (renamed to Subsequent-sibling combinator in selectors Level 4 ):

(~选择器实际上是通用同级组合器 (在选择器级别4中重命名为后续同级组合 ):)

The general sibling combinator is made of the "tilde" (U+007E, ~) character that separates two sequences of simple selectors.

(通用同级组合器由分隔两个简单选择器序列的“波浪号”(U + 007E,?)字符组成。)

The elements represented by the two sequences share the same parent in the document tree and the element represented by the first sequence precedes (not necessarily immediately) the element represented by the second one.

(这两个序列所表示的元素在文档树中共享相同的父对象,而第一个序列所表示的元素在第二个所表示的元素之前(不一定紧接)。)

Consider the following example:

(考虑以下示例:)

 .a ~ .b { background-color: powderblue; } 
 <ul> <li class="b">1st</li> <li class="a">2nd</li> <li>3rd</li> <li class="b">4th</li> <li class="b">5th</li> </ul> 

.a ~ .b matches the 4th and 5th list item because they:

(.a ~ .b与第4个和第5个列表项匹配,因为它们:)

  • Are .b elements

    (是.b元素)

  • Are siblings of .a

    (是.a兄弟姐妹)

  • Appear after .a in HTML source order.

    (按HTML源顺序显示在.a之后。)

Likewise, .check:checked ~ .content matches all .content elements that are siblings of .check:checked and appear after it.

(同样, .check:checked ~ .content.check:checked同级项的所有.content元素匹配,并出现在其后。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...