Displaying Code in HTML
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.
