How TO - Create a Syntax Highlighter


Learn how to create and use a Syntax Highlighter.


Syntax Highlighter

Code snippets are easier to read when you add some color:

<!DOCTYPE html>
<html>
<body>

<h1>Testing an HTML Syntax Highlighter</h2>
<p>Hello world!</p>
<a href="https://www.w3schools.com">Back to School</a>

</body>
</html>


How To Create a Syntax Highlighter

Step 1) Add HTML:

Example

A <div> containing HTML code:

<div id="myDiv">
&lt;!DOCTYPE html&gt;<br>
&lt;html&gt;<br>
&lt;body&gt;<br>
<br>
&lt;h1&gt;Testing an HTML Syntax Highlighter&lt;/h2&gt;<br>
&lt;p&gt;Hello world!&lt;/p&gt;<br>
&lt;a href="https://www.w3schools.com"&gt;Back to School&lt;/a&gt;<br>
<br>
&lt;/body&gt;<br>
&lt;/html&gt;
</div>
Step 2) Add JavaScript:

Example

Create a function called "w3CodeColor" and call it, using the DIV element as a parameter:

w3CodeColor(document.getElementById("myDiv"));

function w3CodeColor(elmnt) {
  // click "Try it Yourself" to see the JavaScript...
}
Try it Yourself »

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.