How to display raw HTML code in PRE or something like it but without escaping it
You can use the xmp
element, see What was the <XMP> tag used for?. It has been in HTML since the beginning and is supported by all browsers. Specifications frown upon it, but HTML5 CR still describes it and requires browsers to support it (though it also tells authors not to use it, but it cannot really prevent you).
Everything inside xmp
is taken as such, no markup (tags or character references) is recognized there, except, for apparent reason, the end tag of the element itself, </xmp>
.
Otherwise xmp
is rendered like pre
.
When using “real XHTML”, i.e. XHTML served with an XML media type (which is rare), the special parsing rules do not apply, so xmp
is treated like pre
. But in “real XHTML”, you can use a CDATA section, which implies similar parsing rules. It has no special formatting, so you would probably want to wrap it inside a pre
element:
<pre><![CDATA[
This is a demo, tags like <p> will
appear literally.
]]></pre>
I don’t see how you could combine xmp
and CDATA section to achieve so-called polyglot markup
How to display raw HTML code on an HTML page
is there a tag for don't render HTML until you hit the closing tag?
No, there is not. In HTML proper, there’s no way short of escaping some characters:
&
as&
<
as<
(Incidentally, there is no need to escape >
but people often do it for reasons of symmetry.)
And of course you should surround the resulting, escaped HTML code within <pre><code>…</code></pre>
to (a) preserve whitespace and line breaks, and (b) mark it up as a code element.
All other solutions, such as wrapping your code into a <textarea>
or the (deprecated) <xmp>
element, will break.1
XHTML that is declared to the browser as XML (via the HTTP Content-Type
header! — merely setting a DOCTYPE
is not enough) could alternatively use a CDATA section:
<![CDATA[Your <code> here]]>
But this only works in XML, not in HTML, and even this isn’t a foolproof solution, since the code mustn’t contain the closing delimiter ]]>
. So even in XML the simplest, most robust solution is via escaping.
1 Case in point:
textarea {border: none; width: 100%;}
<textarea readonly="readonly">
<p>Computer <textarea>says</textarea> <span>no.</span>
</textarea>
<xmp>
Computer <xmp>says</xmp> <span>no.</span>
</xmp>
How does github raw code work? It can display text like code without pre tag
Then answer is in the http header. Web pages are served with a Content-type
header set to text/html
. If you use text/plain
instead, browsers will show it as pre-formatted.
How to show div tag literally in code / pre tag?
Unfortunately it doesn't work with HTML tags.
<code>
means "This is code", <pre>
means "White space in this markup is significant". Neither means "The content of this element should not be treated as HTML", so both work perfectly, even if they don't mean what you want them to mean.
Is there any way to show "
<div>
" inside of<pre>
or<code>
tag without actually interpreting it as HTML?
If you want to render a <
character then use <
, with >
for >
and &
for &
.
You can't (in modern HTML) write markup and have it be interpreted as text.
Display html code in html page
Use entities, like
var encodeHtmlEntity = function(str) {
var buf = [];
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['', str[i].charCodeAt(), ';'].join(''));
}
return buf.join('');
};
bootbox.alert('<pre><code data-language="html">'+ encodeHtmlEntity(document.getElementById('editor').innerHTML)) + '</code></pre>');
Is there a HTML/CSS way to display HTML tags without parsing?
You can use a script
element with its type
set to denote plain text, and set its display
property to block
. This only affects the parsing behavior: no markup (tags or entity or character references) is recognized, except for the end tag of the element itself </script>
. (So it is not quite the same as xmp
, where the recognized tag is </xmp>
.) You can separately make white space handling similar to that of xmp
and pre
and/or set the font the monospace as in those elements by default.
Example:
<style>
script {
display: block;
}
</style>
Then within document body:
<script type="text/plain">
<i>é</i>
</script>
Tested on newest versions of IE, Chrome, Firefox, Opera. Didn’t work in IE 8 and IE 7 emulation on IE 9, but that’s probably a bug in the emulation.
However, I don’t see why you would use this instead of xmp
, which hasn’t stopped working. It’s not in the specs, but if you are worried about that, you should have always been worried. Mentioned in HTML 2.0 (the first HTML spec ever) as avoidable, it was deprecated in HTML 3.2 and completely removed in HTML 4.0 (long ago: in 1997).
The xmp
is making a comeback rather than dying. The W3C HTML5 (characterized as the current HTML specification by W3C staff) declares xmp
as obsolete and non-conforming, but it also imposes a requirement on browsers: “User agents must treat xmp
elements in a manner equivalent to pre
elements in terms of semantics and for purposes of rendering. (The parser has special behavior for this element though.)” The old parsing behavior is thus not explicitly required, but clearly implied.
Related Topics
Regular Expression For Extracting Tag Attributes
Example of Multipart/Form-Data
How to Make an Editable Div Look Like a Text Field
Best Approach to Real Time Http Streaming to Html5 Video Client
How to Stream Audio/Video Files Such as Mp3, Mp4, Avi, etc Using a Servlet
HTML Select Form with Option to Enter Custom Value
Starting with a Forward Slash in HTML for "Href"
Link with Target="_Blank" and Rel="Noopener Noreferrer" Still Vulnerable
Text Not Centered with Justify-Content: Center
Embed Image in a ≪Button≫ Element
How to Get Folder Directory from HTML Input Type "File" or Any Other Way
Two Inline-Block, Width 50% Elements Wrap to Second Line
How to Access a Mobile'S Camera from a Web App
What Are the Allowed Tags Inside a ≪Li≫
How to Vertically Align into the Center of the Content of a Div with Defined Width/Height