5

I would like to render HTML from a console.(log/info/warn):

console.html("<h1>Hello!</h1>");

would render…

Hello!

…into the console panel. Is it possible somehow?

ps.: for the record, I would like to have more coloring options than log/info/warn/error messages.

3
  • 1
    why dont you use console.log(document.body); Commented Mar 22, 2016 at 20:02
  • Is console.html exist even ? Commented Mar 22, 2016 at 20:05
  • you can also use console.table to render an array or array of objects: developer.mozilla.org/en-US/docs/Web/API/Console/table Commented Mar 25, 2021 at 19:06

1 Answer 1

18

In a way it's possible using the CSS format specifier.

Example:

console.log("%cThis will be formatted with large, blue text", "color: blue; font-size: x-large");

The CSS format specifier allows you to customize the display in the console. Start the string with the specifier and give the style you wish to apply as the second parameter.

This feature is supported by all main browser developer tools. See the reference for the Chrome DevTools, the one for the Firefox DevTools or the one for Firebug.

So while you can't use HTML per se, you can style whatever text you want using CSS to mimic many HTML elements:

console.log("%cHello!", "font-size: 3em");

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah baby yeah. Very nice. In my MacOS, works in all browsers except Opera DragonFly.
Nice, but HTML tags are not interpreted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.