Syntax Highlighting for the Web
Highlight.js is a syntax highlighter written in JavaScript. It works in the browser as well as on the server with Node.js. The highlight function detects the language automatically.
function greet(name) {
// This is a syntax highlighted code block
if (name) {
console.log(`Hello, ${name}!`);
} else {
console.log("Hello, stranger!");
}
}
greet("World");
.language-javascript class applied automatically
⚡
Lightweight
Core is small (~9.5KB) and fast
🌐
Multi-Language
Supports 180+ languages
🧠
Autodetect
Automatic language detection
How It Works
1. Include CSS
<link rel="stylesheet" href="/highlight.js.min.css">
2. Apply Classes
<pre><code class="language-javascript">...</code></pre>
CSS Code
body {
background-color: #f0f0f0;
color: #333;
transition: background 0.3s, color 0.3s;
}
.dark {
background-color: #121212;
color: #f0f0f0;
}
Python Code
def fibonacci(n):
if n <= 1:
return n
else:
return fibonacci(n-1) + fibonacci(n-2)
# Print the first 10 Fibonacci numbers
for i in range(10):
print(fibonacci(i))