How to display HTML tags as plain text

How to display HTML tags as plain text

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to display HTML tags as plain text

When building a rich-text editor or an input form that accepts raw HTML, you quickly run into a specific rendering problem: how do you explain the syntax without having your own explanation get interpreted by the browser? If you simply wrap your instructions in <pre> and <code> blocks, the browser's parser will often still attempt to render the tags inside those blocks if they are not properly escaped.

The core issue lies in how HTML parsers work. When a browser encounters &lt;strong&gt; in the source code, it knows that this is an escape sequence representing a literal less-than sign followed by the word "strong". However, if you write <strong> directly in your template without escaping it, the parser treats it as an opening tag and applies the formatting. To display the tags themselves as text, you must ensure the browser receives them as entity codes, not as raw markup tokens.

In a standard HTML context, the solution is straightforward: use &lt; instead of < and &gt; instead of >. This converts the angle brackets into their character equivalents so the browser renders them as symbols rather than structural elements. For example, to show a user how to bold text, your output should look like this in the source:

&lt;strong&gt;Look just like this line - so then know how to type it&lt;/strong&gt;

If you are working within a templating engine like Blade in Laravel, the framework handles escaping automatically by default. If you pass a string containing HTML tags directly into a Blade view without using {{ }}, the variables will be escaped, and the tags will appear as plain text. Conversely, if you use {{ $content }}, Laravel will render the HTML inside the variable. You must explicitly escape it using {!! $content !!} only when you intend to render the HTML, or ensure your data source has already been sanitized and is being treated as safe string content within a non-rendered context.

However, simply escaping angle brackets isn't always enough if you are dealing with attributes. If your documentation needs to show an input field like <input type="text">, you must also escape the equals sign and quotes if they are part of the syntax you want to display literally. A common pitfall is forgetting that closing tags often need the same treatment. To display a complete tag correctly, you might end up with:

&lt;div class="example"&gt;Content here&lt;/div&gt;

Another frequent mistake involves mixing raw HTML output with escaped content within the same document. If you are building an instructional page where some sections need to render actual forms or buttons while others need to show code examples, you must be disciplined about where you apply {{ }} and where you use {!! !!} or manual entity encoding. Applying htmlspecialchars in PHP before passing data to the view is a safe default for any string that contains instructional text about HTML syntax.

If your application relies on third-party rich-text editors, ensure that the editor's configuration allows you to output raw HTML entities in its help tooltips or documentation modals. Some libraries might strip out entity codes when saving content, so verify that your storage layer preserves these escape sequences if you plan to display them later as instructions.

For more complex scenarios where you need to handle dynamic HTML snippets, consider using a dedicated escaping library or function that specifically targets HTML entities. This ensures consistency across different environments and prevents accidental rendering of user-generated content meant to be displayed as code examples. Always remember that the distinction between data and markup is critical; treating instructional text about tags as data rather than markup is the only way to guarantee they appear correctly to the end user.

Our network