To display computer code in HTML, you can use the and
tags. The tag is used to wrap inline code snippets, while the tag is used for displaying preformatted blocks of code. Here's an example:
Inline code:
html
<p>This is an example of <code><code></code> tag.</p>
Preformatted code block:
html
<pre>
<code>
function greet() {
console.log('Hello, world!');
}
greet();
</code>
</pre>
In the example above, the inline code snippet <code><code></code> is displayed as <code>, using HTML entities to escape the angle brackets. The preformatted code block is wrapped in the <pre> tag, and the code itself is enclosed within the <code> tags. The indentation and formatting of the code are preserved when using the <pre> tag.
You can further enhance the presentation of code snippets using CSS classes or libraries like Prism or Highlight.js for syntax highlighting and additional styling.