{"id":1788,"date":"2025-07-29T19:45:46","date_gmt":"2025-07-29T17:45:46","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=1788"},"modified":"2025-07-29T19:37:25","modified_gmt":"2025-07-29T17:37:25","slug":"css-in-console-log","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/css-in-console-log","title":{"rendered":"CSS in console.log"},"content":{"rendered":"\n<p>As a web developer, I sometimes use the JavaScript console to debug my code (though I must admit, I do it less and less;  step-by-step debugging is usually more insightful).<\/p>\n\n\n\n<p>I <em>just<\/em> discovered that you can actually add CSS styling to your console messages! You can change the color, size, underline text&#8230; basically make your logs a lot more readable oO<\/p>\n\n\n\n<p>Let\u2019s take a look at how it works, it\u2019s a simple trick that can make a big difference when scanning through console output.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_47_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"ez-toc-toggle-icon-1\"><label for=\"item-69dac557d47ed\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-69dac557d47ed\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/extendsclass.com\/blog\/css-in-console-log\/#Styling_console_output\" title=\"Styling console output\">Styling console output<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/css-in-console-log\/#Using_groups_in_the_console\" title=\"Using groups in the console\">Using groups in the console<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/css-in-console-log\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Styling_console_output\"><\/span>Styling console output<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The <code>%c<\/code> directive lets you apply CSS styles to the part of the message that follows it. You pass the style as the second argument to <code>console.log()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\n  \"Here's a %cslick-looking log message!\",\n  \"font-family: monospace; padding: 8px 12px; margin: 6px 0; border-radius: 4px; font-size: 13px; color: white; background-color: blue;\"\n);<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"606\" height=\"129\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/slick-looking-message.png\" alt=\"\" class=\"wp-image-1792\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/slick-looking-message.png 606w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/slick-looking-message-300x64.png 300w\" sizes=\"(max-width: 606px) 100vw, 606px\" \/><\/figure><\/div>\n\n\n<p>You can even apply <strong>multiple styles<\/strong> in a single <code>console.log()<\/code> call:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log('%cFirst style : %cSecond style', \r\n    'color: green; font-weight: bold;', \r\n    'color: yellow; padding: 2px;'\r\n);\r<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"517\" height=\"108\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/two-styles.png\" alt=\"\" class=\"wp-image-1791\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/two-styles.png 517w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/two-styles-300x63.png 300w\" sizes=\"(max-width: 517px) 100vw, 517px\" \/><\/figure><\/div>\n\n\n<p>For cleaner code, you might define some <strong>predefined styles<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const awesomeStyles = {\n  error: 'background: #D32F2F; color: white;',  \n  warn: 'background: #FE9811; color: white;', \n  log:  'background: #9B19B1; color: white;'  \n};\n\nconsole.log('%c Beautiful error', awesomeStyles.error);\nconsole.log('%c Awesome warning', awesomeStyles.warn);\nconsole.log('%c Great log', awesomeStyles.log);<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"417\" height=\"161\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/predefined-styles.png\" alt=\"\" class=\"wp-image-1790\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/predefined-styles.png 417w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/predefined-styles-300x116.png 300w\" sizes=\"(max-width: 417px) 100vw, 417px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_groups_in_the_console\"><\/span>Using groups in the console<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Beyond CSS, the console also supports <strong>grouping<\/strong>, which helps structure your output by nesting related logs visually.<\/p>\n\n\n\n<p>To use it, call <code>console.group()<\/code>. If you want the group to be collapsed by default, use <code>console.groupCollapsed()<\/code>. To close the current group, call <code>console.groupEnd()<\/code>.<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log(\"A log outside any group\");\n\nconsole.group(\"Our First group\");\nconsole.log(\"blah blah\");\n\nconsole.group(\"Our Second group\");\nconsole.log(\"blah blah\");\nconsole.log(\"Still in the second group\");\nconsole.groupEnd();\n\nconsole.log(\"Back in our first group\");\nconsole.groupEnd();\n\nconsole.debug(\"We are out of all groups now\");<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"605\" height=\"293\" src=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/groups.png\" alt=\"\" class=\"wp-image-1789\" srcset=\"https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/groups.png 605w, https:\/\/extendsclass.com\/blog\/wp-content\/uploads\/2025\/07\/groups-300x145.png 300w\" sizes=\"(max-width: 605px) 100vw, 605px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Hmmm&#8230; not sure it&#8217;s actually useful, but hey, it does exist! More info here:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/console#styling_console_output\">MDN \u2013 Console<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a web developer, I sometimes use the JavaScript console to debug my code (though I must admit, I do it less and less; step-by-step debugging is usually more insightful). I just discovered that you can actually add CSS styling to your console messages! You can change the color, size, underline text&#8230; basically make your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1790,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":""},"categories":[2],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/1788"}],"collection":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/comments?post=1788"}],"version-history":[{"count":2,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/1788\/revisions"}],"predecessor-version":[{"id":1794,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/1788\/revisions\/1794"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/1790"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=1788"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=1788"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=1788"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}