Which is standard compliant between these two?
<p>Text text text ... <ol> <li>First element</li> </ol> </p> <p> Other text text ... </p>
<p> Text text text ... </p> <ol> <li>First element</li> </ol> <p> Other text text ... </p>
List elements (in particular, ol and ul elements) cannot be children of p elements. When a sentence contains a bulleted list, therefore, one might wonder how it should be marked up.
No. If it's list, it has list-items in it.
For semantic purposes, content within the li element should be wrapped in a p tag. If there is more than one paragraph or section in a li , do not close the li until after the last element. This differs from the current standard, but is cleaner for accessible content.
The children (direct descendants) of a ul element must all be li elements. This is a purely syntactic requirement. The way to fix the error depends on semantics, however. If the inner lists correspond to subtopics of the topic of the preceding li , then you should wrap the inner list inside that li , e.g.
The short answer is that ol
elements are not legally allowed inside p
elements.
To see why, let's go to the spec! If you can get comfortable with the HTML spec, it will answer many of your questions and curiosities. You want to know if an ol
can live inside a p
. So…
4.5.1 The
p
element:Categories: Flow content, Palpable content.
Content model: Phrasing content.
4.5.5 The
ol
element:Categories: Flow content.
Content model: Zero or more li and script-supporting elements.
The first part says that p
elements can only contain phrasing content (which are “inline” elements like span
and strong
).
The second part says ol
s are flow content (“block” elements like p
and div
). So they can't be used inside a p
.
ol
s and other flow content
can be used in in some other elements like div
:
4.5.13 The
div
element:Categories: Flow content, Palpable content.
Content model: Flow content.
The second. The first is invalid.
A browser will handle it like so:
<p>tetxtextextete <!-- Start of paragraph --> <ol> <!-- Start of ordered list. Paragraphs cannot contain lists. Insert </p> --> <li>first element</li></ol> <!-- A list item element. End of list --> </p> <!-- End of paragraph, but not inside paragraph, discard this tag to recover from the error --> <p>other textetxet</p> <!-- Another paragraph -->
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With