HTML Code Generator
WM-51Paste code or text and get it escaped and wrapped in the right HTML element: code, pre with code, kbd, samp or var.
Runs in your browser. Nothing is uploaded.
Code or text (optional first line language: js)
HTML
How to use it
- Paste the code or text. Add a first line such as language: js to set a language class.
- Pick the element: inline code, a pre block, kbd, samp or var.
- Or use the keyboard action to turn Ctrl+Shift+P into nested kbd elements.
- Copy the result into your page.
Which element for which job
code marks a fragment of computer code. pre keeps whitespace and line breaks, so a multi-line listing goes in pre with a code element inside it. kbd marks text the user types or a key they press, samp marks output from a program, and var marks a variable name in a mathematical or programming context. None of them add a font by themselves beyond the default monospace; you style them with CSS. Choosing by meaning helps assistive tools and gives you stable selectors.
Escaping and the language class
Any code that contains a less-than sign, a greater-than sign or an ampersand must be escaped, or the browser reads it as markup. The tool converts them to <, > and &. The language line adds a class such as language-js to the code element. That naming is a convention that syntax highlighting libraries commonly read; the tool itself does not colour anything. Blank lines at the start and end of a block are trimmed, because the HTML parser drops a newline that follows the opening pre tag.
Keyboard shortcuts with nested kbd
The keyboard action splits a line at each plus sign and wraps every key in its own kbd, inside an outer kbd for the whole combination. This nesting is an accepted way to mark a shortcut: the inner elements are single keys and the outer one is the whole input. To show the plus key itself, write two plus signs, for example Ctrl++. Key names are escaped but not translated, so write them as your audience expects.
Questions people ask
How do I show HTML code on a web page?
Escape the angle brackets and ampersands, then place the text in a pre and code pair. This tool does the escaping and the wrapping in one step, so tags appear as text instead of being run.
What is the difference between code and pre?
code says that the text is computer code but does not keep the layout. pre keeps spaces and line breaks exactly. For a multi-line snippet use both: pre containing code.
How do I add a language for syntax highlighting?
Put a line such as language: python first. The output gets class="language-python" on the code element, which is the form many highlighting libraries look for. You still need to load one of them; this tool does not highlight.
What does the kbd element mean?
It marks user input, usually keys on a keyboard. Browsers show it in a monospace font by default. Nesting one kbd per key inside an outer kbd is a common way to mark a combination such as Ctrl+C.
Why is the first blank line of my code removed?
The HTML parser ignores one newline right after the opening pre tag, so a leading blank line would vanish anyway. The tool trims blank lines at both ends so the output matches what a browser will show.
About this tool
The text is HTML-escaped (& becomes &, < becomes <, > becomes >), so code that contains tags displays as written. The block choice writes pre with a code element inside and keeps your line breaks and spaces exactly; blank lines at the start and end are trimmed, because the HTML parser drops a newline directly after the opening pre tag. A language line adds a class such as language-js, the naming convention many syntax highlighters read; this tool does not colour the code itself. The keyboard action turns Ctrl+Shift+P into nested kbd elements; write ++ for a plus key. Control characters are removed.