A Snazzy Trick: How To Style Your console.log() Messages

March 25, 2021 • 2 min read

Presentational content

A quick guide (with examples) on applying CSS to log messages in the browser's console.

// Styling console.log messages with CSS
console.log("%cHello World!", "color: blue; font-size: 20px;");

THE PROBLEM

Our console messages don't grab users' attention.

Basic console message

A basic console.log message.

How can we make them more engaging?

A SOLUTION

By including a %c in console.log's first argument, and passing some CSS declarations as the second argument, we can style the message on the browser's console.

style console message

A styled console log message! 🌈

Here are some ideas

color console message

Change color in your browser's console.

font family console message

Change font-family and font-size in your browser's console.

several styles

Apply several styles to the same string.

reuse styles

Reuse console.log styles.

Many websites use this trick to grab the curious developers' attention or to prevent non-technical users to run code on the console. Some of them are Facebook, Pinterest, and Gmail.

Thanks for reading! Take care, Jon Portella.


WDYT about this post? I read you here:

Get notified of new blog posts?

RSS FeedGet an Email

RSS is the correct choice, btw.