让咱们今年有一些万圣节主题的帖子精神!我会从超过 GRAVE.eR.CSS选择器中挑选一些将冻结你骨头的选择器。也许不可能,但他们至少有点怪异。javascript
Heydon Pickering使这一个在两年前出名。 我看起来像这样:css
* + * { margin-top:1.5em; }
这个想法是,只有具备先前兄弟的元素在顶部得到边缘。 因此你不必作像下面的东西(codepen):html
.the-last-one-so-don't-put-margin-on-me { margin-bottom: 0; }
相反,你甚至有点免费的间距:java
<div class="module"> <div>Owl</div> <div>Owl</div> <div>Owl</div> <div>Owl</div> </div>
* + * { margin-top: 1rem; } .module { border: 0.5rem solid #AF4629; padding: 1rem; background: #F59131; } .module > div { padding: 1rem; background: #efefef; } .module * + *::after { content: " (lobotomized)"; color: #AF4629; font-style: italic; } body { background: black; margin: 0; padding: 1rem; }
你能够看到其余人也在玩弄它。web
这个小家伙是如此奇怪的字符,个人WordPress网站甚至不会保存他们,因此让咱们在一个Pen(嵌入可能看起来很奇怪,试图看看CodePen自己):浏览器
html:app
<div class="༼•̫͡•༽"> Mr. Ghost </div> <div class="👻"> Mrs. Ghost </div>
csside
.༼•̫͡•༽ { background: GhostWhite; color: darken(GhostWhite, 10); font-family: 'Creepster', cursive; padding: 3rem; text-align: center; font-size: 3rem; margin-bottom: 1rem; } .👻 { background: AntiqueWhite; color: darken(AntiqueWhite, 15); font-family: 'Creepster', cursive; padding: 3rem; text-align: center; font-size: 3rem; } body { margin: 0; padding: 1rem; }
说到不寻常的字符,请记住表情符号也是有效的!工具
<div class="👻"> Mrs. Ghost </div>
.👻 { }
这些选择器也太奇葩了! 但在这种状况下,其中一些字符须要在CSS中转义。 像从蒙特塞拉岛逃离。 或者其余的东西。 幸运的是Mathias Bynens有一个工具。网站
这样意味着咱们能够这样作(codepen):
<div class="~⁰෴⁰~"> Monster </div>
.\~⁰෴⁰\~ { background: lightgreen; text-align: center; padding: 2rem; }
或者是一些毒牙:
<div ^^=^^> OoooOOOO FANGS </div>
[\^\^^=\^\^] { }
the-brain:empty { color: green; }
什么样的选择器是大脑?这是咱们创造了咱们本身的疯狂科学像弗兰肯斯坦的怪物元素。或者只是建立一个自定义元素或什么。
<template> The brain! </template>
var tmpl = document.querySelector('template'); var brainProto = Object.create(HTMLElement.prototype); brainProto.createdCallback = function() { var root = this.createShadowRoot(); root.appendChild(document.importNode(tmpl.content, true)); }; var brain = document.registerElement('the-brain', { prototype: brainProto });
(或许这些天应该是使用customElements.define()?我不知道。)
因此如今咱们的原始选择器将匹配:
<the-brain></the-brain>
可是按选择器的建议,若是咱们这样作它将不匹配:
<the-brain> Fallback content inside. </the-brain>
它甚至不匹配:
<the-brain> </the-brain>
否者咱们将作the-brain:blank {},可是:blank到目前为止也不支持。
什么是不敏感? 除了大多数东西之外的。 咦,这仍然是一个真正奇怪的选择器,对不对?
a[href$=".zip" i] { }
“i”在结尾处告诉属性值,“.zip”能够匹配大小写字符。
有一个来自Wes Bos:
斑马条纹是容易的,对吗?
tr:nth-child(odd) { background: WhiteSmoke; }
可是若是咱们经过将类名从咱们的魔法javascript能见度应用到某些行必定行:
... <tr><td></td></tr> <tr class="BANISHED"><td></tr></tr> <tr><td></td></tr> ...
.BANISHED { display: none; }
如今咱们的斑马条纹是全乱了,像是不少坚果都掉进锅中释放。
选择器4级修复了这个问题:
tr:nth-child(odd of li:not(.BANISHED)) { background: WhiteSmoke; }
这意味着“奇数”只是仍然可见的行计算,斑马条纹将保持不变。它在任何地方都不支持。
还记得咱们建立的自定义元素吗? <the-brain>? 让咱们说,该元素的模板是这样的,在其中有一些实际的HTML:
<template> <div class="braaaain">The brain!</div> </template>
正如咱们已经看到的,你可使用CSS元素会级联到有如你所指望的。但你不能直接把它放在里面的元素。
/* works fine */ the-brain { color: pink; } /* But you can't reach inside like this */ .braaaain { color: red; }
它像是一个封装好的web组件,你能插入到里面,像这样:
html /deep/ .braaaain { color: red; }
这只适用于Blink,但我不知道它是标准的仍是什么。 跨浏览器兼容性的错误。
#container { }
原文出处:《Spoooooky CSS Selectors》
转载时请注明:来自w-rain的我的博客