Chapter 34: Marking up computer code

Block with <per> and <code>

If the formatting (white space, new lines, indentation) of the code matters, use the pre element in combination with
the code element:

<pre>
 <code>
 x = 42
 if x == 42:
 print "x is … … 42"
 </code>
</pre>

You still have to escape characters with special meaning in HTML (like < with <), so for displaying a block of HTML code (<p> This is a paragraph.</p>), it could look like this:

<pre>
 <code>
 &lt;p>This is a paragraph.&lt;/p>
 </code>
</pre>

Inline with <code>

If a sentence contains computer code (for example, the name of an HTML element), use the code element to mark it
up:

Advertisement
<p>The <code>a</code> element creates a hyperlink.</p>

Recent Posts

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago