{"id":246781,"date":"2016-10-26T08:11:58","date_gmt":"2016-10-26T15:11:58","guid":{"rendered":"http:\/\/css-tricks.com\/?p=246781"},"modified":"2016-10-26T08:15:25","modified_gmt":"2016-10-26T15:15:25","slug":"spoooooky-css-selectors","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/spoooooky-css-selectors\/","title":{"rendered":"Spoooooky CSS Selectors"},"content":{"rendered":"

Let’s get into the spirit a little bit this year with some Halloween themed posts! I’ll kick it off with some CSS selectors FROM BEYOND THE GRAVE. Er. CSS selectors that will CHILL YOU TO THE BONE. OK maybe not, but they will be at least kinda weird.<\/p>\n

<\/p>\n

The Lobotomized Owl Selector<\/h3>\n

Heydon Pickering made this one famous<\/a> two years ago. I looks like this:<\/p>\n

* + * {\r\n  margin-top: 1.5em;\r\n}<\/code><\/pre>\n

Lobotomized owl? Get it?!<\/p>\n

\"\"
Illustration by Ping Zhu<\/a>, kiped from A List Apart<\/figcaption><\/figure>\n

The idea is that only elements that have a previous sibling<\/em> get margin on top<\/em>. So that you don’t have to do stuff like:<\/p>\n

.the-last-one-so-don't-put-margin-on-me {\r\n  margin-bottom: 0;\r\n}<\/code><\/pre>\n

Instead, you get even spacing kinda for free:<\/p>\n

See the Pen Lobotomized Owl<\/a> by Chris Coyier (@chriscoyier<\/a>) on CodePen<\/a>.<\/p>\n

You can see other people playing around<\/a> with it. <\/p>\n

The Mr. Ghost Selector<\/h3>\n

This little guy is such weird characters that my WordPress site won’t even save them, so let’s do this in a Pen (the embed might look weird too, try looking on CodePen itself):<\/p>\n

See the Pen Ghost Selectors<\/a> by Chris Coyier (@chriscoyier<\/a>) on CodePen<\/a>.<\/p>\n

Speaking of unusual characters, remember that emoji are valid too!<\/p>\n

<div class=\"&#x1f47b;\">\r\n  Mrs. Ghost\r\n<\/div><\/code><\/pre>\n
.&#x1f47b; {\r\n\r\n}<\/code><\/pre>\n

Monster Selectors<\/h3>\n

Speaking of unusual characters, how about some of these dongers<\/a>:<\/p>\n

\"\"<\/figure>\n

Those could be selectors too! But in this case, some of those characters need to be escaped in CSS. ESCAPE FROM MONSTER ISLAND. Or something. Fortunately Mathias Bynens has a tool for that<\/a>.<\/p>\n

\"\"<\/figure>\n

Which means we can do like:<\/p>\n

See the Pen jrRBqB<\/a> by Chris Coyier (@chriscoyier<\/a>) on CodePen<\/a>.<\/p>\n

Or how about some FANGS:<\/p>\n

<div ^^=^^>\r\n   OoooOOOO FANGS\r\n<\/div><\/code><\/pre>\n
[\\^\\^^=\\^\\^] {\r\n  \r\n}<\/code><\/pre>\n

Another Lobotomy Selector<\/h3>\n

How about one like this:<\/p>\n

the-brain:empty {\r\n  color: green;\r\n}<\/code><\/pre>\n

What kind of selector is the-brain? It’s an element we CREATED OURSELVES through MAD SCIENCE like FRANKENSTEIN’S MONSTER. Or just create a custom element<\/a> or whatever.<\/p>\n

<template>\r\n  The brain!\r\n<\/template><\/code><\/pre>\n
var tmpl = document.querySelector('template');\r\n\r\nvar brainProto = Object.create(HTMLElement.prototype);\r\n\r\nbrainProto.createdCallback = function() {\r\n  var root = this.createShadowRoot();\r\n  root.appendChild(document.importNode(tmpl.content, true));\r\n};\r\n\r\nvar brain = document.registerElement('the-brain', {\r\n  prototype: brainProto\r\n});<\/code><\/pre>\n

(Maybe this is supposed to use<\/a> customElements.define()<\/code> these days? I dunno.)<\/p>\n

So now our original selector will match if we do<\/a>:<\/p>\n

<the-brain><\/the-brain><\/code><\/pre>\n

But as the selector suggests, it will not match if we do:<\/p>\n

<the-brain>\r\n  Fallback content inside.\r\n<\/the-brain><\/code><\/pre>\n

It won’t even match:<\/p>\n

<the-brain> <\/the-brain><\/code><\/pre>\n

Otherwise we could do the-brain:blank {}<\/code>, but :blank<\/code><\/a> isn’t really supported yet.<\/p>\n

The Insensitive Selector<\/h3>\n

What’s scarier than insensitivity? Other than MOST ANYTHING. Yah well this is still a really weird looking selector, right? <\/p>\n

a[href$=\".zip\" i] {\r\n\r\n}<\/code><\/pre>\n

The ” i” at the end there is telling the attribute value that “.zip” can match upper or lower case characters.<\/p>\n

This one is from Wes Bos:<\/p>\n

\n

🔥 CSS4 is getting case insensitive matching on attribute selectors! pic.twitter.com\/7LQBi2VUcL<\/a><\/p>\n

— Wes Bos (@wesbos) December 2, 2015<\/a><\/p><\/blockquote>\n