How to make html ignore code that is part of text? - html

I need to include some codes in my html document
I've tried <pre> tag, but that didn't help.
How do I get this code into a document like text?
Thanks

Short Answer.
Encode your code using an online HTML Encoder and then put it inside pre
<pre>
<%--your encoded code goes here--%>
</pre>
Long Answer.
The above will only help you to show your code. If you want proper highlighting for your code, Try something like SyntaxHighlighter
Link: How to use SyntaxHighlighter.

You have to use html entities. Example:
<div>
Some Stuff
</div>
should be
<div>
Some Stuff
</div>
and it will render as the first one

You can use <pre> tag. Each time you insert any texts within the <pre> tag it wont get parsed as html document. One caveat though, if you try to put an opening HTML tag inside the pre tag, you have to do it like this:
<pre>
<TEST>
TEST!!
</TEST>
</pre>

Use the xmp tag. It is easier and quicker than using an HTML encoder. Example:
<h1>This is a heading.</h1>
<p>This is a pharagraph</p>
<xmp>
<h1>This is a heading.</h1>
<p>This is a pharagraph</p>
</xmp>

You can use a combination of the <pre> and <code> tags. The <pre> tag retains the formatting , and the <code> tag outputs in monospaced font. Wrap the <code> tag in <pre> tag, and paste whatever block of code in the <code> elements body. This will output like the following:
<pre>
<code>
function(){
var myVar = "This is code";
return myVar;
}
</code>
</pre>

Some people might crucify me not escaping my code. But this worked for me.
CSS
.tag:before{
content: '<'
}
.tag:after{
content: '>'
}
HTML
<pre>
<span class="tag">tag</span>
</pre>
<!--Instead of having to escaping all the character-->
<tag> </tag>
<!--Kinda interested to see why this is bad. I understand that not escaping code can be dangerous b/c of SQL injections and all sort of BS but why is this not a viable option-->

Use
encode
Example:
<br> -> encoded -> <br>
use <br> in your text
same answer as Ibu but maybe you want a fast way to encode your tags.

To not escape any characters at all, you can use a textarea:
textarea {
font-family: inherit;
font-size: inherit;
border: none;
background-color: transparent;
resize: none;
outline: none;
}
<div>In order to show a bullet point in html use the li tags:</div>
<div>
<textarea readonly><li>Hello</li></textarea>
</div>
<div>And this is what it will look like:</div>
<li>Hello</li>
Run the snippet and notice the <li> and </li> tags render verbatim rather than being converted to a bullet.
Now why do we need the CSS and the extra HTML tags and attributes?
The CSS removes all the styling from textarea since the textarea will typically include styling for borders, resize gripper, and will use "input" style fonts. The textarea tag needs the "readonly" attribute so users can't edit it. The extra div tag around the textarea makes the textarea insert more correctly into the document flow.
It has some extra steps and it changes the DOM considerably but if you really need to show text strictly without escaping any characters at all for whatever reason.

I guess you just want to display a piece of code so you can just use https://highlightjs.org/
include this in your web page:
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/highlightjs/cdn-release#11.6.0/build/styles/default.min.css">
<script src="//cdn.jsdelivr.net/gh/highlightjs/cdn-release#11.6.0/build/highlight.min.js"></script>
<script>hljs.highlightAll();</script>
then add the <pre><code> tags to wrap your escaped piece of code
you can use https://www.freeformatter.com/html-escape.html
<pre>
<code class="language-html">
<h1>this is my HTML code</h1>
</code>
</pre>
or for CSS code
<pre>
<code class="language-css">
input { caret-color: red;}
</code>
</pre>
doc here https://highlightjs.org/usage/

Related

Css rule for comments inside html code tag?

How to set a css rule so I can format comments inside html code tags?
something like regex //.*
<pre>
<code>
git reset [file] //unstage but preserve file content
...
</code>
</pre>
You can put the comments in tags:
.comment {
color:grey;
/*Your CSS here*/
}
<script src="https://css-library.hg0428.repl.co/script.js"></script>
<pre>
<code>
git reset [file] <span class="comment">//unstage but preserve file content</span>
...
</code>
</pre>
If you dont want to use tags then you will need to use JavaScript to loop through all the code tags, locate comments, and apply the class.

HTML + CSS trying to use <code> or <xmp> to create line of text

I am trying to write guide like in codeacademy.
I want to write line with part of code and then white text.
i tried couple of methods, the text always go down line below the <code> or <xmp>
How can I create straight line with some code, for example red colored, then some text? I tried this:
<xmp style="color:#C34D57; "> <!DOCTYPE html> textextext </xmp>
and dozen more methods
As MDN states, the <xmp> element is obsolete and should not be used. It goes on to say:
Use the <pre> element or, if semantically adequate, the <code>
element instead. Note that you will need to escape the '<' character
as '<' to make sure it is not interpreted as markup.
So you could use:
<pre><!DOCTYPE html> textextext </pre>
Example:
pre {
color: #C34D57;
}
span {
color: green;
}
<pre><!DOCTYPE html> <span>textextext</span> </pre>
See also https://developer.mozilla.org/en-US/docs/Glossary/Entity

How to insert paragraph tags on newlines without tag overlapping?

I have a string with some HTML markup, but without any paragraphs. I want to replace newlines with paragraph tags, but if I do it "the dumb way":
html.gsub!(/\s*\n+\s*/, '</p><p>')
html = '<p>' + html + '</p>'
, then I will get markup overlapping:
This is text. <b>Bold text.\n
Still bold,</b> now plain.
becomes:
<p>This is text. <b>Bold text.</p><p>Still bold,</b> now plain.</p>
I know that HTML5 allows overlapping tags, but it is still very-very ugly. I want to get something like this:
<p>This is text. <b>Bold text.</b></p><p><b>Still bold,</b> now plain.</p>
How can I fix this problem?
UPD. <br/> is not what I need - I intend to use Kramdown with custom parser to generate Markdown from this string, and in the string I need open and closing tag for each paragraph.
Insert HTML <br> tags. They are well-defined as standalone tags that don't have paired end tags. If XHTML were still a thing, they would be <br/> tags.
By adding a <br> tag you can insert a single line break. It also has no end tag, so no need to try and close it.
W3Schools - < br > Tag
UPDATED:
If you need paired tags you could create a line break using CSS.
HTML
<p class="break">
Break right after this and
<span>before this</span>
</p>
CSS
p.break {
span {
display: table;
}
}
As shown here on Codepen.

Inline pre formatting?

This should be easy points as I forgot how and can't find it on Google.
How do I accomplish something like this:
Blah Blah Blah some code Blah Blah
in wordpress? pre doesn't work as it will give a line break.
It may be better to use <code> than <pre> to display inline code, for two reasons:
it's treated as inline
the semantics of <code> are a much better match than <pre>
But definitely use CSS classes (as #Blender points out).
There's a caveat: <code> doesn't escape <> brackets, so if you want to display HTML then you have to manually escape brackets as < and >. (which you should really do anyway; tags in HTML really should be clearly distinguished from data)
<tt>text</tt> gives text
Wait... Wordpress? What HTML tags does it support?
You can have an inline <pre> by adding some custom CSS to your theme's stylesheet:
pre.inline {
display: inline;
}
Now, when you write:
I am some <pre class="inline">code, see?</pre> Foo.
It shows up like this:
I am some code, see? Foo.
In this case is better to use <code>, it has a more semantic html syntax and any css workaround won't be needed to make it inline
p {
font-family: sans-serif
}
code {
background: #c7c7c7;
padding: .1rem .2rem;
border-radius: .2rem;
}
<p>
Some text <code>some code</code> some text
</p>

Formatting to a page html

The text in my source code is formatted properly, but when it shows up in the browser all the formatting disappears. Is there a tag I could add to the paragraph tag to make the text properly format?
you could use the <pre> and </pre> tags to preserve formatting instead of the <p> tag
The <pre> tag sounds like what you need.
<div style="white-space:pre">
hereIsSomeSourceCode();
if (blah == 3)
doSomething();
</div>