Table of Contents
In this post, we'll learn how to display HTML tags as plain text.
Normally, HTML tags are not visible to the end-user because they are used to render the content of the page.
However, you can still display the tags on the page anyway if you make sure of HTML entities.
HTML Entities
HTML entities are a way to display certain characters in HTML. In our case, we need a way to display the <
and >
characters, because these are reserved characters in HTML.
Because they have special meaning in HTML, they are not usually displayed as plain text.
Here's an example of using HTML entities to display HTML tags:
HTML<h1>This is a heading.</h1>
<ul>
<li>This is a list item.</li>
</ul>
<ol>
<li>This is a list item.</li>
</ol>
This is how it will look like rendered in the browser:
HTML<h1>This is a heading.</h1>
<ul>
<li>This is a list item.</li>
</ul>
<ol>
<li>This is a list item.</li>
</ol>
Here is a list of all the reserved HTML characters and their entities:
<
:<
>
:>
&
:&
"
:"
To encode your HTML, you can use a search and replace for every reserved character to convert it to its corresponding entity.
Preserve Formatting and Indentation
Once you encode your HTML, you will want to preserve the formatting and indentation of the HTML.
To do this, use the <pre>
tag. This tag is used on on preformatted text, hence the name.
Here's an example of how to use the <pre>
tag with encoded HTML:
HTML<pre>
<h1>This is a heading.</h1>
<ul>
<li>This is a list item.</li>
</ul>
<ol>
<li>This is a list item.</li>
</ol>
</pre>
Conclusion
We've seen how to display HTML tags as plain text using HTML entities. We've also seen how to use the pre
tag to preserve the formatting and indentation of HTML.
Hopefully, you've found this post helpful!
- Getting Started with Svelte
- Create an RSS Reader in Node
- Git Tutorial: Learn how to use Version Control
- How to Set Up Cron Jobs in Linux
- Best Visual Studio Code Extensions for 2022
- How to deploy a Node app using Docker
- Learn how to use v-model with a custom Vue component
- How to Scrape the Web using Node.js and Puppeteer
- Build a Real-Time Chat App with Node, Express, and Socket.io
- Using Push.js to Display Web Browser Notifications
- Building a Real-Time Note-Taking App with Vue and Firebase
- Setting Up a Local Web Server using Node.js