How can I show code in a website using HTML? Basically, I have a C++ program that I'd like to share on my website and I want to show it in the page.
Is there anyway to show a C++ code in HTML other than using HTML text?
HTML includes a tag called <code>
, which is meant for the purpose you describe.
The spec even includes an example class name convention to indicate which language the code is in:
<pre><code class="language-pascal">var i: Integer;
begin
i := 1;
end.</code></pre>
I don’t know of any web browser that supports such a convention (come on, Chrome), but the JavaScript syntax highlighters mentioned in other answers could use it to work their magic.
As you can see in the example, the <code>
tag is usually wrapped in the <pre>
tag, which preserves white space, which is often important for code.