Post HTML code in a div box without getting it affected - html

Right now I have posted the code into a textbox because I want the code in plain text. Now I wonder if I can put the code into a div box instead i get the same result.
Right now:
<textarea id="copytext" readonly="readonly"><p>Show the p tags in plain text</p></textarea>

Place your code between <pre> tags so that it is not interpreted by your browser. http://www.w3schools.com/tags/tag_pre.asp
<pre><img src="pic.jpg"></pre>
if you want a block of code this should be your markup:
<blockquote>
<pre>
<code>
My pre-formatted "quoted" code here.
</code>
</pre>
</blockquote>
Also take a look at this tool: https://code.google.com/p/syntaxhighlighter/

Testing 123, This shows the picture for me ????
<blockquote>
<pre>
<code>
<img src="http://lorempixel.com/200/200"/>
</code>
</pre>
</blockquote>

Related

How do you showcase html code within a html page? [duplicate]

This question already has answers here:
How to show <div> tag literally in <code>/<pre> tag?
(11 answers)
Closed 2 years ago.
When i say this, i mean how do i make a specific piece of code show not as part of the actual website, but as a demonstration for others to see?
There are various ways to do so
You may use <pre></pre> tag
<pre>
your-code
</pre>
using special <code> tag
<pre>
<code>
your-code
</code>
</pre>
you may like to use the HTML code within
<![CDATA[<your-code>]]>
use any syntax highligtening library e.g. prettify or syntaxhighlighter
You will have to use:
The <pre> - Preformatted tag to display the html text
The < - Less than tag in place of < tag in your code
The > - Greater than tag in place of > tag in your code.
i.e <div> shall be replaced by <div>
Brief example
<pre>
<!DOCTYPE html>
<html>
<body>
<h1>The code element</h1>
<p>The HTML <code>button</code> tag defines a clickable button.</p>
<p>The CSS <code>background-color</code> property defines the background color of an element.</p>
</body>
</html>
</pre>
<pre>
<code>
you code here
</code>
</pre>

CKEditor escape html

I have CKEditor on post text where I want to insert html-code. Something like this:
<pre>
<code class="html">
<img src="path/to/img.jpg" />
</code>
</pre>
But when I save the text, CKEditor interprets img-tag as actual html-image and shows broken image. How I can prevent that and tell to escape html-code in pre-code block
<pre>
<code class="html">
<img src="path/to/img.jpg" />
</code>
</pre>
This is an image inside a pre element. What you want to create is:
<pre>
<code class="html">
<img src="path/to/img.jpg" />
</code>
</pre>
And if you write <img .../> inside pre element in CKEditor, then CKEditor will return this (valid) HTML.
What developers often don't know is that when they load that content from their database into <textarea> this content may need to be encoded (depends on how it's saved). In PHP for instance it's enough to pass it through htmlspecialchars().

What are valid places to put HTML comments?

I am only concerned about HTML5 in this question.
This does not seem to work:
<code><<!-- test -->span class="otherCode">Bar</span></code>
( http://jsfiddle.net/dYSeK/ )
However, this works for me when I have long URLs that I want to wrap in source code.
<div>
<a href="https://www.google.co.in/search?q=test">https://<!--
-->www.google.co.in/search?q=test</a>
</div>
( http://jsfiddle.net/wMGdk/ )
What are all the valid places in an HTML code where I can place HTML comments?
You can place comments between any tag, but not inside of a tag declaration.
So, this is correct:
<code><!-- test --><span class="otherCode">Bar</span></code>
...and here's what it looks like properly indented (which is why it makes sense):
<code>
<!-- test -->
<span class="otherCode">Bar</span>
</code>
You can place comments on the header and body section.
In your example try this:
<code><!-- test -->span class="otherCode">Bar</span></code
I just removed a less than sign.

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
<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/

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>