CSS formatting in the console
I was poking around in Google Lens recently (which is Google’s magical reverse image lookup service) and I was mildly surprised by what I saw in the developer tools console:

Code that runs in the dev console can be very powerful, so I can understand why they want to stop people from pasting random code in here – although it feels like a bigger issue on, say, your Gmail inbox, rather than image search. I’d never heard the name Self-XSS before, but it definitely happens, and trying to reduce it is no bad thing.
After I’d stared at it for a few moments, and thought “that’s neat”, I got to wondering: how are Google formatting these messages? How are they making their text big and bold and red and yellow? Having a way to make console messages stand out would be quite handy.
It turns out that if you include %c
in your console.log
, you can then pass CSS styles as extra arguments. Here are a few examples:

You can also include multiple %c
specifiers, and multiple CSS snippets, to style different parts of your message differently:

This felt weird to me at first, but now I write this post I realise it’s just the %
-style format strings from C which I’ve been using my entire career, and which appear in a bunch of other languages. But I’ve always used %
-substitutions for values, never styles. That’s what feels new.
I’m not sure I’d use this for production code, but I can imagine it’d be useful for debugging. I make heavy use of console.log()
, and being able to highlight particular values would help me find them that bit faster (especially if it’s a chatty site).
This doesn’t work everywhere, but it does seem to work in the major browsers, and there’s documentation for it on MDN.
A few years ago, at a JavaScript meetup, I made a joke that “my web development knowledge starts and ends with console.log
“. On days like today, it’s clear that I still have more to learn.