Can I have a <pre> Tag inside a <p> tag in Tumblr?

痴心易碎 提交于 2019-11-29 17:02:56

问题


today I played around a little bit with Tumblr. I tried to add a preformatted text into a description paragraph block like so:

{block:Description}
    <p id="description">{Description}</p>
{/block:Description}

However, the <pre> element gets rendered just after the <p> element and not within it which was my intention.

Am I doing something wrong here or is it a bug?


回答1:


You cannot nest block-level elements, such as <pre>, inside <p> in HTML. So Tumblr is behaving correctly.

You should close your paragraph before inserting the pre-formatted text.

If you want teletype-style text, try the <tt> tag, but note it is a non-standard element and may not render in all browsers. The correct way to achieve formatting is using CSS.




回答2:


Also, I just realized where I was having a similar issue pasting code into tumblr inside <pre> and <code> tags is that just because you wrap your code in those tags doesn't prevent you from having to escape the carrots with &lt; and &gt; instead of the carrots.




回答3:


And the HTML5 terminology is:

  • p's "Content model" is "Phrasing content": "https://html.spec.whatwg.org/multipage/grouping-content.html#the-p-element
  • pre's "Categories" are: "Flow content" and "Palpable content": https://html.spec.whatwg.org/multipage/grouping-content.html#the-pre-element

Since pre's categories don't include "Phrasing content", pre cannot be put inside p.

Maybe it is possible to simulate pre with CSS styles though: How to change a span to look like a pre with CSS? but I'm not sure it is allowed in p.

See also: List of HTML5 elements that can be nested inside P element?



来源:https://stackoverflow.com/questions/5371787/can-i-have-a-pre-tag-inside-a-p-tag-in-tumblr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!