How to create formatted javascript console log messages

Now I switched to the console in Chrome on Facebook.

Surprisingly, I received this message in the console.


Now my question is:
How is this possible?
I know that there are several β€œexploit” methods for the console, but how can you do this font formatting in the console? (and is it console.log?)

+59
javascript html google-chrome webkit exploit
Mar 03 '14 at 19:45
source share
4 answers

Yes, you can format console.log() something like this:

 console.log("%cExtra Large Yellow Text with Red Background", "background: red; color: yellow; font-size: x-large"); 

Note the %c before the text in the first argument and the style specifications in the second argument. The text will look like your example.

See Google Styling Connectors Using CSS or the FireBug Console Documentation for more details.

Documentation links also include some other neat tricks, such as including object references in the console log.

+82
Mar 03 '14 at 19:53
source share

Try the following:

 console.log("%cThis will be formatted with blue text", "color: blue"); 

Quoting documents

You use the% c format specifier to apply custom CSS rules to any string that you write to the console using console.log () or its associated Methods.

Source: https://developers.google.com/web/tools/chrome-devtools/console/console-write#styling_console_output_with_css

+18
Mar 03 '14 at 19:52
source share

You can also format substrings.

 var red = 'color:red'; var blue = 'color:blue'; console.log('%cHello %cWorld %cfoo %cbar', red, blue, red, blue); 

enter image description here

+7
Nov 24 '16 at 11:28
source share

On Google website : website

 console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large"); 
+6
Mar 03 '14 at 19:54
source share



All Articles