I 'waddled' by the Console in Chrome on Facebook today.
Surprisingly I got this message in the console.
Now my question is:
How is this possible?
I know that there are a few 'exploit' methods for the console, but how can you make such font formatting in the console? (and is it console.log?)
Asked
Active
Viewed 6.9k times
110

Smern
- 18,746
- 21
- 72
- 90

Anders Kjeldsen
- 1,224
- 2
- 10
- 14
5 Answers
153
Yes, you can format the console.log()
with something like this:
console.log("%cExtra Large Yellow Text with Red Background", "background: red; color: yellow; font-size: x-large");
Note the %c
preceding the text in the first argument and the style specifications in the second argument. The text will look like your example.
See Google's "Styling Console Output with CSS" or FireBug's Console Documentation for more details.
The documentation links also include some other neat tricks like including object links in a console log as well.

Smern
- 18,746
- 21
- 72
- 90
-
You might want to check that a compatible browser is in use before trying to use format strings in `console.log`, since older browsers might stop your script with an exception. [caniuse says](https://caniuse.com/#feat=mdn-api_console_log_substitution_strings) it was introduced in Firefox 9 and Edge 79; Chrome started supporting it evidently some time before Chrome 83 but we don't know exactly when. – Silas S. Brown Jun 24 '20 at 13:58
44
Try this:
console.log("%cThis will be formatted with blue text", "color: blue");
Quoting the docs,
You use the %c format specifier to apply custom CSS rules to any string you write to the Console with console.log() or related methods.
-
10Hi downvoter, any comments to add? - it's hard to improve an answer (that you think is not good) when you don't leave comments explaining yourself. :) – blurfus Jul 26 '18 at 18:15
36
You can also format substrings.
var red = 'color:red';
var blue = 'color:blue';
console.log('%cHello %cWorld %cfoo %cbar', red, blue, red, blue);

Krzysztof Boduch
- 675
- 9
- 12
-
4Note that it is only possible to style within the first argument of `console.log` and the styles must follow immediately. – Qwerty Apr 06 '18 at 12:09
3
Just extending other answers and you can reuse the existing css style of selector, class, element. Try this in SO console window.
const customLog = (message, cssSelector) =>
console.log(
`%c${message}`,
Object.entries(getComputedStyle(document.querySelector(cssSelector)))
.map(([k, v]) => `${k}:${v}`)
.join(";")
);
customLog("Hello StackOverflow", "#question-header > div > a");
customLog("Hello StackOverflow", "strong");
customLog("Hello StackOverflow", "a");

Siva K V
- 10,561
- 2
- 16
- 29