DEV Community

Cover image for Sick Console Bro! How To Style Your console.log with CSS
Jack Harner ๐Ÿš€
Jack Harner ๐Ÿš€

Posted on โ€ข Originally published at harnerdesigns.com

17 7

Sick Console Bro! How To Style Your console.log with CSS

Originally Posted On Harner Designs | Photo by Glenn Carstens-Peters on Unsplash

In Chrome and Firefox (>31) you can add CSS styles to your console.log() messages. It's fairly simple and straightforward.

All you need to do is include a %c string before your log message and then pass your CSS as a parameter to the console.log( ) function. Like so:

console.log("%c{{Log Message}}", "{{CSS}}");
Enter fullscreen mode Exit fullscreen mode

For example, this code runs on my portfolio:

console.log("%cHarner Designs", "color:#233E82; font-family:'Ubuntu'; display: block;font-weight:bold; font-size:48px; background:#fff;");
    console.log("%cLike to dig into the meaty bits of the website?\nThanks for looking! Hit us up on Twitter @harnerdesigns!", "color:#222; font-family:'Ubuntu'; font-weight:100; font-size:24px; background:#fff;");
    console.log("%cDid you know you can style your console output?!", "color:#333; font-family:'Ubuntu'; font-weight:100; font-size:18px; background:#fff;");
    console.log("%cCheck our blog to learn how: https://harnerdesigns.com/blog/style-your-console-log-with-css/", "line-height: 3em; padding: 0.5em; text-align: center; border: 1px dotted #aaa; background:#fff; font-size: 14px;");
Enter fullscreen mode Exit fullscreen mode

and outputs like this to the console:

Styling Multiple Strings In One Log

It's also possible to include multiple strings in one command and style them differently. Check it out:

console.log("%cString1" + "%cString2", "{{CSS for String1}}", "{{CSS for String2}}");
Enter fullscreen mode Exit fullscreen mode

Reusing Styles Across Log Messages

You can also store the CSS You want to apply to a variable and then pass that to multiple console.logs:

var consoleStyle = "{{Reusable CSS}}";
console.log("%cString1", consoleStyle);
console.log("%cString2", consoleStyle);
Enter fullscreen mode Exit fullscreen mode

Conclusion

Do you leave any little easter eggs in your console? Could you see a use case for this in your own projects? I'd love to know down in the comments! Show me some examples of cool things you've found in console messages.

Recent Blog Posts

Image of Datadog

The Future of AI, LLMs, and Observability on Google Cloud

Datadog sat down with Googleโ€™s Director of AI to discuss the current and future states of AI, ML, and LLMs on Google Cloud. Discover 7 key insights for technical leaders, covering everything from upskilling teams to observability best practices

Learn More

Top comments (9)

Collapse
 
daviddalbusco profile image
David Dal Busco โ€ข

Not sure anyone still use IE <= v9 but if so, it would need the workaround described by Andy E on stackoverflow.com/questions/547293... in order to print to the console

Collapse
 
daviddalbusco profile image
David Dal Busco โ€ข โ€ข Edited

Oh and of course a screenshot of our upcoming open source editor for presentations ๐Ÿ˜‰

Collapse
 
tr11 profile image
Tiago Rangel โ€ข

DeckDeckGo

"hmmm... sounds weirdly familiar"

Collapse
 
azza85 profile image
aaron โ€ข

Nice one I've seen this on a couple of sites but had never looked into how to do it

Collapse
 
jackharner profile image
Jack Harner ๐Ÿš€ โ€ข

I don't think I've ever stumbled across a styled console like this in the wild yet. At least not that I can remember.

I see ASCII Art all the time though. I.e. from Facebook:

Dev.to does it as well!

Collapse
 
ijason profile image
Jason โ€ข

This is awesome! Thanks for sharing

Collapse
 
jackharner profile image
Jack Harner ๐Ÿš€ โ€ข

No problem!

Collapse
 
ben profile image
Ben Halpern โ€ข
Collapse
 
jackharner profile image
Jack Harner ๐Ÿš€ โ€ข

Love it!

๐Ÿ‘‹ Kindness is contagious

Please leave a โค๏ธ or a friendly comment on this post if you found it helpful!

Okay