🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Expert Masterclasses.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEhtml

html Documentation

LOADING ENGINE...

Displaying Code

Master the tags for technical documentation: inline code, preformatted blocks, variables, and user input.

code.html
<!-- Inline -->
<p>
Run <code>npm start</code>
</p>
<!-- Block -->
<pre>
<code>
const x = 1;
</code>
</pre>
💻
code.html
1 / 12
💻

Tutor:When writing technical documentation, you often need to display computer code. If you use normal paragraphs, the browser will treat it as regular text. We use specific tags to tell the browser: 'This is code'.


CODE TREE

Unlock nodes by learning how to display code.

Concept 1: Inline Code

The <code> tag is for fragments of code inside regular text. It changes the font family to monospace.

System Check

Does the <code/> tag preserve whitespace by itself?

Community Holo-Net

Showcase Your Code Snippets

Created a beautiful code block? Share your <code> examples.

Enjoying this guide?

Codesyllabus is 100% free and open-source. Support our mission, pay for server infrastructure, and fuel new tutorials by buying us a coffee!

Displaying Code in HTML

Author

Pascual Vila

Frontend Instructor.

When documenting software, showing code examples is crucial. HTML provides specialized tags to handle the display of computer code, distinguishing it from regular text.

Inline Code

The <code> tag is used for inline snippets, such as variable names, functions, or short commands within a sentence. Browsers typically render this in a monospaced font.

Preformatted Blocks

By default, HTML collapses whitespace. To display multi-line code blocks with proper indentation, we use the <pre> tag. It tells the browser to respect your whitespace exactly.

Semantic Inputs & Outputs

Beyond just code, we have <kbd> for keyboard inputs (like shortcuts), <var> for mathematical variables, and <samp> for sample program output.

Code Display Glossary

<code>
Defines a piece of computer code. Used inline. Changes font to monospace.
<pre>
Defines preformatted text. Preserves both spaces and line breaks.
<kbd>
Represents user input, such as keyboard keys or voice commands.
<var>
Defines a variable in programming or a mathematical expression.
<samp>
Represents output from a computer program or system.