昨天在办公室,我们想出了一些奇怪而神奇的 CSS 技巧。例如,下方这个链接会使空链接非常显眼:
a[href = ""] {
background: red;
color: white;
font-size: x-large;
}
您还可以为绝对链接设置与相对链接不同的样式:
a[href ^= http] {
display: inline-block;
color: red;
transform: rotate(180deg);
}
如果您想为指向您网域之外的链接采用不同的样式,可以使用 :not() 选择器。实际上,我们在 HTML5Rocks 上为外部链接添加小箭头就是通过这种方式。
a[href ^= 'http']:not([href *= 'html5rocks.']) {
background: transparent url(arrow.png) no-repeat center right;
padding-right: 16px;
}
谨在此提醒您,您可以对链接以外的元素设置样式,下面介绍了如何将所有 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 属性函数的强大功能来填充 :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) "\" />";
}
请注意,attr() 会读取匹配元素的属性值,因此,如果您将其用于 #foo:before,它会读取 #foo 中的属性。