To display computer code in HTML, you can use the <code>
element. This element is used to represent a fragment of computer code.
Here's an example of how you can use the <code>
element:
<p>To print "Hello, World!" in Python, you use the <code>print("Hello, World!")</code> statement.</p>
In this example, the text "print("Hello, World!")" is marked up as computer code using the <code>
element. When rendered in a browser, it will typically be displayed in a monospaced font.
Additionally, if you want to display a block of code, you can use the <pre>
element along with <code>
:
<pre><code>
def greet():
print("Hello, World!")
greet()
</code></pre>
This will preserve any formatting (such as spaces and line breaks) within the <pre>
element, making it suitable for displaying multiline code snippets.
By using the <code>
and <pre>
elements, you can effectively represent computer code in HTML.