Welcome to WebmasterWorld Guest from 184.108.40.206
Forum Moderators: incrediBILL
Firefox 220.127.116.11 renders the code like this (verified in firebug):
IE6 renders it correctly. I need my divs to be inside my paragraphs for all browsers. This makes no sense to me since they're both just block level elements. What is happening here?
What are the advantages to having p behave this way in the html specs? I always figured p was a regular block level element like a div with default top and bottom margins.
Your figuring is slightly out, though it's understandable as sometimes the terminology or slang we speak can get misunderstood or have different meanings ;)
Div & P, are only both "block level" elements in the formatting sense, not the content model: read more on HTML's definition of it's models [w3.org]
The formatting definition of a block level element is that it creates a new line before and after itself, it is not a complete description of it's role, or what it can contain, that is the role of the content model. For the content model definition of any element you need to look at the definition of the element itself: 9.3.1 Paragraphs: the P element [w3.org] where you see:
<!ELEMENT P - O (%inline;)* -- paragraph -->
Underneath the definition you will see which elements have optional end (closing) tags.
for <p> it is; Start tag: required, End tag: optional
If you go back to the first link, in section 7.5.4 you see that <div> and <span> are classed as "Grouping" Elements, that is imho a good description and covers both formatting and content models:
The DIV and SPAN elements, in conjunction with the id and class attributes, offer a generic mechanism for adding structure to documents. These elements define content to be inline (SPAN) or block-level (DIV) but impose no other presentational idioms on the content. Thus, authors may use these elements in conjunction with style sheets, the lang attribute, etc., to tailor HTML to their own needs and tastes.
It's not that it's an advantage for a <p> to be written like this, it's semantics - in the language of HTML a <p> is a paragraph.
For those elements whose END tag is optional, the technical nature of parsing semantic elements needs to be made clear, the parser needs rules so it understands the language and where to close the element; In the case of <p>'s the parser closes the element as soon as it meets a new block level element as you would if you were writing a paragraph. Which is what firebug showed you FF was doing. With other element it's not quite as sweeping as that as e.g. <li> elements have optional closing and can contain other block level elements but will be closed on meeting a sibling <li>, and <head> gets closed on meeting <body> etc.. anyway this is why people are saying that FF is right. It is, because of the content model rules.
The natural way, non-machine way if you like, to think of these block level, in the formatting context, elements and why they cannot contain other blocks is simple too, Have you ever spoken or written a <p>aragraph with another paragraph inside it, let alone a "grouping" division inside it?
HTML makes a lot of sense, to me anyway, if thought of like the written word. Imagine going off on a rant and writing a long letter of complaint.. I imagine an area (division) of the document would contain many paragraphs (p's) which would have some very colorful words (inline elements). in them :)
Are there any other block level elements that only allow inline elements as children?
[edited by: SuzyUK at 2:18 pm (utc) on Jan. 31, 2008]