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:
xxxxxxxxxx
1
console.log('%cThis is a red text', 'color: red');
Where %c
directive sets color: red
style for the following text.
In this example, we use the %c
directive to apply multiple CSS styles to console output.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<script>
5
6
var redText = 'color: red';
7
var blueText = 'color: blue';
8
var greenText = 'color: green';
9
10
console.log(
11
'%cThis is red text\n' +
12
'%cThis is blue text\n' +
13
'%cThis is green text\n',
14
redText, blueText, greenText
15
);
16
17
</script>
18
</body>
19
</html>
Result:
