TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

The Tilde is an Amazing CSS Selector

61 点作者 donteatbark大约 13 年前

9 条评论

alanh大约 13 年前
Careful not to misunderstand the syntax. The linked article confusingly has this example:<p><pre><code> input:checked ~ #main &#62; #findMe </code></pre> … described thusly: “With the tilde character on #container input:checked would be able to target #findMe …”<p>Please note the tilde has nothing to do with #findMe. The tilde only relates the input element and #main, which are in-order siblings. (Adjacent in-order siblings, in fact, so + would have worked just as well as ~ in this example.)<p>Please also note that ~ does not select siblings; it selects <i>following</i> siblings.<p><pre><code> &#60;i&#62;italic&#60;/i&#62; &#60;a href="#"&#62;link&#60;/a&#62; &#60;b&#62;bold&#60;/b&#62; </code></pre> With the above HTML, the following selectors are NOT equivalent:<p><pre><code> i ~ b { /* this will match the bold tag above */ } b ~ i { /* this will NOT match any elements in this example */ }</code></pre>
评论 #3985852 未加载
ricardobeat大约 13 年前
Unfortunately the tilde selector is crippled: it doesn't target <i>all</i> siblings, only the ones <i>after</i> the base selector.<p>To illustrate:<p><pre><code> &#60;p&#62;1&#60;/p&#62; &#60;p class="two"&#62;2&#60;/p&#62; &#60;p&#62;3&#60;/p&#62; .two ~ p { color:red; } // only "3" will be red </code></pre> That prevents what would be the most useful cases, like unmarking sibling menu items, slides and the like.
sirclueless大约 13 年前
This post abuses IDs. If you have something with the ID #main, it should be unique anyways or else you have invalid markup. If it's unique, why do you need a sibling selector to find it?<p>This post could be improved by using classes:<p><pre><code> #container input:checked ~ .main &#62; .findMe { ... } </code></pre> But really, the sibling selector is powerful enough that you might not need classes at all. I think the following selector makes a much more powerful statement of the kind of thing you can do now that you couldn't before:<p><pre><code> input:checked ~ div span { ... }</code></pre>
评论 #3985551 未加载
评论 #3986076 未加载
sgdesign大约 13 年前
Can't say I understood what it does from that post, so here's another description:<p><a href="http://reference.sitepoint.com/css/generalsiblingselector" rel="nofollow">http://reference.sitepoint.com/css/generalsiblingselector</a>
评论 #3985268 未加载
评论 #3985238 未加载
评论 #3985232 未加载
X-Istence大约 13 年前
Am I missing something, but that isn't valid CSS. You can't have selectors inside of a selector block in CSS...
评论 #3985440 未加载
评论 #3985452 未加载
luzon19大约 13 年前
There already are many CSS3 sliders created using the 'tilde' selector.<p><a href="http://cssdeck.com/item/348/pure-css3-content-slider" rel="nofollow">http://cssdeck.com/item/348/pure-css3-content-slider</a><p><a href="http://cssdeck.com/item/308/awesome-rotating-css-image-slider" rel="nofollow">http://cssdeck.com/item/308/awesome-rotating-css-image-slide...</a><p><a href="http://cssdeck.com/item/153/solitary-css3-slider-rotation-transition" rel="nofollow">http://cssdeck.com/item/153/solitary-css3-slider-rotation-tr...</a><p><a href="http://cssdeck.com/item/363/simply-css-image-slide" rel="nofollow">http://cssdeck.com/item/363/simply-css-image-slide</a><p><a href="http://cssdeck.com/item/364/pure-css3-slideshow-without-page-jump" rel="nofollow">http://cssdeck.com/item/364/pure-css3-slideshow-without-page...</a>
评论 #3985554 未加载
tim_heap大约 13 年前
I wrote up something that actually demonstrates the idea of matching :checked elements to change the state of another element. Think styleable checkboxes!<p><a href="http://jsfiddle.net/3CUvm/" rel="nofollow">http://jsfiddle.net/3CUvm/</a>
StavrosK大约 13 年前
Hmm, I wonder if we can simulate a Turing machine in css nowadays...
评论 #3986549 未加载
stesch大约 13 年前
This isn't CSS he is writing.