How to change the color of a string in the chrome console?

Sergey :

I know how to change the color of the first line.

console.log ( '%c%s', 'color:' + color, str )

But my string looks like this.

console.log('%c'+ args[args.length-1], 'color:' + color,performance['now'](true, args[args.length-1]),'[(', ...args.slice(0, args.length-1),'*)',`${message}`,']')

It is give my this result: It is give my this result:

How can I change my console.log so that the message '~~~~~~~~~ example ~~~~~~~~~~' also changes color ?

Addition:

I don't understand...

Фll examples are of the form

console.log ( '%c%s', 'color:' + color, str )

I made one line of this kind and my color is preserved. But here is another problem.

    console.log(`%c ${args[args.length-1]} ${performance['now'](true, args[args.length-1])} [(${args.slice(0, args.length-1)} *) ${message}]`, 'color:' + color)

result:

result

But how do I display objects in expanded form?

When trying to add a second color to the console, she does not see this.

console.log('%c'+ args[args.length-1], 'color:' + color,performance['now'](true, args[args.length-1]),'[(', ...args.slice(0, args.length-1),'*)',`%c` +`${message}`,']','color:' + color)

result:

result

Miroslav Glamuzina :

Your format appears to be just a little off:

// Run this in devtools
let example1 = 'performance';
let example2 = JSON.stringify({JSON: {select: 1,transformWith:()=>{}}},null,2);
let message = '~~~~~~~~~ example ~~~~~~~~~';

console.log(`%c${example1}\r\n%c${example2}\r\n%c${message}`, 'color:#bada55', 'color:#ba0055', 'color:#1ada55');
Hope this helps,

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=31806&siteId=1