JJ Gerrish
JJ Gerrish

Reputation: 882

How can I style the text background color in a console.log?

I'm trying to style a console.log() message so that it looks nicer (background colours, text colours etc...).

I'm using the following code:

console.log('%c Created by' + '%c www.google.com', 'background: #13212E; color: #FFF; padding: 5px 10px;', 'background: #05E5C8; color: #13212E; padding: 5px 10px;');

Which outputs in Chrome's console like:

Console.log() output

As you can see it's almost perfect, but the color style isn't being applied to the link ('www.google.com'), and I also want to remove the white background.

Is there any way to style these properties for a link in a console.log() message?

Upvotes: 8

Views: 1920

Answers (1)

Or Assayag
Or Assayag

Reputation: 6336

const colors = {
 Reset: "\x1b[0m",
 Bright: "\x1b[1m",
 Dim: "\x1b[2m",
 Underscore: "\x1b[4m",
 Blink: "\x1b[5m",
 Reverse: "\x1b[7m",
 Hidden: "\x1b[8m",
 fg: {
  Black: "\x1b[30m",
  Red: "\x1b[31m",
  Green: "\x1b[32m",
  Yellow: "\x1b[33m",
  Blue: "\x1b[34m",
  Magenta: "\x1b[35m",
  Cyan: "\x1b[36m",
  White: "\x1b[37m",
  Crimson: "\x1b[38m"
 },
 bg: {
  Black: "\x1b[40m",
  Red: "\x1b[41m",
  Green: "\x1b[42m",
  Yellow: "\x1b[43m",
  Blue: "\x1b[44m",
  Magenta: "\x1b[45m",
  Cyan: "\x1b[46m",
  White: "\x1b[47m",
  Crimson: "\x1b[48m"
 }
};

console.log(colors.bg.Blue, colors.fg.White , "'%c Created by' + '%c www.google.com'", colors.Reset);

Upvotes: 2

Related Questions