DEV Community

Gajus Kuizinas
Gajus Kuizinas

Posted on • Edited on

My favorite CSS hack

There is one CSS snippet that I have been copy-pasting for 5 years:

* { 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); }

Enter fullscreen mode Exit fullscreen mode

^ This is one of my favourite inventions.

Update 2020 October. I have since adopted a bit more elaborate snippet:

* { outline: 1px solid rgba(255,0,0,.2); :hover { outline: 1px solid rgba(255,0,0,0.6); } }
* * { outline: 1px solid rgba(0,255,0,.2); :hover { outline: 1px solid rgba(0,255,0,0.6); } }
* * * { outline: 1px solid rgba(0,0,255,.2); :hover { outline: 1px solid rgba(0,0,255,0.6); } }
* * * * { outline: 1px solid rgba(255,0,255,.2); :hover { outline: 1px solid rgba(255,0,0,0.6); } }
* * * * * { outline: 1px solid rgba(0,255,255,.2); :hover { outline: 1px solid rgba(0,255,0,0.6); } }
* * * * * * { outline: 1px solid rgba(255,255,0,.2); :hover { outline: 1px solid rgba(0,0,255,0.6); } }
* * * * * * * { outline: 1px solid rgba(255,0,0,.2); :hover { outline: 1px solid rgba(255,0,0,0.6); } }
* * * * * * * * { outline: 1px solid rgba(0,255,0,.2); :hover { outline: 1px solid rgba(0,255,0,0.6); } }
* * * * * * * * * { outline: 1px solid rgba(0,0,255,.2); :hover { outline: 1px solid rgba(0,0,255,0.6); } }

Enter fullscreen mode Exit fullscreen mode

I first shared it on Quora in 2014 (What are the most interesting HTML/JS/DOM/CSS hacks that most web developers don't know about?) and I still get notifications of someone upvoting this answer daily.

But what does this horrible thing do?

It is meant to be used when you are working with layout, e.g.

Without CSS hack

The problem is that unless the element on the page has a solid background or it is a picture, you do not see how does it fit into the layout, e.g. most of the text nodes, pictures with transparency, etc.

With the above CSS applied you will see something along the lines of:

With CSS hack

Different depth of nodes will use different colour allowing you to see the size of each element on the page, their margin and their padding. Now you can easily identify inconsistencies.

Latest comments (92)

Collapse
 
coswise profile image
Cos

pretty cool!

Collapse
 
murroughfoley profile image
Murrough Foley

A great little tip.

Collapse
 
jstewart8053 profile image
jstewart8053

Genuis!

Collapse
 
vuong profile image
Vuong

It's so nice! :)

Collapse
 
siarheibobryk profile image
Siarhei Bobryk

Thanks for sharing!

Collapse
 
co0kie profile image
co0kie

I do this often and I just add a new style rule directly in devtools as simple as
*{box-shadow: 0 0 0 1px red}

Collapse
 
polemius profile image
polemius

Thank you! This is very useful!

Collapse
 
_smellycode profile image
нιтєѕн кυмαя

Reminds me of Addy's CSS Layout debugger

$$('*').map((A,B)=>A.style.outline=`1px solid hsl(${B*B},99%,50%`)

gist.github.com/addyosmani/fd3999e...

Collapse
 
innovade profile image
Madison Courto

This is great, I wrote a quick extension
chrome.google.com/webstore/detail/...

Collapse
 
joshhumble profile image
Josh Humble

VERY interesting and cool. Thanks for this!

Some comments may only be visible to logged-in visitors. Sign in to view all comments.