DEV Community

lucky chauhan
lucky chauhan

Posted on

1

The Secret Trick to Customizing Console Logs with CSS

Tired of boring, plain-text console logs? Did you know you can style your browser’s console output with colors, custom fonts, backgrounds, and even animations—all using a simple JavaScript trick?

In this post, you’ll learn how to use the %c formatter to apply CSS styling to console.log() messages, making debugging more visually intuitive and even fun!


✨ Why Style Console Logs?

Before we dive into the code, here’s why you should care:

Highlight important logs (errors in red, warnings in yellow)

Organize debug output (API calls vs. state changes)

Make debugging more enjoyable (because who doesn’t love colors?)


🛠 How It Works: The %c Formatter

The secret is the %c placeholder in console.log(). It tells the browser: “The next argument is CSS—apply it to this text!”

Basic Syntax

console.log("%cStyled Text", "CSS goes here");
Enter fullscreen mode Exit fullscreen mode

Example: Colored & Bold Log

console.log(
  "%cHello, Console!",
  "color: white; background: #007BFF; font-weight: bold; padding: 4px;"
);
Enter fullscreen mode Exit fullscreen mode

This logs a blue-background, bold, white text message.


🎨 Advanced Styling Tricks

1. Multiple Styles in One Log

Use multiple %c placeholders for different styles:

console.log(
  "%cError:%c File not found!",
  "color: red; font-weight: bold;",
  "color: #333;"
);
Enter fullscreen mode Exit fullscreen mode

2. Backgrounds, Borders & Padding

Make logs stand out like notifications:

console.log(
  "%cSUCCESS",
  "background: #28A745; color: white; padding: 4px 8px; border-radius: 4px;"
);
Enter fullscreen mode Exit fullscreen mode

3. Fun with Fonts & Shadows

console.log(
  "%c✨ Debug Mode Activated!",
  "font-family: sans-serif; font-size: 18px; text-shadow: 1px 1px 2px #ccc;"
);
Enter fullscreen mode Exit fullscreen mode

🚀 Practical Uses

  • Color-code log levels (errors, warnings, info)
  • Highlight API request/response logs
  • Make debugging sessions less monotonous

🔍 Try It Yourself!

  1. Open DevTools (F12 or Ctrl+Shift+I).
  2. Paste any example above into the console.
  3. Experiment with your own styles!

Pro Tip: Combine this with console.group() for neatly organized logs.


💬 Your Challenge

What’s the craziest style you can create? Try:

  • Gradient text
  • Animated logs (using @keyframes)
  • A full-width console "header"

📌 Key Takeaways

✔ Use %c to inject CSS into console.log().

✔ Style text, backgrounds, borders, and more.

✔ Great for debugging, logging, and fun experiments.

Now go make your console logs pop with color! 🎉


Tags: #JavaScriptTips #WebDevelopment #Debugging #FrontEnd

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (1)

Collapse
 
nadim_mahmud_e7a2ff078389 profile image
Nadim Mahmud

It was great and informative thank you keep it up?🐧

Tiger Data image

🐯 🚀 Timescale is now TigerData: Building the Modern PostgreSQL for the Analytical and Agentic Era

We’ve quietly evolved from a time-series database into the modern PostgreSQL for today’s and tomorrow’s computing, built for performance, scale, and the agentic future.

So we’re changing our name: from Timescale to TigerData. Not to change who we are, but to reflect who we’ve become. TigerData is bold, fast, and built to power the next era of software.

Read more

👋 Kindness is contagious

Delve into a trove of insights in this thoughtful post, celebrated by the welcoming DEV Community. Programmers of every stripe are invited to share their viewpoints and enrich our collective expertise.

A simple “thank you” can brighten someone’s day—drop yours in the comments below!

On DEV, exchanging knowledge lightens our path and forges deeper connections. Found this valuable? A quick note of gratitude to the author can make all the difference.

Get Started