如何在console.log中设置文本背景颜色的样式?

时间:2019-05-10 08:50:18

标签: javascript google-chrome google-chrome-devtools console.log

我正在尝试设置console.log()消息的样式,使其看起来更好(背景颜色,文本颜色等)。

我正在使用以下代码:

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

Chrome控制台中的输出如下:

Console.log() output

如您所见,它几乎是完美的,但是color样式并未应用于链接('www.google.com'),我也想删除白色背景。

有什么方法可以为console.log()消息中的链接设置这些属性的样式?

1 个答案:

答案 0 :(得分:0)

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);