昨日のオフィスで、奇妙で魔法のような CSS のトリックを思いつきました。次の例をご覧ください。空のリンクが見やすく表示されます。
a[href = ""] {
background: red;
color: white;
font-size: x-large;
}
jsFiddle でのライブサンプルを確認する
絶対リンクは、相対リンクとは異なるスタイルで設定することもできます。
a[href ^= http] {
display: inline-block;
color: red;
transform: rotate(180deg);
}
jsFiddle でのライブサンプルを確認する
自分のドメイン外を指すリンクに別のスタイルを使用したい場合は、:not() セレクタを使用できます。HTML5Rocks の外部リンクの横に小さな矢印が表示されているのは、このとおりです。
a[href ^= 'http']:not([href *= 'html5rocks.']) {
background: transparent url(arrow.png) no-repeat center right;
padding-right: 16px;
}
jsFiddle でのライブサンプルを確認する
リンクのスタイル設定以外でも、PNG 画像を反転させる方法は次のとおりです。
img[src $= .png] {
filter: invert(100%);
}
属性セレクタから進んで、ドキュメント ヘッドとその他の要素を表示できることをご存じですか?
head {
display: block;
border-bottom: 5px solid red;
}
script, style, link {
display: block;
white-space: pre;
font-family: monospace;
}
または、CSS の attr 関数を使用して :after と :before コンテンツを埋めることもできます。
script:before {
content: "<script src=\"" attr(src) "\" type=\"" attr(type) "\">";
}
script:after {
content: "</script>";
}
style:before {
content: "<style type=\"" attr(type) "\">";
}
style:after {
content: "< /style>";
}
/* And for a finish, <link> */
link:before {
content: "<link rel=\"" attr(rel) "\" type=\"" attr(type) "\" href=\"" attr(href) "\" />";
}
jsFiddle でのライブサンプルを確認する
attr() は一致する要素の属性値を読み取るので、#foo:before に対して使用すると、#foo から属性を読み取ります。