How to add plain text code in a webpage? [duplicate] - html

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 2 years ago.
I know it is possible because this website does it, but I tried researching how and just got a bunch of junk, so how do I add tags to a website paragraph without the browser interpreting it as code.
For example, if I have <p><div></div></p>, I want the div to display in the browser as text not have the browser interpret it as html. Is this complicated to do?
I have been writing tutorials for school, and it would be much easier if I could add the code directly to the webpage in text form instead of images, so students can copy and paste it.

Look at how this website itself achieves this:
<p>For example, if I have <code><p><div></div></p></code>, I want the div to display in the browser as text not have the browser interpret it as html. Is this complicated to do?</p>
You need to replace the < and > with their HTML character entities.

There are many ways to use:
Replace < with <
`<h1>This is heading </small></h1>`
Place the code inside </xmp><xmp> tags
<xmp>
<ul>
<li>Coffee</li>
<li>Tea</li>
</ul>
</xmp>
I do not recommend other ways because they do not work on all browsers like <plaintext> or <listing>.

You want to look into something called HTML Entities.
If you want the < character to appear on a website, for example, you can write this HTML code: <. These are the five basic HTML Entities and their source code equivalents:
< <
> >
" "
' &apos;
& &
If you are using a programming language (such as PHP or ASP.NET), then there is probably a built-in command that will do the conversion for you (htmlspecialchars() and Server.HtmlEncode, respectively).

Use the tag <PRE> before a block of reformatted text and </PRE> after.
The text between these tags is rendered as monospaced characters with line breaks and spaces at the same points as in the original file. This may be helpful for rendering poetry without adding a lot of HTML code. Try this:
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went
the lamb was sure to go.

To add plain text code in a webpage, HTML Character Escaping is needed on five characters:
< as <> as >& as &&apos; as &apos;" as "
(OR)
<xmp> tag may also be used as an alternate, this tag disturbs the style and is obsolete.
<xmp>Code with HTML Tags like <div> etc. </xmp>

Use the html entity/special character of the tag, such as < (for less than)
<p> in html -> <p> in browser
You could also write <p> since there is no ambiguity about the opening tag.
Many languages have built in methods to convert HTML special characters such as php's htmlspecialchars

You need to escape the HTML tags, namely the less-than sign. Write it as < and it will appear as < on the HTML page.

Your html needs to not be in tags. If you use the <> tags you will have it converted into code not text, if I was to write <br> in the middle of a sentence then it would do this You will need to Write the code in code so to speak, using the < > (< >)
and then you get what you need.

I just discovered a much simpler solution at CSS-Tricks...
Just have your outer-most wrapper be a 'pre' tag, followed by a 'code' tag, then inside the code tag put your code in paranthesis.

The simplest way to do it without having to reformat your text using entities is to use JQuery.
<div id="container"></div>
<script>
$('#container').text("<div><h1>Hello!</h1><p>I like you.</p></div>");
</script>
If you then do alert($('#container').prop('innerHTML'));, you get <div><h1>Hello!</h1><p>I like you.</p></div>
How useful that technique is depends somewhat on where your material is coming from.

Use iframe and txt file:
<iframe src="html.txt"></iframe>

Related

<p> Inception - HTML

In my HTML project, I am trying to mention the <p> tag. However, VS Code is interpreting it as an actual <p> tag and it causes weird things to happen. Does anybody have a way to get around this? Thank you in advance!
Use < in place of < and > in place of >.
Bytheway, it is common to style code snippets with the code tag.
<code><p></code>
And if you ever need a multiline code snippet, wrap it in a pre tag to preserve whitespace.
<pre><code><p>
this stays indented and the newlines where added without a br tag
</p>
</code></pre>
You need to use HTML character entities for greater than & less than signs. Try
>p<
Use them between <xmp></xmp> tag.
Code:
<xmp><p></p></xmp>
Output:
<p></p>

Tag to ignore HTML formatting code?

I thought the <code> tags were supposed to ignore HTML like with BBcode, but that doesn't seem to be the case. Is there a way to achieve this in HTML so I can show code examples? I found a page about <plaintext> and <xmp>, but they're apparently deprecated, so what should I use? I've seen <!-- --> tags for comments, but those are completely hidden from view. I just need something to ignore tags.
In HTML, the only solution guaranteed to work everywhere is to escape the code manually.
<code>
<img src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</code>
Replace the < character with <
Replace the > character with >
Optionally surround your HTML sample with or without tags. (e.x. <code> or <pre>)

How do I show actual HTML Code in textarea rather than rendered HTML?

I have a code that saves (html code) plus (some text) in mysql from textarea.
I then take the text from the mysql and display it under the textarea. The thing is if I save the code
<div style="color:red">Hello</div>
in mysql and then display it, I see Hello in red, but I want to see the actual
<div style="color:red">Hello</div>
to appear under the textarea. I hope you understand my problem.
so when you've grabbed the data from the database you want to actually display the html, rather than the page rendering the html?
if so just use the php function htmlentities();
You can use the xmp element, see What was the 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, .
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:
<![CDATA[
This is a demo, tags will
appear literally.
<div style="color:red">Hello</div>
]]>
you can refer this ans : https://stackoverflow.com/a/16785992/3000179
If you want to do on browser level, you can follow the steps :
Replace the & character with &
Replace the < character with <
Replace the > character with >
Optionally surround your HTML sample with <pre> and/or <code>
tags.
Hope this helps.

Why is my "pre" HTML tag not working in one specific instance? [duplicate]

This question already has answers here:
How to escape < and > inside <pre> tags
(8 answers)
Closed 9 years ago.
I have some html that uses a few "pre" tags to preserve code snippets, using "html" as the lang. For the most part, it works, but in one specific instance, namely:
<pre lang="html">
<li><a runat="server" href="~/Images">Images</a></li>
</pre>
Instead of preserving the snippet, it shows a bullet and a hyperlinked "Images" - what the deuce?
The whole shebang can be seen, malformed, beneformed, and/or mutilated at: http://jsfiddle.net/7BSLc/
<pre> is for telling the browser to keep white space formatting and doesn't tell the browser not to render html elements within. you need to encode the html you're trying to put in. You can use encoded values for the angle brackets < and it will render the html as text instead of randering the html. http://jsfiddle.net/7BSLc/9/
It is ugly, but it works:
<li><a runat="server" href="~/Images">Images</a></li>
HTML-Elements are interpreted within pre-elements! You should use HTML-entities for HTML-specific chars.

HTML tag that causes other tags to be rendered as plain text [duplicate]

This question already has answers here:
How to display raw HTML code on an HTML page
(30 answers)
Closed 3 years ago.
I'd like to add an area to a page where all of the dynamic content is rendered as plain text instead of markup. For example:
<myMagicTag>
<b>Hello</b> World
</myMagicTag>
I want the <b> tag to show up as just text and not as a bold directive. I'd rather not have to write the code to convert every "<" to an "<".
I know that <textarea> will do it, but it has other undesirable side effects like adding scroll bars.
Does myMagicTag exist?
Edit: A jQuery or javascript function that does this would also be ok. Can't do it server-side, unfortunately.
You can do this with the script element (bolded by me):
The script element allows authors to include dynamic script and data blocks in their documents.
Example:
<script type="text/plain">
This content has the media type plain/text, so characters reserved in HTML have no special meaning here: <div> ← this will be displayed.
</script>
(Note that the allowed content of the script element is restricted, e.g. you can’t have </script> as text content (it would close the script element).)
Typically, script elements have display:none by default in browser’s CSS, so you’d need to overwrite that in your CSS, e.g.:
script[type="text/plain"] {display:block;}
You can use a function to escape the < >, eg:
'span.name': function(){
return this.name.replace(/</g, '<').replace(/>/g, '>');
}
Also take a look at <plaintext></plaintext>. I haven't used it myself but it is known to render everything that follows as plain text(by everything i mean to say it ignores the closing tag, so all the following code is rendered as text)
The tag used to be <XMP> but in HTML 4 it was already deprecated. Browser's don't seem to have dropped its support but I would not recommend it for anything beyond quick debugging. The MDN article about <XMP> lists two other tags, <plaintext> and <listing>, that were deprecated even earlier. I'm not aware of any current alternative.
Whatever, the code to encode plain text into HTML is pretty straightforward in most programming languages.
Note: the term similar means exactly that—all three are designed to inject plain text into HTML. I'm not implying that they are synonyms or that they behave identically—they don't.
There is no specific tag except the deprecated <xmp>.
But a script tag is allowed to store unformatted data.
Here is the only solution so far showing dynamic content, as you wanted.
Run code snippet for more info.
<script id="myMagicTag" type="text/plain" style="display:block;">
<b>Hello</b> World
</script>
Use Visible Data-blocks
<script>
document.querySelector("#myMagicTag").innerHTML = "<b>Unformatted</b> dynamic content"
</script>
No, that's not possible, you need to HtmlEncode it.
If your using a server-side language, that's not really difficult though.
In .NET you would do something like this:
string encodedtext = HttpContext.Current.Server.HtmlEncode(plaintext);
In my application, I need to prevent HTML from rendering
"if (a<b || c>100) ..."
and
"cout << ...".
Also the entire C++ code region HTML must pass through the GCC compiler with the desired effect. I've hit on two schemes:
First:
//<xmp>
#include <string>
//</xmp>}
For reasons that escape me, the <xmp> tag is deprecated. I find (2016-01-09) that Chrome and FF, at least, render the tag the way I want. While researching my problem, I saw a remark that <xmp> is required in HTML 5.
Second, in <head> ... </head>, insert:
<style type="text/css">
textarea { border: none; }
</style>
Then in <body> ... </body>, write:
//<br /> <textarea rows="4" disabled cols="80">
#include <stdlib.h>
#include <iostream>
#include <string>
//</textarea> <br />
Note: Set "cols="80" to prevent following text from appearing on the right. Set "rows=..." to one more line than you enclose in the tag. This prevents scroll bars. This second technique has several disadvantages:
The "disabled" attribute shades the region
Incomprehensible, complex comments in the code sent to the compiler
Harder to understand
More typing
However, this methhod is neither obsolete nor deprecated. The gods of HTML will make their faces to shine unto you.