Any way of detecting whether a browsers console is able to render colors?

前端 未结 2 1843
予麋鹿
予麋鹿 2021-02-12 09:58

Is there a way of checking to see if a browser is capable of using console colors without sniffing?

console.log(\'%c Oh my heavens! \', \'background: #222; color         


        
2条回答
  •  闹比i
    闹比i (楼主)
    2021-02-12 10:25

    I wrote Console.js https://github.com/icodeforlove/Console.js to allow us to do this a bit easier

    Console.styles.register({
      red: 'color: red',
      underline: 'text-decoration: underline',
      bold: 'font-weight: bold'
    });
    

    then you can just do this

    console.log('red text '.red + 'red underlined text'.red.underline + 'red bold text'.red.bold);
    

    it will gracefully degrade like this

    console.js example

提交回复
热议问题