我最喜欢的CSS hack

3,510 阅读1分钟

有一个我已经复制粘贴5年的CSS片段:

* { background-color: rgba(255,0,0,.2); }
* * { background-color: rgba(0,255,0,.2); }
* * * { background-color: rgba(0,0,255,.2); }
* * * * { background-color: rgba(255,0,255,.2); }
* * * * * { background-color: rgba(0,255,255,.2); }
* * * * * * { background-color: rgba(255,255,0,.2); }
* * * * * * * { background-color: rgba(255,0,0,.2); }
* * * * * * * * { background-color: rgba(0,255,0,.2); }
* * * * * * * * * { background-color: rgba(0,0,255,.2); }

这是我最喜欢的发明之一。

2014年,我首次在Quora上分享了它(What are the most interesting HTML/JS/DOM/CSS hacks that most web developers don't know about?),现在,我每天仍然收到有人支持这个答案的通知。

那么,这个可怕的代码片段做了什么?

它意味着当你使用布局时适用,比如:

time-01

问题是,除非页面上的元素具有纯色背景或者一张图片,否则你看不到它是如何适合布局。例如大多数的文本节点,具有透明度的图片等。

应用上面的CSS,你会看到类似(下面)的东西:

time-02

不同深度的节点使用不同的颜色。允许你查看页面上每个元素的大小,它们的边距和填充。现在,你可以容易地识别出不一致性。

参考和后话