MyInternships.in

HTML Basics

HTML Computer Code Elements

HTML has special elements for showing computer code, keyboard keys, and program output. They render text in a monospace font and, more importantly, give it the right meaning.


What are Computer Code Elements?

These are semantic elements for technical content. The <code> element marks a snippet of code, <pre> preserves exact whitespace, <kbd> represents keyboard input, <samp> represents program output, and <var> marks a variable.

By default browsers show these in a monospace (fixed-width) font, which lines characters up neatly — exactly what you want for code.

Basic Usage

Inline code inside a sentence
Example
<p>Use the <code>console.log()</code> function to print a message.</p>

Example: Code, Keys, and Output

Each element renders differently
Example
<!DOCTYPE html>
<html>
<body>
  <p>Type <code>npm start</code> to run the app.</p>
  <p>Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to stop it.</p>
  <p>The program printed: <samp>Server running on port 3000</samp></p>
  <p>The formula uses the variable <var>x</var>.</p>
</body>
</html>

More Examples

A multi-line code block with <pre> and <code>
Example
<pre style="background:#1e1e1e; color:#dcdcdc; padding:12px; border-radius:6px;">
<code>function greet(name) {
  return "Hello, " + name;
}</code>
</pre>
Keyboard shortcut list
Example
<ul>
  <li><kbd>Ctrl</kbd> + <kbd>S</kbd> — Save</li>
  <li><kbd>Ctrl</kbd> + <kbd>Z</kbd> — Undo</li>
</ul>

Code Elements Reference

ElementRepresents
<code>A fragment of computer code.
<pre>Preformatted text with exact whitespace.
<kbd>Keyboard input from the user.
<samp>Sample output from a program.
<var>A variable or placeholder name.
💡

Combine <pre> and <code> for multi-line code blocks: <pre> keeps the line breaks and indentation, and <code> marks the content as code.

⚠️

Inside <code> or <pre>, the < and > characters are still parsed as tags. To show them literally, use the entities &lt; and &gt;.

Key Takeaways

  • <code> marks inline code; <pre> preserves whitespace.
  • <kbd> shows keyboard input; <samp> shows program output.
  • <var> marks a variable name.
  • Escape < and > as &lt; and &gt; to show them as text.

Related HTML Topics

Keep learning with these closely related tutorials.

Ready to use your HTML skills?

Find web development internships and fresher jobs across India.

Browse Web Dev Internships