HowTo Home


Icon Bar Menu Icon Accordion Tabs Vertical Tabs Tab Headers Top Navigation Responsive Topnav Search Bar Side Navigation Fullscreen Navigation Off-Canvas Menu Hover Sidenav Buttons Horizontal Scroll Menu Vertical Menu Bottom Navigation Responsive Bottom Nav Bottom Border Nav Links Fixed Menu Slide Down Bar on Scroll Sticky Navbar Hover Dropdowns Click Dropdowns Dropdown in Navbar Mega Menu Pagination Breadcrumbs Button Group Vertical Button Group


Slideshow Modal Images Lightbox Responsive Image Grid Image Grid Tab Gallery Image Overlay Image Effects Image Text Transparent Image Text Full Page Image Hero Image Rounded Images Meet the Team Sticky Image Flip an Image Shake an Image Portfolio Gallery Portfolio with Filtering


Alert Buttons Animated Buttons Social Media Buttons Loading Buttons Next/prev Buttons Round Buttons Scroll To Top Button


Login Form Signup Form Contact Form Newsletter Responsive Form Clear Input Field Copy Text to Clipboard Animated Search Custom Checkbox/Radio Toggle Switch Password Validation Toggle Password Visibility Multiple Step Form


Filter List Filter Table Filter Elements Sort List Sort Table


Fullscreen Video Modal Boxes Timeline Progress Bars Skill Bar Range Sliders Tooltips Popups Calendar Zebra Striped Table Responsive Tables HTML Includes To Do List Loaders Star Rating User Rating Overlay Effect Contact Chips Cards Profile Card Alerts Notes Labels Circles Coupon Fixed Footer Equal Height Clearfix Snackbar Scroll Drawing Pricing Table Parallax Aspect Ratio Build a Web Site Toggle Like/Dislike Toggle Hide/Show Toggle Class Add Class Remove Class Arrows Shapes Browser Window Custom Scrollbar Device Look Placeholder Color Vertical Line Animate Icons Countdown Timer Typewriter Coming Soon Page Center Website Thumbnails Chat Messages Testimonials Draggable HTML Element Syntax Highlighter JS Animations Contact Section


2 Column Layout 3 Column Layout 4 Column Layout Expanding Grid Mixed Column Layout


Google Maps Google Translate Google Charts Google Fonts


Convert Weight Convert Temperature Convert Length Convert Speed

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>

<h1>Testing an HTML Syntax Highlighter</h2>
<p>Hello world!</p>
<a href="">Back to School</a>


How To Create a Syntax Highlighter

Step 1) Add HTML:


A DIV containing HTML code:

<div id="myDiv">
&lt;!DOCTYPE html&gt;<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=""&gt;Back to School&lt;/a&gt;<br>
Step 2) Add JavaScript:


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


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