LEARNING OBJECTIVES โต
- Understand the semantic purpose of
<samp>according to the WHATWG specification. - Differentiate between
<code>(program code),<kbd>(user input), and<samp>(computer output). - Combine
<pre>,<samp>, and<kbd>to build authentic, interactive CLI terminal simulations. - Style terminal consoles with realistic command prompts, ANSI color accents, and error streams.
๐ The Mental Model & Story (Intuitive Foundation)
Imagine speaking into an intercom at a secure research facility:
- You speak: "Security, open bay door 4." (User Input)
- The electronic voice on the speaker responds: "Access granted. Door 4 opening in 3... 2... 1..." (System Output)
In software and web documentation, this dialogue happens constantly. A developer types a command into their terminal, and the operating system or compiler prints back lines of text, status reports, or error messages.
The <samp> (Sample Output) element represents that second voice: the text emitted by a computer program, script, terminal shell, or operating system. It is the quote from the computer.
+-----------------------------------------------------------------------------------+
| Terminal Conversation: |
| |
| $ npm run build <--- User typed this (<kbd>) |
| |
| > Building bundle... |
| > Compiled in 240ms <--- Computer printed this (<samp>) |
| > Bundle size: 42.1 kB |
+-----------------------------------------------------------------------------------+
Technical Deep Dive & Specifications
WHATWG Specification & Semantic Definition
The <samp> element represents (sample) output from a computer program or computing system.
- Content model: Phrasing content.
- Default display:
inline. - Default font:
monospace.
Semantic Triad: <code> vs <kbd> vs <samp>
| Element | Semantic Identity | Real-World Metaphor | Concrete Example |
|---|---|---|---|
<code> |
Programmatic syntax / source code | The blueprint | const sum = (a, b) => a + b; |
<kbd> |
User input / keystrokes | The human speaking | git push origin main |
<samp> |
System output / compiler results | The computer answering | fatal: remote origin already exists. |
Nesting <samp> and <kbd> in Interactive CLI Sessions
The WHATWG specification explicitly recommends nesting <samp> and <kbd> to model conversational CLI sessions:
<pre><samp><span class="prompt">solanki@devbox:~$</span> <kbd>ping -c 1 localhost</kbd>
PING localhost (127.0.0.1) 56(84) bytes of data.
64 bytes from localhost: icmp_seq=1 ttl=64 time=0.038 ms
--- localhost ping statistics ---
1 packets transmitted, 1 received, 0% packet loss</samp></pre>
In this structure:
<pre>preserves exact spaces and line breaks.<samp>wraps the overall terminal session output.<kbd>wraps the command typed by the user (ping -c 1 localhost).
+-------------------------------------------------------------------------------+
| <pre> |
| <samp> |
| $ <kbd>git status</kbd> |
| On branch main |
| Your branch is up to date with 'origin/main'. |
| </samp> |
| </pre> |
+-------------------------------------------------------------------------------+
๐ป Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 11โ20: Encapsulates the terminal window within
.terminal-cardusing a deep slate background. - Line 23โ31: Builds the classic macOS 3-dot window title bar (
red,yellow,green). - Line 57โ72: Combines
<pre>and<samp>to host the complete terminal output log. - Line 57: Nests
<kbd class="user-cmd">npm run test</kbd>directly inside<samp>to mark up the exact command the user executed. - Line 63โ71: Uses CSS color helper classes (
output-success,output-dim) to emulate ANSI terminal color codes for passed tests and timestamps.
Expected Browser Render Output
A dark IDE terminal window with window controls, showing the blue terminal prompt, yellow user input npm run test, and green/gray test summary output.
๐๏ธ Hands-On Exercise
๐ฏ The Challenge: Git Merge Conflict Console
Instructions:
- Build a mock terminal session for an engineer encountering a Git merge conflict.
- Mark up the user command (
git merge feature/auth-redesign) using<kbd>. - Mark up the system output (Auto-merging files, CONFLICT notices, Automatic merge failed) using
<samp>. - Wrap everything inside a responsive
<pre>container. - Highlight the word
CONFLICTin warning red using CSS.
๐ Starter Code Sandbox
โ ๏ธ Common Pitfalls
- Using
<code>for System Error Messages: Programmers often mark up404 Not FoundorNullPointerExceptionwith<code>. The semantic element for output emitted by a program is<samp>. - Forgetting
<pre>for Multi-Line Logs:<samp>is an inline phrasing element. If you have 20 lines of server logs and omit<pre>, they will collapse into one unreadable horizontal line. - Using
<samp>for Interactive Form Fields:<samp>is for quoting already-produced output. For live dynamic calculations inside a form, use<output>.
๐ก Pro Tips
- Inline Sample Badges: In API documentation, you can use inline
<samp>to display HTTP status returns directly in prose:"The API responds with <samp>429 Too Many Requests</samp> if the rate limit is exceeded." - Accessibility Role Mapping: In screen readers,
<samp>text is exposed as computer output. When rendering live streaming logs over WebSockets, pairing<samp>witharia-live="polite"ensures assistive devices announce incoming log lines smoothly.
๐ Key Takeaways
<samp>represents sample output from a computer program, script, CLI tool, or operating system.<code>= Source code,<kbd>= User keystrokes,<samp>= Computer output.- Wrap
<samp>in<pre>when displaying multi-line server logs or stack traces. - Nest
<kbd>inside<samp>to authentically represent an interactive CLI session where a user inputs commands and the machine responds. - By default,
<samp>is an inline element rendered in the user-agent monospace font. - --