Colors in JavaScript console
Easily colorize your JavaScript console by combining console.log() with CSS directives. Use the '%c' directive to initiate style changes:
With one simple line, you now have red text in your console. You can chain style changes for more impressive effects:
In the above case, "Red" is displayed in red, and "Blue" in blue. The magic trick here is %c
which activates CSS styling for your console text.
Applying styles
Basic Colors: Jazz up your console logs by adding colors straight from CSS:
Error and Warning Highlighting: Make certain messages such as errors and warnings more noticeable with specific color schemes and font styling:
Advanced Styles: For some visual flair in your console, experiment with backgrounds, padding, and border parameters:
Styling Multiple Arguments: Inject different styles for different parts of a single log output:
Personalizing the console
Customizing the color scheme
While color-coding console messages is helpful, having custom color palettes adds a personal touch.
- Leverage Chrome's Custom.css or userContent.css in Firefox to predefine color schemes.
- Use template-colors-web or a similar library for defining complex color templates.
- Mix and match different colors, apply shadow effects or even mix in emojis to create more visually striking console logs.
Filtering for efficient debugging
In a heavy debugging session, visual markers can be extremely beneficial. Utilize the console's built-in filters for Errors, Warnings, or Logs. Use Ctrl + F
or Cmd + F
to quickly find specific log entries.
Getting creative with styles
Ultimately, your console logs should be as unique as you are. Try experimenting with rainbow drop shadows or even gradient text to create visually impressive logs:
Was this article helpful?