This question already has answers here:
Is HTML case sensitive?
(6 answers)
Closed 8 years ago.
So the thing is, I am currently analyzing html documents by reading them though java and I see that the p tag is one of the most commonly used tags. I know that it's there to provide a new line, but what I don't know is why in some documents I see
<P>Hello world!</P>
and in others
<p>Hello world!</p>
Sometimes both are even used in the same document.
It seems to have exactly the same effect but I am just wondering if there is any reason these two variations exist.
There is no difference.
In HTML, elements are case-insensitive.
However, in XHTML, you must use lowercase.
http://www.w3.org/TR/html-markup/documents.html#case-insensitivity
HTML is case-insensitive. as you can see in the documentation.
They're same. It does not matter if its lowercase or uppercase or even mixed.
<p></p> Is used for a new paragraph
HTML is case-insensitive, which means you can use both spellings.
there is no difference. Inherited from SGML, HTML is not case sensitive for elements and attributes.
I prefere to use the lower-case form... Else I've the impression that the coder is shouting at me ^^
Related
This question already has answers here:
Is whitespace allowed within XML/HTML tags?
(4 answers)
Closed 8 years ago.
I'm building a slicer for deserializing html and xml. The question is - is it legal in either language to put a space after angle bracket and still have it count as a tag? For example
< div>
Or should I just consider that plain text?
And yes, I tried searching web for this but even after browsing through w3, I couldn't spot an explicit answer.
The HTML spec from WHATWG indicates the opening bracket must be immediately followed by the tag name.
The XML spec from W3C has the same requirements.
You may not have space leading the tag name. However, there can be any amount of space between the end of the tag name and the closing bracket. The following is valid:
<p ></p >
<p
></p>
No this is not legal,
For example this,
<html>
<head>
</head>
<body>
<h1>Hey sup man</h1>
< div><p>hey sup</p></div>
</body>
</html>
outputs this to the browser,
Hey sup man
< div>
hey sup
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.
This question already has answers here:
Are (non-void) self-closing tags valid in HTML5?
(8 answers)
Closed 9 years ago.
Do you need to have a / at the end of an img tag? I saw an example on W3schools.com without a /:
<img src="smiley.gif" alt="Smiley face" height="42" width="42">
I know it isn't necessary to self-close the tag, at least in my browser, but should I do so?
The / is only required for XHTML & XML.
If you're using a HTML5 doctype, then there's no need to terminate self-closing tags in this way.
This applies to <img src="img.png" />, <br />, <hr /> etc.
I.e. Just use <img src="img.png">, <br> and <hr>.
If you need an empty element (like a div), don't use <div />, instead use <div></div>. This is important since in HTML5, the slash is ignored and <div /> is interpreted as <div> without a closing tag.
It's only required for XHTML standards, as mentioned in other answers. HOWEVER, it also has another use.
Some code editors such as Notepad++ allow you to expand/collapse tags to make for faster viewing. But if you just put <img>, how is it supposed to know the difference between a tag that doesn't require an end tag, and one that uses the same tag name but does (ie. in XML)? This is especially true if you use custom tags.
Therefore, using /> explicitly tells the editor that it's self-closing, allowing it to continue working just fine and not having it warn you about a mismatched tag.
Apart from the validity issue, which simply depends on the doctype you are validating against, the slash really matters only if your page is served with an XML content type, such as application/xhtml+xml (which is rarely used, because old versions of IE choke on it).
If you do that, then well-formedness error handling is Draconian: any error (such as a start tag without a matching end tag, which can be the start tag itself when the <img ... /> syntax is used) prevents the page from being displayed at all, and instead an error message is shown.
The code is acceptable in html without using / but it is required in XHTML. I prefer to put / on so that there is no problem migrating from HTML to
XHTML
I think it would be better to close the img tag. Off the top of my head I can think of 2 reasons:
It won't validate under xhtml, if you care about this sort of thing.
Anything/anyone trying to programmatically work with it may get confused/run in to issues about unclosed tags. Who knows, this may include you in the future. :)
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:
< <
> >
" "
' '
& &
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 &' as '" 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>
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
RegEx match open tags except XHTML self-contained tags
I have a HTML page with
<a class="development" href="[variable content]">X</a>
The [variable content] is different in each place, the rest is the same.
What regexp will catch all of those links?
(Although I am not writing it here, I did try...)
What about the non-greedy version:
<a class="development" href="(.*?)">X</a>
Try this regular expression:
<a class="development" href="[^"]*">X</a>
Regexes are fundamentally bad at parsing HTML (see Can you provide some examples of why it is hard to parse XML and HTML with a regex? for why). What you need is an HTML parser. See Can you provide an example of parsing HTML with your favorite parser? for examples using a variety of parsers.
Regex is generally a bad solution for HTML parsing, a topic which gets discussed every time a question like this is asked. For example, the element could wrap onto another line, either as
<a class="development"
href="[variable content]">X</a>
or
<a class="development" href="[variable content]">X
</a>
What are you trying to achieve?
Using JQuery you could disable the links with:
$("a.development").onclick = function() { return false; }
or
$("a.development").attr("href", "#");
Here's a version that'll allow all sorts of evil to be put in the href attribute.
/<a class="development" href=(?:"[^"]*"|'[^']*'|[^\s<>]+)>.*?<\/a>/m
I'm also assuming X is going to be variable, so I added a non-greedy match there to handle it, and the /m means . matches line-breaks too.