0

So my div is being pushed out of my

$html = '
<p>
    <a href="*link*?id='.$newsr["topic_id"].'" title="'.$newsr["topic_subject"].'">'.$newsr["topic_subject"].'</a><div style="float:right;">'.$newsr["topic_date"].'</div>
</p>';
echo $html;

with as result :

<p>
    <a href="*link*?id=21" title="GHBJN">GHBJN</a>
</p>
<div style="float:right;">2013-07-18 17:05:09</div>

Why is this happening? How do I fix this?

crush
  • 16,713
  • 9
  • 59
  • 100
Jacob Brol
  • 91
  • 13

2 Answers2

7

The <p> tag can only contain phrasing content (see W3C).

The <div> element on the other hand is a block element, which implicitly closes any open paragraphs tags as defined in the standard.

A p element’s end tag may be omitted if the p element is immediately followed by an address, article, aside, blockquote, dir, div, dl, fieldset, footer, form, h1, h2, h3, h4, h5, h6, header, hr, menu, nav, ol, p, pre, section, table, or ul element, or if there is no more content in the parent element and the parent element is not an a element.

This is exactly, what is applying here.

Sirko
  • 72,589
  • 19
  • 149
  • 183
  • I believe it's allowed in html5 – Arnaud Le Blanc Aug 22 '13 at 12:28
  • 2
    @arnaud576875 Sure it is allowed. But the defined behavior may differ from what is expected (by an inexperienced user). – Sirko Aug 22 '13 at 12:30
  • Care to explain why ? – Arnaud Le Blanc Aug 22 '13 at 12:39
  • @arnaud576875 If you do not know the behavior concerning implicit closing tags, you could assume, that the `
    ` should be contained within the `

    ` and hence, e.g., the appropriate styling is applied. The defined behavior, however, results in different structures like shown by OP.

    – Sirko Aug 22 '13 at 12:46
1

One one does not simply put <div> element in <p> element.


edit: for more explanation please refer to this question.

Community
  • 1
  • 1
Crazy Yoghurt
  • 2,365
  • 2
  • 26
  • 37