开发者

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

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

&lt;div&gt;
   Some Stuff
&lt;/div&gt;

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>
     &lt;TEST>
            TEST!!
     &lt;/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-->
&lt;tag&gt; &lt;&#47;tag&gt;

<!--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 -> &lt;br&gt;

use &lt;br&gt; 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/

  1. 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">
     &lt;h1&gt;this is my HTML code&lt;/h1&gt;
  </code>
</pre> 

or for CSS code

<pre>
  <code class="language-css">
     input { caret-color: red;}
  </code>
</pre> 

doc here https://highlightjs.org/usage/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜