DEV Community

Gajus Kuizinas
Gajus Kuizinas

Posted on • Updated 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!

Collapse
 
realoscarcastro profile image
Oscar Castro

Awesome thanks!

Collapse
 
chrisnager profile image
Chris Nager • Edited

Ha, nice! I created a Chrome plugin, simple-debug.css, a few years back that does something similar with the following CSS:

*{outline:1px solid #fff!important;background-color:rgba(0,2,54,.1)!important}
Collapse
 
heygambo profile image
Christian Gambardella

lol just wanted to post that idea here.

Collapse
 
_juliettech profile image
juliette_chevalier • Edited

There's also a plug in called Pesticide (Chrome) & Open Pesticide (Firefox) that does this by showing you the size of all divs in your page. Really handy for identifying which div is causing the bug.
See example here

Collapse
 
kennickv profile image
Ken Vermeille

Thanks for the tip!

Collapse
 
tariq profile image
Tariqul Islam

thanks a lot, sir.

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