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().
Related
I was writing a HTML code for my blog,
and once I've used span tag
<span name="home">Home</span>
I could not see the clear text but this:
Home />
What can I do, to see clear text?
Thanks for your help.
You need to close an HTML tag declaration before you can write new tags.
So, <div <span>Some text</span> /> is incorrect. To correctly nest tags, use:
<div>
<span>Some text</span>
</div>
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>
I'm learning html/css and following the tutorial # W3Schools.com.
The code that I'm having trouble with is # http://www.w3schools.com/css/tryit.asp?filename=trycss_vertical-align
In line 11
<p>An <img src="w3schools_logo.gif" alt="W3Schools" width="270" height="50" /> image with a default alignment.</p>
what does the / do just just before the > and after the height attribute?
I looked at http://www.w3schools.com/tags/tag_img.asp but it didn't mention /.
Absolutely nothing, the trailing / is just ignored by the parser.
It is allowed on void tags (i.e. those without other tags/text content inside) by HTML5 specs to make the tag "self-closing", thus the markup valid XML. But it has no HTML-specific meaning.
Conversely, it is compulsory in xHTML for the same reason (making the markup valid XML).
In HTML there are two kind of tag :
First, e.g <div> that you need to close like </div> because it can have other tag inside
Second, e.g <img/> <br/> that doesn't need </img> or </br> to close. These are called void tags means it can't contains other tags.
The "/" is basically used for ending tags.
In HTML you need to start your tag and you need to end your tag
the code
here<img
marks the opening of the tag
so you need to close it with />
/> is required for ending HTML tags which don't have a closing tag . They are required to make tags compliant with XHTML.
Other examples are
<br/>
<hr/>
in HTML, elements have a beginning and ending tag e.g. <p> & </p> and in between these beginning and ending tags come the content of that element.
But some elements do not have content e.g. <br /> for line break and <img src="" /> for images. Such Elements are called empty elements and do not need a closing tag separately. Hence, the /> denotes closing of this tag.
in html each end every tag has an opening and closing like for body tag, the syntax is like <body></body>
similarly for imagae tag its like <img></img>
But we can use the other short form also...which is we are marking the closing option also with the same img tag... <img/> and then we can give all the options (properties for the img tag) inside it . src="w3schools_logo.gif" alt="W3Schools" width="270" height="50"
so it should be finally like <img src="w3schools_logo.gif" alt="W3Schools" width="270" height="50" />
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/
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>