Choose the element for the example’s role
The HTML Standard defines code for a fragment of computer code. Its pre element represents preformatted text whose structure can be conveyed through typographic conventions. Combine them for a multi-line source example; use code alone within a sentence for a short identifier. A font choice does not provide either semantic role by itself. Read the code element definition and pre element definition before treating every monospaced paragraph as a code block.
Set a family and a deliberate overflow treatment
Start with ordinary selectable text and a monospace fallback. This example uses the system’s available monospace family; it does not download a named font.
<pre class="code-sample"><code>const total = price * quantity;</code></pre>
code { font-family: monospace; }
.code-sample {
overflow-x: auto;
padding: 1rem;
line-height: 1.5;
tab-size: 4;
}
Compare Source Code Pro and Roboto Mono below as separate specimens. To use one on a website, supply its authorized font resource through the HTML/CSS font workflow.
Preserve source characters and indentation
When displaying HTML source, escape markup characters so the browser shows the intended tags instead of interpreting them as the surrounding document. Keep the original source alongside the example. MDN documents tab-size as the width used to display tab characters; changing that setting does not replace tab characters with spaces. Test an actual indented sample rather than adding manual alignment spaces around a screenshot. Recheck straight quotes, operators and copied text against the source.
Review long lines at the reading width
Try a short command, a nested example and the longest line expected in the article. Review them on a narrow screen and at increased text size. With the scrolling block above, confirm that the full line remains reachable; if the editorial design instead wraps lines, distinguish visual wrapping from source newlines. Keep captions and explanations outside the code when they are not executable content. A font specimen helps compare letterforms, while the page layout determines the practical reading experience.
Your questions, answered.
Does the code element install a coding font?
No. It identifies code content. CSS and the available font resources determine the rendered family.
Does tab-size change tabs into spaces?
No. It controls how tab characters are displayed. Preserve the original source characters separately.
Sources & background
Sources supporting this page’s explanations. Practical examples are our editorial suggestions.