What are the allowed elements within the P tag?

11

I've been through a few issues, and I could still see in this answer that the <p> tag does not accept any element as a child.

It seems that there are exceptions to some tags, that is, they can be added within a <p> , such as <br> , <a> .

But I'm always in doubt when I want to use this <p> tag on what I can or can not put inside it.

So, I wanted to know:

  • What is the sense of having this restriction of the <p> tag not accepting certain elements?
  • What are the allowed elements within a <p> tag?

Note : The linked question answers that the <p> tag does not accept certain elements, but does not explain why or why some are accepted.

    
asked by anonymous 10.07.2018 / 18:16

3 answers

13

The <p> element is categorized as flow element and < a href="https://html.spec.whatwg.org/multipage/dom.html#palpable-content-2"> palpable element , and can therefore be used anywhere an element is expected flow. Allows as child elements any element that is a phrase element .

The definitions of each other, in addition to the links mentioned, are found in W3C, 3.2 Elements , 3.2.5 Content models , 3.2.5.1 Kinds of contents :

The relationship between the groups is:

Source and version of the iterative image: link

In a simplified way, sentence elements are the elements that define the content of the application or its formatting - it is worth mentioning the word formatting here, since formatting is not stylized (CSS applies styles).

At the moment, the phrase elements are (links to the documentation you find here ):

  

a, abbr, area (if descending from a <map> ), audio, b, bdi, bdo,   embed, dfn, datalist, datalist, dite, date,   i, iframe, img, input, ins, kbd, label, link (if allowed in   body), map, mark, MathML math, meta (if you have the attribute    itemprop ), meter, noscript, object, output, picture, progress, q,   s, script, select, slot, small, span, strong, sub, sup,   svg, template, textarea, time, u, var, video, wbr, autonomous custom   elements and text .

If other elements are added as children of <p> , it is possible for the element to close earlier than expected when the client processes the HTML structure, as Rafael commented on your answer .

The specification also explicitly cites the example of defining lists within a paragraph, where it is not possible to use elements <ol> and <ul> within <p> .

p > ul {
  color: red;
}
<p>
  <ul>
    <li>Item 1</li>
    <li>Item 2</li>
  </ul>
</p>

The interpreted HTML will be:

<p></p>
<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>
<p></p>

So much so that CSS, which defines the red font for a ul element within a p , did not work. In this case, the ideal is, as it was interpreted, to generate two distinct paragraphs, one before and another after the list or define the content within another element, as well as div .

<div>For instance, this fantastic sentence has bullets relating to
<ul>
 <li>wizards,
 <li>faster-than-light travel, and
 <li>telepathy,
</ul>
and is further discussed below.</div>

That will be interpreted exactly the way it is.

    
10.07.2018 / 18:58
10

The <p> tag defines a paragraph.

Within the <p> tag, any plain text content and markup is allowed, such as: <abbr>, <audio>, <b>, <bdo>, <br>, <button>, <canvas>, <cite>, <code>, <command>, <data>, <datalist>, <dfn>, <em>, <embed>, <i>, <iframe>, <img>, <input>, <kbd>, <keygen>, <label>, <mark>, <math>, <meter>, <noscript>, <object>, <output>, <progress>, <q>, <ruby>, <samp>, <script>, <select>, <small>, <span>, <strong>, <sub>, <sup>, <svg>, <textarea>, <time>, <var>, <video>, <wbr>

If you insert within the <p> tag some element such as: <address>, <article>, <aside>, <blockquote>, <div>, <dl>, <fieldset>, <footer>, <form>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <header>, <hr>, <menu>, <nav>, <ol>, <pre>, <section>, <table>, <ul>

The paragraph will be automatically closed. Let's look at the example:

<p> teste <div> div </div> </p>

By analyzing the source code, the <p> tag will close automatically.

<p> teste </p>
 <div> div </div>
<p></p>

Sources : The Paragraph element , Content with Allowed Text , W3C .

    
10.07.2018 / 18:59
-1

It's about web semantics.

The "p" comes from a paragraph, so it is expected that the content of this tag will have plain text and, at most, text formatting tags, although this can be done more appropriately with CSS.

    
10.07.2018 / 18:27