Frank Robert Anderson

I am a Developer who does web good and likes to do other stuff good too.

Blog Tutorials Rants Everything Back to My Site

Add Some Style to Your JavaScript Log

Skip the fluff

Backstory

Anytime I want to show off what the web can do with a simple blog I have to pull up this awesome site. I was on A Very Cool Site about Hackery, Math & Design, and the question was asked "how did he do that?"

So it was time to open my console. This is what I found.

Which lead to the question "how did he do that?"

The that in question is the stylish console message. From the image one can see colors and drop shadows. Neat!

How that is done.

I you are not aware already, there are quite a few methods available in the global console object. If you are not familure I recommend the Mozilla Developer Network pages on console. Some methods of importance are:

  • log (duh)
  • assert
  • trace
  • error
  • count

These are old news and I wont go into them in detail here. Just look them up on the MDN docs.

So then how was it done?

Scroll ahead to the Outputting Text to the console portion of the MDN doc and you will see that console.log will take in its first parameter some string wrappers. %f for float, %s for string, and so on. At the very bottom of the page you will see the %c, which wrapps the remaining text in some style as the second parameter.

 console.log("%cHello", "color: red;");

Will render as:

Hello

Results across browsers

In the console. From the screen shots below you will see that this works will all modern browsers (although, I don't have direct access to a PC with IE or Edge at the moment, but I will assume they work too).

Chrome

Firefox Inspector

Firefox Firebug

Appendix

This doesn't work in Edge.

Written on September 10, 2015 by Frank Robert (frob) Anderson

Social Networks

Blogroll

BTMash's blob of contradictions
LA Grafitti
Justin Biard's icodealot
Copyright Frank Robert Anderson 2020