Format Code Now
/

HTML Code Tag Generator

WM-29

Paste a snippet and get it escaped and wrapped, so angle brackets show as text. Pick code for inline use, pre and code for a block, kbd for keys or samp for output.

Runs in your browser. Nothing is uploaded.

Code or text to wrap (optional line: language: js)

HTML

How to use it

  1. Paste the code or text. Add a language: line, such as language: html, if you want a language class on the block.
  2. Pick code, pre + code block, kbd or samp.
  3. Copy the escaped markup.
  4. Paste it into your page or your CMS source view.

Why the snippet has to be escaped

If you put <div> straight into a page, the browser treats it as a real tag and shows nothing. Showing markup needs the less-than sign written as &lt;, and the ampersand as &amp;, so it is read as text. This tool does that replacement, and only that. Quotes are left alone in element content because they do not need escaping there, and your line breaks and indentation are not changed.

Four elements, four meanings

code is a fragment of computer code, used inline. pre keeps whitespace and line breaks, so a block of code goes inside pre with code nested in it. kbd marks what a user types or presses, such as Ctrl + S. samp marks sample output from a program. The HTML specification suggests a language-xxx class on the code element to name the language, and syntax highlighters read it. The browser itself does nothing with that class.

Questions people ask

How do I show HTML code on a web page?

Escape the less-than and ampersand characters, then wrap the result in code, or in pre and code for several lines. Pasting it here does both. The browser then displays the tags as text instead of running them.

What is the difference between code and pre?

code marks text as computer code but collapses whitespace like normal text. pre keeps spaces and line breaks exactly. For a multi-line snippet, use pre with a code element inside it.

How do I add a language to a code block?

Add a language: line, such as language: python, and the pre + code action writes class="language-python" on the code element. Highlighting libraries use that class. Without such a library, it has no visible effect.

What is the kbd tag for?

kbd marks input from the user, usually keyboard keys or shortcuts, such as Ctrl + C. It is different from samp, which marks what a program prints, and from code, which marks source code.

Does this tool add syntax coloring?

No. It writes plain markup with escaped text. Coloring comes from a highlighting script and a stylesheet on your site, which read the language class on the code element.

About this tool

Replaces & < and > with entities so the snippet shows as text instead of being read as markup, then wraps it. The pre + code action keeps line breaks and indentation exactly and adds a language-xxx class when you give a language: line; the class is a common convention that syntax highlighters read, the browser does nothing with it. The code action is for short inline fragments, kbd is for keys a person presses, samp is for program output. A language: line is only used by the pre + code action. The tool does not add syntax coloring. Optional lines: id:, class:, title:, lang:, data-* and aria-*.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.