Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 5

Preformatted Text & Code Display

When you need to display text exactly as it is typed - preserving all spaces, tabs, and line breaks - HTML provides the <pre> tag. Combined with the <code> tag for inline code snippets and the <kbd>, <samp>, and <var> tags for keyboard input, sample output, and variables, these elements are essential for technical documentation, tutorials, and any page that displays code or formatted text. If you are building a developer blog, writing documentation, or creating tutorials (like this course!), mastering these tags is critical.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Preformatted and Code Tags

HTML provides several tags specifically for displaying code and preformatted content:

<pre> - Preformatted text. Preserves ALL whitespace, tabs, and line breaks exactly as written in the source code. The browser displays it in a monospace font (like Courier). This is the only HTML element that does not collapse whitespace.

<code> - Inline code. Marks text as a fragment of computer code. Displayed in a monospace font. Used for inline mentions like 'The <code>getElementById()</code> method'. Does NOT preserve whitespace by itself.

<pre><code> - Combined. The standard way to display blocks of code. <pre> preserves formatting, <code> adds semantic meaning. This is exactly how syntax-highlighted code blocks work on sites like GitHub and Stack Overflow.

<kbd> - Keyboard input. Shows text the user should type. Example: 'Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy.' Typically displayed in a monospace font with a subtle border.

<samp> - Sample output. Shows example output from a program or system. Example: 'The command returned: <samp>File not found</samp>.'

<var> - Variable. Represents a mathematical variable or programming variable. Displayed in italic. Example: 'The area is <var>π</var> × <var>r</var>².'

Diagram
Loading diagram…

Combine pre and code tags to preserve indentation in code snippets

Code Display Example

Code Display Example
<!DOCTYPE html>
<html>
<body>
  <h1>Code Display Tags</h1>
  
  <!-- Inline code with <code> -->
  <p>Use <code>document.getElementById()</code> to select elements.</p>
  
  <!-- Block of code with <pre><code> -->
  <h2>JavaScript Example</h2>
  <pre><code>function greet(name) {
  console.log("Hello, " + name);
  return true;
}

greet("World");</code></pre>
  
  <!-- Keyboard input -->
  <p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save.</p>
  <p>Press <kbd>F12</kbd> to open DevTools.</p>
  
  <!-- Sample output -->
  <p>Terminal output: <samp>Build successful. 0 errors.</samp></p>
  
  <!-- Variable -->
  <p>The equation: <var>y</var> = <var>m</var><var>x</var> + <var>b</var></p>
  
  <!-- Pre for ASCII art / formatted text -->
  <h2>ASCII Art</h2>
  <pre>
   /_/  
  ( o.o ) 
   > ^ <
  /|   |  </pre>
</body>
</html>

Try It Yourself: Code Display

Try It Yourself: Code DisplayHTML
HTML
✓ Valid
HTML|29 lines|✓ Valid syntax
UTF-8

Quick Quiz

Module 2 - Knowledge Check

Module 2 - Knowledge Check
Question 1 of 5

Which tag adds semantic importance (tells Google the text matters) in addition to making text bold?

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/5 done