Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show code (specifically C++) in an HTML page?

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?

like image 982
hhdgfdg Avatar asked Apr 19 '11 00:04

hhdgfdg


People also ask

HOW include C code in HTML?

You can include or embed C code in an HTML document, but it will not do anything; it will taken just as text data. There is no support in browsers for using C as client-side scripting language. Theoretically, you could write an interpreter for C (or a subset of C) in JavaScript.

How do I display code snippets in HTML?

HTML provides many methods for text-formatting but <code> tag is displayed with fixed letter size, font, and spacing. Some points about <code> tag: It is mainly used to display the code snippet into the web browser.

How do you display code on a website?

To view only the source code, press Ctrl + U on your computer's keyboard. Right-click a blank part of the web page and select Page source from the pop-up menu that appears.

How do I get the code from an HTML file?

On Firefox, you can also use the keyboard shortcut Command-U to view the source code of a webpage. On Chrome, the process is very similar. Navigate to the top menu item “View” and click on “Developer/View Source.” You can also use the keyboard shortcut Option-Command-U .


1 Answers

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.

like image 65
Paul D. Waite Avatar answered Sep 28 '22 05:09

Paul D. Waite