How do I create formatted javascript console log messages

浪尽此生 提交于 2019-11-28 03:01:31

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.

ochi

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.

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

You can also format substrings.

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

Jonathan

From Google's website: site

console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!