HTML (Hypertext Markup Language) provides a set of elements that are used to display computer code on a web page. Here are some of the most commonly used code elements in HTML:
<code>– The<code>element is used to indicate a fragment of computer code. This element does not add any semantic meaning to the code; it is simply used to visually distinguish the code from surrounding text.<pre>– The<pre>element is used to display preformatted text. This element preserves any white space and line breaks in the text. This is particularly useful for displaying code, as it preserves the formatting of the code.<var>– The<var>element is used to indicate a variable or placeholder in a code example. This element does not add any semantic meaning to the code; it is simply used to visually distinguish the variable from surrounding text.<kbd>– The<kbd>element is used to indicate keyboard input in a code example. This element does not add any semantic meaning to the code; it is simply used to visually distinguish the keyboard input from surrounding text.<samp>– The<samp>element is used to indicate sample output from a code example. This element does not add any semantic meaning to the code; it is simply used to visually distinguish the output from surrounding text.<pre><code>– The combination of the<pre>and<code>elements is often used to display code examples with preserved formatting.
Example usage:
<pre><code>
function add(a, b) {
return a + b;
}
</code></pre>
This will display the code with preserved formatting:
function add(a, b) {
return a + b;
}
