Within my HTML section of an SHTML page I have the following:
<div class="block_center_1000_text_left">
<p class="anchor2" >
...
<a class="standard_paragraph_18">
<ul>
<li>Robert Schumann: Papillons Op.2</li>
<li>Frédéric Chopin: Preludio in do diesis minore Op.45</li>
<li>Claude Debussy: “Pour le Piano” (Prélude - Sarabande - Toccata)</li>
<li>Franz Liszt: Sonata in si minore R21</li>
</ul>
...
</a>
</p>
</div>
I get a validation error from Dreamweaver:
document type does not allow element "ul" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag [XHTML 1.0 Transitional]
I'm not sure how to interpret the error. I'd like the list of classical composers ordered with a bullet or something like that.
Thanks for looking at this.
A list (<ul>) is not allowed inside an anchor (<a>). Officially that is. A browser will show it nonetheless.
In addition: the HTML5 spec describes the content model (content that is allowed or expected inside an element) of an anchor as transparent. Transparent means that the allowed or expected content is derived from it's parent.
Example: if an anchor's parent is a <p>, you should look to the <p> to see what is allowed. If it's direct parent would've been an <div>, then you should look to the <div>.
In this case, the anchor element is a direct descendent of a paragraph. And a paragraph allowes phrasing content, which is text, or elements that markup text (such as <strong>). But an <ul> is not phrasing content, so should be placed outside of a <p>.
So to sum up, theoratically this would be correct:
<p class="anchor2">Some text about birds or something totally unrelated to birds</p>
<a href="#">
<ul>
<li>Johann Sebastian Bach</li>
...
</ul>
</a>
But in your particular example you only assigned a class to the anchor. But an anchor without an id attribute (which you can use to link to that specific spot in a document) or href element basically doesn't do anything. If you want to style the list, apply the classes to the list itself, not to some parent element.
Related
As far as I know, this is right:
<div>
<p>some words</p>
</div>
But this is wrong:
<p>
<div>some words</div>
</p>
The first one can pass the W3C validator (XHTML 1.0), but the second can't. I know that nobody will write code like the second one. I just want know why.
And what about other tags' containment relationship?
An authoritative place to look for allowed containment relations is the HTML spec. See, for example, http://www.w3.org/TR/html4/sgml/dtd.html. It specifies which elements are block elements and which are inline. For those lists, search for the section marked "HTML content models".
For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements.
<!ELEMENT P - O (%inline;)* -- paragraph -->
This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1, which says that the P element "cannot contain block-level elements (including P itself)."
In short, it is impossible to place a <div> element inside a <p> in the DOM because the opening <div> tag will automatically close the <p> element.
According to HTML5, the content model of div elements is flow content
Most elements that are used in the body of documents and applications are categorized as flow content.
That includes p elements, which can only be used where flow content is expected.
Therefore, div elements can contain p elements.
However, the content model of p elements is Phrasing content
Phrasing content is the text of the document, as well as elements that
mark up that text at the intra-paragraph level. Runs of phrasing
content form paragraphs.
That doesn't include div elements, which can only be used where flow content is expected.
Therefore, p elements can't contain div elements.
Since the end tag of p elements can be omitted when the p element is immediately followed by a div element (among others), the following
<p>
<div>some words</div>
</p>
is parsed as
<p></p>
<div>some words</div>
</p>
and the last </p> is an error.
Look at this example from the HTML spec
<!-- Example of data from the client database: -->
<!-- Name: Stephane Boyera, Tel: (212) 555-1212, Email: sb#foo.org -->
<DIV id="client-boyera" class="client">
<P><SPAN class="client-title">Client information:</SPAN>
<TABLE class="client-data">
<TR><TH>Last name:<TD>Boyera</TR>
<TR><TH>First name:<TD>Stephane</TR>
<TR><TH>Tel:<TD>(212) 555-1212</TR>
<TR><TH>Email:<TD>sb#foo.org</TR>
</TABLE>
</DIV>
Did you notice something? : There was no closing tag of the <p> element. a mistake in the specs ? No.
Tip #1: The closing tag of <p> is OPTIONAL
You may ask: But then how would a <p> element knows where to stop?
From w3docs:
If the closing tag is omitted, it is considered that the end of the paragraph matches with the start of the next block-level element.
In simple words: a <div> is a block element and its opening tag will cause the parent <p> to be closed, thus <div> can never be nested inside <p>.
BUT what about the inverse situation ? you may ask
well ...
Tip #2: The closing tag of the <div> element is REQUIRED
According to O’Reilly HTML and XHTML Pocket Reference, Fourth Edition (page 50)
<div> . . . </div>
Start/End Tags
Required/Required
That is, the <div> element's end will only be determined by its closing tag </div> hence a <p> element inside is will NOT break it.
After the X HTML, the conventions has been changed, and now it's a mixture of conventions of XML and HTML, so that is why the second approach is wrong and the W3C validator accepts the things correct that are according to the standards and conventions.
Because the div tag has higher precedence than the p tag. The p tag represents a paragraph tag whereas the div tag represents a document tag.
You can write many paragraphs in a document tag, but you can't write a document in a paragraph. The same as a DOC file.
As far as I know, this is right:
<div>
<p>some words</p>
</div>
But this is wrong:
<p>
<div>some words</div>
</p>
The first one can pass the W3C validator (XHTML 1.0), but the second can't. I know that nobody will write code like the second one. I just want know why.
And what about other tags' containment relationship?
An authoritative place to look for allowed containment relations is the HTML spec. See, for example, http://www.w3.org/TR/html4/sgml/dtd.html. It specifies which elements are block elements and which are inline. For those lists, search for the section marked "HTML content models".
For the P element, it specifies the following, which indicates that P elements are only allowed to contain inline elements.
<!ELEMENT P - O (%inline;)* -- paragraph -->
This is consistent with http://www.w3.org/TR/html401/struct/text.html#h-9.3.1, which says that the P element "cannot contain block-level elements (including P itself)."
In short, it is impossible to place a <div> element inside a <p> in the DOM because the opening <div> tag will automatically close the <p> element.
According to HTML5, the content model of div elements is flow content
Most elements that are used in the body of documents and applications are categorized as flow content.
That includes p elements, which can only be used where flow content is expected.
Therefore, div elements can contain p elements.
However, the content model of p elements is Phrasing content
Phrasing content is the text of the document, as well as elements that
mark up that text at the intra-paragraph level. Runs of phrasing
content form paragraphs.
That doesn't include div elements, which can only be used where flow content is expected.
Therefore, p elements can't contain div elements.
Since the end tag of p elements can be omitted when the p element is immediately followed by a div element (among others), the following
<p>
<div>some words</div>
</p>
is parsed as
<p></p>
<div>some words</div>
</p>
and the last </p> is an error.
Look at this example from the HTML spec
<!-- Example of data from the client database: -->
<!-- Name: Stephane Boyera, Tel: (212) 555-1212, Email: sb#foo.org -->
<DIV id="client-boyera" class="client">
<P><SPAN class="client-title">Client information:</SPAN>
<TABLE class="client-data">
<TR><TH>Last name:<TD>Boyera</TR>
<TR><TH>First name:<TD>Stephane</TR>
<TR><TH>Tel:<TD>(212) 555-1212</TR>
<TR><TH>Email:<TD>sb#foo.org</TR>
</TABLE>
</DIV>
Did you notice something? : There was no closing tag of the <p> element. a mistake in the specs ? No.
Tip #1: The closing tag of <p> is OPTIONAL
You may ask: But then how would a <p> element knows where to stop?
From w3docs:
If the closing tag is omitted, it is considered that the end of the paragraph matches with the start of the next block-level element.
In simple words: a <div> is a block element and its opening tag will cause the parent <p> to be closed, thus <div> can never be nested inside <p>.
BUT what about the inverse situation ? you may ask
well ...
Tip #2: The closing tag of the <div> element is REQUIRED
According to O’Reilly HTML and XHTML Pocket Reference, Fourth Edition (page 50)
<div> . . . </div>
Start/End Tags
Required/Required
That is, the <div> element's end will only be determined by its closing tag </div> hence a <p> element inside is will NOT break it.
After the X HTML, the conventions has been changed, and now it's a mixture of conventions of XML and HTML, so that is why the second approach is wrong and the W3C validator accepts the things correct that are according to the standards and conventions.
Because the div tag has higher precedence than the p tag. The p tag represents a paragraph tag whereas the div tag represents a document tag.
You can write many paragraphs in a document tag, but you can't write a document in a paragraph. The same as a DOC file.
My question is:
Which html elements can contain other elements with the same tag name. (Like a <div> inside another <div> which is allowed.) And which html-elements (among them who are able to have content) are not allowed to have elements with the same tag name among it's descendants. (Like <p> inside another <p> which is not allowed.)
background
I want to write an html-parser (a lexer to be more precise) to be able to automatically process html-documents that my script reads from internet. I know there are out-of-the-box parsers (and lexers) for almost every language, but I want to try to write my own.
When doing so, there is the problem to handle malformed html, and one of the problems is to close html-elements that have a valid opening-tag, but no close-tag. So you have to make an educated guess where a <div> without matching </div> ends, and where a <p> without matching </p> has it's end.
You can split html-elements into three classes:
Elements that per definition can't have any content, like <img> or <br>
Elements that can contain descendants of the same type (<div> in <div> is allowed)
Elements that can have content, but not of the same element-type (<p> can contain text, <a> and many other elements, but <p> in <p> is not allowed)
Here I'm not interested in the "void elements" as described in 1 because those elements can't have close tags (and so, they never will miss closing tags).
When it comes to create closing tags that was missing, types 2 and 3 must be handled differently.
If your receive a document that contains this:
<body> a <div> b <div> c </body>
most browser will internally transform it into something like this:
<body>
a
<div>
b
<div>
c
</div> <!-- inserted -->
</div> <!-- inserted -->
</body>
All divs are closed at the same point, just before the first existing non-div-closing-tag that, together with its matching opening-tag, embraces the div-tags who's closing tags are missing. This algorithm gives nested elements where each unclosed element becomes the child of its previous unclosed fellow.
But if you get this
<body> a <p> b <p> c </body>
most browsers will convert it into this:
<body>
a
<p>
b
</p> <!-- inserted -->
<p>
c
</p> <!-- inserted -->
</body>
In this case one p-element is closed when the next p-element begins, or when a non-p-closing-tag is detected, who's opening-partner lays before the opening p-tag who's closing tag is missing. This algorithm does not produce nested elements of the same type, but produces siblings who are children of the same parent.
And to be able to decide which algorithm should be used to close elements, I need to know which elements belong to which class.
I get this error after validating my HTML.
Line 18, Column 60: document type does not allow element "div" here; missing one of "object", "applet", "map", "iframe", "button", "ins", "del" start-tag
<a href="index.html"><div class="item" id="home"><br />
I think it says I'm not allowed to use DIVs in 'a' tags, but I have to use a layer if not a div here in order to get desired view. What can I use instead of div?
The error message includes these lines:
One possible cause for this message is that you have attempted to put a block-level element (such as "" or "") inside an inline element (such as "", "", or "").
I get another error message for the line:
<a class="img" href="#"><li>Item 1</li></a>
I think I can't use <li> </li> tags inside <a> </a> tags, but in this case I have to, what do you suggest?
Use a <span> instead, this is legal.
The only difference between a <span> and a <div> is that a <span> is a display:inline; element by default and <div> is a display:block; element by default.
You want to use an inline element within an inline element (<a>) because having a block element inside one doesn't make sense. An inline element continues with the 'flow' of a page while a block element breaks the flow (ie. goes to the next line).
Normally in these situations you can use a <span> without any problems.
EDIT: Making your <a> element have display:block; make also solve your problem.
EDIT: It is not actually valid HTML to have an <li> within an <a> tag, they must be within either a <ul> or <ol>.
Here are some options that will work fine, no extra CSS needed.
<a class="img" href="#">
<span>Item 1</span>
</a>
Or
<div>
<a class="img" href="#">Item 1</a>
</div>
Or
<ul>
<li>
<a class="img" href="#">Item 1</a>
</li>
</ul>
The only way to validate is changing your doctype to HTML5 as explained in this previous post:
Valid HTML: ul, p, div inside 'a' element
Hope this helps.
what is the best way to make a link in html
i mostly use html4 but im trying to implement html5
so my question is basically what should be the wrapping tag, the p or a?
this:
<p class="center">
<a href="#">
<img src="addToCart.png">
</a>
</p>
or this:
<a href="#">
<p class="center">
<img src="addToCart.png">
</p>
</a>
thanks.
Under HTML 4.1 an a element, display: inline by default, cannot contain a block-level element, such as a p; therefore p should wrap a.
If, though, you're using HTML 5, and have <!doctype html>, then either way is acceptable (and valid), so use whichever you find best meets your requirements.
Anchors can only contain inline elements (span, strong, em etc). Paragraph tags are block - and therefor not valid. Set the display:inline and it will certainly work, but its no longer valid.
Your first example is correct.
As noted = this is true in html4 only.
Spec for <a>
Spec for <p>
In summary, both of the above can be used and are valid. <a> has a transparent content model (with the exception of excluding other interactive content) so it can wrap practically anything. <p> has "phrasing content" as its model, and <a> is in the phrasing content category. <img> is also in "phrasing content."
Thus, both of your examples are totally valid. It entirely depends on what you want to do specifically.