I want to place an example of code without the tags being processed?
You can include code examples in your HTML by using the <code> tag. This automatically uses a monospaced font and also semantically labels our code as what it is.
The <code> tag is used to define a piece of computer code. The content inside is displayed in the browser's default monospace font. So, by using <pre> & <code>, you can display a code snippet such as function.
By right-clicking on the HTML in the “Elements” tab and selecting “Edit as HTML,” you can make live edits to the markup of a webpage that Chrome will immediately render once you're done editing.
Xmp tag will work:
<xmp> ...code... </xmp>
ETA: As of this writing this still works in all major browsers, but as others have pointed out it has officially been obsoleted in HTML5 [link]. Browsers could stop rendering it any day without warning or notice and it should be avoided for production sites. Still, there is no replacement that doesn't involve HTML encoding your string manually or using iframe trickery. It still has a place in my personal utility page tool-belt, but I would not consider it for a client.
The <pre>
tag allows you to display text with whitespaces and line breaks as-is. Also, code must be entity-encoded: For example, the code sample
<html> <head><title></title></head> <body></body> </html>
will become
<pre><html> <head><title></title></head> <body></body> </html></pre>
<
encodes into <
&
encodes into &
PHP htmlentities
does the job:
<pre><?php echo htmlentities($code) ?></pre>
<textarea>
does the job too: it also allow the code to be copied.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With