homepage Welcome to WebmasterWorld Guest from 54.163.70.249
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / HTML
Forum Library, Charter, Moderators: incrediBILL

HTML Forum

    
Firefox rendering div inside p
wiglaf




msg:3560082
 10:31 pm on Jan 28, 2008 (gmt 0)

my doctype:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

my html:
<p>
Test:
<div>test1</div>
<div>test2</div>
<div>test3</div>
</p>

Firefox 2.0.0.9 renders the code like this (verified in firebug):
<p>
Test:
</p>
<div>test1</div>
<div>test2</div>
<div>test3</div>

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?

 

varya




msg:3560098
 10:48 pm on Jan 28, 2008 (gmt 0)

Only inline elements are allowed inside p tags.

div isn't just a block level element, it's a *grouping* element. Paragraphs aren't.

g1smd




msg:3561288
 8:22 pm on Jan 29, 2008 (gmt 0)

Use <span> inside other block-level elements.

Fotiman




msg:3561332
 9:00 pm on Jan 29, 2008 (gmt 0)

Or change your <p> to a <div>.

Fotiman




msg:3561333
 9:02 pm on Jan 29, 2008 (gmt 0)

IE6 renders it correctly.

Actually, no it doesn't (if IE is rendering the div's inside the <p>). On the contrary, (and as usual) Firefox displays it correctly and IE does not.

wiglaf




msg:3562666
 8:53 am on Jan 31, 2008 (gmt 0)

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. Are there any other block level elements that only allow inline elements as children? It's just weird cause I've been writing html for 8 years and have never run into this spec.

SuzyUK




msg:3562832
 2:12 pm on Jan 31, 2008 (gmt 0)

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 -->
in english this means element <p> can only contain inline elements and is a paragraph. It's the 'paragraph' bit that defines the semantics of that particular element.

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?

Yes, The "heading" elements [w3.org], <h1>, <h2>, etc.. some more but likely not all : <address>, <dt>, <pre>

-hth

[edited by: SuzyUK at 2:18 pm (utc) on Jan. 31, 2008]

wiglaf




msg:3563113
 6:58 pm on Jan 31, 2008 (gmt 0)

Wow, great post. Thanks.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / HTML
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved