Falytom logoFALYTOM
Games
ToolsUI ComponentsServicesProductsAbout
LoginSign Up
menu
  1. Home
  2. /Web Development
  3. /Html
  4. /Preformatted Code
Your Progress0%
0 of 76 completed

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data

Preformatted Text and Code

Displaying code and preserving formatting in HTML

When you need to display code snippets, computer output, or text where spacing and line breaks matter, HTML provides special elements that preserve formatting and use monospace fonts.

The Pre Element

The <pre> element (short for "preformatted") displays text exactly as it's written in your HTML, preserving all spaces, line breaks, and tabs. By default, it also uses a monospace font.

pre-basic.html
<pre>
This    text    has    extra    spaces
And multiple
    lines
        with indentation
</pre>

The browser displays this exactly as written:

This text has extra spaces And multiple lines with indentation

When to Use Pre

  • ASCII art or diagrams
  • Poetry or lyrics where line breaks are important
  • Code blocks where indentation matters
  • Computer output or logs

The Code Element

The <code> element is used to mark up inline code snippets or programming-related text. It displays text in a monospace font but doesn't preserve whitespace like <pre>.

code-inline.html
<p>
  Use the <code>console.log()</code> function to output messages 
  to the browser console.
</p>

<p>
  The <code>background-color</code> CSS property changes the 
  background of an element.
</p>

Combining Pre and Code

For displaying multi-line code blocks, it's best practice to combine both elements:

pre-code-combined.html
<pre><code>function greet(name) {
  console.log("Hello, " + name);
  return true;
}

greet("World");</code></pre>

This combination tells browsers: "This is code (semantic meaning) that should preserve formatting (visual presentation)."

The Kbd Element

The <kbd> element (short for "keyboard") represents user input, typically from a keyboard. It's used to show which keys or commands a user should type.

kbd-element.html
<p>
  Press <kbd>Ctrl</kbd> + <kbd>C</kbd> to copy the selected text.
</p>

<p>
  To save your file, press <kbd>Ctrl</kbd> + <kbd>S</kbd> on Windows 
  or <kbd>Cmd</kbd> + <kbd>S</kbd> on Mac.
</p>

<p>
  Type <kbd>ls -la</kbd> to list all files in the directory.
</p>

The Samp Element

The <samp> element represents sample output from a computer program or system.

samp-element.html
<p>When you run the command, you'll see:</p>
<pre><samp>
$ node app.js
Server is running on port 3000
Connected to database successfully
</samp></pre>

<p>
  If the file is not found, the system returns 
  <samp>Error: ENOENT: no such file or directory</samp>
</p>

The Var Element

The <var> element represents a variable in mathematical expressions or programming contexts.

var-element.html
<p>
  The formula for the area of a circle is 
  <var>A</var> = π<var>r</var>²
</p>

<p>
  In the equation <code>let <var>x</var> = 10</code>, 
  <var>x</var> is a variable.
</p>

Practical Examples

Tutorial Instructions

tutorial-example.html
<article>
  <h2>Installing Node.js</h2>
  
  <p>Open your terminal and type:</p>
  <pre><kbd>node --version</kbd></pre>
  
  <p>You should see output similar to:</p>
  <pre><samp>v18.16.0</samp></pre>
  
  <p>
    To run your JavaScript file, use the <code>node</code> command 
    followed by your filename.
  </p>
</article>

Code Documentation

documentation-example.html
<section>
  <h3>Array.filter() Method</h3>
  
  <p>
    The <code>filter()</code> method creates a new array with elements 
    that pass a test.
  </p>
  
  <pre><code>const numbers = [1, 2, 3, 4, 5];
const evenNumbers = numbers.filter(num => num % 2 === 0);
console.log(evenNumbers); // [2, 4]</code></pre>
  
  <p>
    In this example, <var>numbers</var> is the original array and 
    <var>evenNumbers</var> stores the filtered result.
  </p>
</section>

Displaying HTML Code

When you want to display HTML code itself (not render it), you need to escape special characters:

  • Replace < with &lt;
  • Replace > with &gt;
  • Replace & with &amp;
html-code-display.html
<pre><code>&lt;div class="container"&gt;
  &lt;h1&gt;Hello World&lt;/h1&gt;
  &lt;p&gt;This is a paragraph.&lt;/p&gt;
&lt;/div&gt;</code></pre>

Styling Tips

These elements have some default styling you should know about:

  • All these elements use monospace fonts by default (like Courier or Consolas)
  • <pre> is a block-level element
  • <code>, <kbd>, <samp>, and <var> are inline elements
  • <kbd> often appears with a subtle border or background

Best Practices

✅ Do:

  • Use <pre><code> together for code blocks
  • Use <kbd> for keyboard shortcuts and commands
  • Use <code> for inline code references
  • Use <samp> for program output
  • Escape HTML characters when displaying HTML code

❌ Don't:

  • Use <pre> just to get a monospace font (use CSS instead)
  • Forget to combine pre with code for code blocks
  • Use code elements for emphasis (use strong or em instead)
  • Nest pre elements (one level of pre is sufficient)

Accessibility Considerations

Using semantic elements helps screen readers and other assistive technologies:

  • Screen readers can identify code blocks and may change pronunciation
  • Keyboard shortcuts marked with <kbd> are clearly identified
  • Proper semantic markup improves document structure and navigation

Key Takeaways

  • <pre> preserves whitespace and line breaks
  • <code> marks up code snippets and programming text
  • <kbd> represents keyboard input
  • <samp> represents computer output
  • <var> represents variables
  • Combine <pre><code> for multi-line code blocks
  • Escape HTML characters when displaying HTML code

What's Next?

Now that you know how to display code and preformatted text, you're ready to learn about subscript and superscript text. In the next lesson, we'll explore the <sub> and <sup> elements for mathematical formulas, chemical notation, and footnotes.

Know someone who'd find this guide helpful? Please share!

Previous
Quotations and Citations
Next
Subscript and Superscript

Never Miss a New HTML Tutorial

Join 1,000+ developers learning HTML step-by-step. Get new tutorials, tips, and exclusive resources delivered to your inbox - completely FREE.

You might also like

UI Components

Ready-made components for your projects

Developer Tools

Boost productivity with handy generators

HTML Game

Learn HTML concepts through interactive gameplay

Join Our Web Development Training Program

Master frontend & backend development with hands-on projects.

Menu

Topics

Getting Started

  • What is HTML?
  • How the Web Works
  • Setting Up Your Environment
  • Your First HTML Page

HTML Document Structure

  • The DOCTYPE Declaration
  • The HTML Element
  • The Head Section
  • Meta Tags
  • The Body Element
  • HTML Comments

Text and Formatting

  • Headings (h1-h6)
  • Paragraphs
  • Line Breaks and Horizontal Rules
  • Text Formatting (Bold, Italic, etc.)
  • Quotations and Citations
  • Preformatted Text and Code
  • Subscript and Superscript

Links and Navigation

  • Creating Links
  • Absolute vs Relative URLs
  • Link Targets and Attributes
  • Email and Phone Links
  • Internal Page Links

Images and Media

  • Adding Images
  • Image Attributes
  • Image Formats
  • Figure and Figcaption
  • Responsive Images
  • Audio Elements
  • Video Elements
  • Embedding Content

Lists

  • Unordered Lists
  • Ordered Lists
  • Description Lists
  • Nested Lists

Tables

  • Table Basics
  • Table Headers and Footers
  • Spanning Columns and Rows
  • Table Captions
  • Styling Tables

Forms - Part 1 (Basics)

  • Introduction to Forms
  • Text Input Fields
  • Labels and Placeholders
  • Textarea Element
  • Checkboxes
  • Radio Buttons

Forms - Part 2 (Advanced)

  • Select Dropdowns
  • File Upload Inputs
  • HTML5 Input Types
  • Form Validation
  • Form Buttons
  • Fieldsets and Legends
  • Form Accessibility

Semantic HTML

  • What is Semantic HTML?
  • Header and Footer
  • Nav Element
  • Main and Article
  • Section and Aside
  • Div vs Semantic Elements
  • Time and Mark Elements

HTML5 Features

  • HTML5 Overview
  • Canvas Element
  • SVG in HTML
  • Geolocation API
  • Drag and Drop
  • Local Storage
  • Web Workers

Web Accessibility

  • Introduction to Accessibility
  • ARIA Attributes
  • Keyboard Navigation
  • Alt Text Best Practices
  • Color Contrast and Typography
  • Testing for Accessibility

Best Practices & SEO

  • HTML Best Practices
  • SEO Fundamentals
  • Meta Tags for SEO
  • Structured Data
Falytom logoFALYTOM

Learn technologies like HTML, CSS, JavaScript, React, NodeJS and more through interactive games, tutorials, components and free tools. Transform your businesses with AI chatbots, SEO-optimized websites, and professional development services.

© 2025 Tomilola Group
  • Facebook logo
  • Twitter logo
  • Instagram logo
  • LinkedIn logo
  • GitHub logo
  • YouTube logo