EN
JavaScript - style console.log()
3
points
In this article, we would like to show you how to style console.log()
in JavaScript.
Quick solution:
console.log('%cThis is a red text', 'color: red');
Where %c
directive sets color: red
style for the following text.
Practical example
In this example, we use the %c
directive to apply multiple CSS styles to console output.
<!doctype html>
<html>
<body>
<script>
var redText = 'color: red';
var blueText = 'color: blue';
var greenText = 'color: green';
console.log(
'%cThis is red text\n' +
'%cThis is blue text\n' +
'%cThis is green text\n',
redText, blueText, greenText
);
</script>
</body>
</html>
Result:
