Add Styles to Console Statements

Read the full article at: Add Styles to Console Statements

O'Reilly

I was recently checking out Google Plus because they implement some awesome effects.  I opened the console and same the following message:

WARNING!

Using this console may allow attackers to impersonate you and steal your information using an attack called Self-XSS.

Do not enter or paste code that you do not understand.

I wasn’t surprised to see that message but what I did notice was that the text was red and the background was yellow.  The text was even a bit bigger.  How did they do it?  Pretty easily:

console.log("%c%s",
            "color: red; background: yellow; font-size: 24px;",
            "WARNING!");

The first argument is the order of style and message, the second is the style set, and the last is the desired message.

As to why you’d want to use this?  If it helps you identify debug information easier in the console, you may consider calling more attention to some messages!

Leave a Reply

Your email address will not be published. Required fields are marked *