12

It seems that (strict) html doesn't allow nesting any non-inline elements inside a <p>, but then how am I supposed to render a paragraph that contains a list (something that occurs often in natural texts). I've seen three answers that all seem unsatisfying:

  • Lists should not happen inside paragraphs. (I'm not going to go into a cultural debate, but I certainly hope that html is not going to become the place to dictate writing style.)
  • Separate the text into a paragraph, then a list, and then a second paragraph with the post-text. This looks bad because now I have <p> chunks that are parts of paragraphs, and that seems bad for a markup that is trying to to move in a more semantic direction.
  • Fake paragraphs using some <div class="mypara"> — which looks like a bad way to bypass the whole thing and give up on using the markup with the proper semantics for text.

Is there some other way to do this that is semantically correct and valid?

Zee
  • 123
  • 1
  • 5
  • +1 for pointing out the common work-arounds for this issue. The problem is even more visible, when you use a list in the middle of a sentence to emphasize its structure, as splitting a sentence across different paragraphs is definitely semantically incorrect. E. g. „I want to buy [list]• milk, • sugar, • Apple, and • a viola[/list] today.“ This is commonly used when typesetting text with TeX. – Palec Nov 22 '13 at 18:01

5 Answers5

10

Paragraphs in HTML 5, as of the latest working draft, are defined as flow elements which can contain phrasing elements only. Lists are also defined as flow elements, and may not be enclosed in paragraphs. Whatever you think the definition of paragraph should be, this is the formal definition of the term in HTML, and I think it's unlikely to change.

There are two possibilities that you might consider besides the two you've mentioned:

  • Consider reaching for a more broad-reaching flow element than paragraph if it applies, such as section, nav, header, footer, or article.
  • Use a hybrid approach: wrap the p – ol – p sequence with a div of your choosing that applies common formatting to the set.

As far as div goes, the HTML 5 spec clearly recommends that it should be a "last resort" element because it doesn't bear semantic meaning in the way that other HTML elements do, and may not be as user-friendly in alternative user agents. Going by this advice, I wouldn't use div at the cost of p for body text if semantics are important to you. However, it can be useful in making sure that the use of multiple paragraphs does not get too visually choppy.

Owen S.
  • 7,665
  • 1
  • 28
  • 44
  • The situation I'm faced with is of a system for generating HTML, and it seems that the best option in this case is therefore to use `div`s. Other than that, the suggestion of using the wider tags seems like a good idea too. – Zee Jul 07 '10 at 03:14
3

I don't think the html <p> tag is meant to control the idioms of actual writing styles. It is simpy a way to display a chunk of text in an orderly manner. I don't believe it is meant to simulate printed material. You will have to use a Div for this one.

John
  • 1,530
  • 1
  • 10
  • 19
  • If it's only for formatting, then it seems like there's very little point in using the `p` tag anyway... But I'm pretty sure that I've seen some mentions of browsers that treated them differently — but I don't remember where. (Maybe something related to screen readers.) – Zee Jul 07 '10 at 02:08
  • Some may handle it diffirently, but you never want to stake the quality of your work on those implimentations. You always, always, want to shoot for the middle ground; the more users you can make happy the better off your site will be. – John Jul 07 '10 at 02:38
  • According to the HTML 4.01 spec the paragraph is an inline element. That is why they do not want nesting. – John Jul 07 '10 at 03:21
3

Is there a visual style reason behind all this? In other words when you have something like the following, is it visually rendering in a way that you do not find ideal?

<p>Some paragraph text.</p>
<ul>
    <li>First list item</li>
    <li>Second list item</li>
    <li>Third list item</li>
</ul>
<p>Another paragraph.</p>

If the problem is too much margin after the P and before the UL, then you can try an adjacent sibling selector like this to compensate:

p + ul { margin-top: -1em; }
Andy Ford
  • 8,410
  • 3
  • 26
  • 36
  • 1
    The visual rendering wasn't the problem (divs work fine anyway) — it was an attempt to find some "right" way to do this, keeping the tags meaningful. – Zee Jul 07 '10 at 08:11
1

I suppose it depends on what you consider to be a "paragraph". It is obvious to me that the designers of HTML in no way consider a list to be part of a paragraph. And while I think an argument could be made for either interpretation, I'm of the opinion that the spec is correct.

While a list doesn't represent a break in line of thought, it certainly represents a change in voice or method of thought, which I believe merits a new semantic container.

In the same vein, a paragraph represents a logical partition in a piece of writing. A list represents a sequence of logical partitions, and while it is imaginable that one logical partition may be then divided into more partitions, it makes more sense to simply create a new partition (especially for such a large break in the voice/method).

Semantics can be largely subjective. And that's fine. However, in this case, I'm of the opinion that the HTML spec correctly represents the semantics.

Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
  • 1
    Well, I specifically avoided discussing whether this is a correct decision or not — more to the point, I *don't* think that the HTML spec authors should be in a position where they determine whether a list-in-a-paragraph is something that should be permitted or not. That requires thorough investigation into typographical works across different cultures, and there are certainly plenty of examples of typographical systems (La/TeX being an obvious one) that permit nested lists in paragraphs. – Zee Jul 07 '10 at 03:06
  • I suppose, then, that the easy answer to your question is "no" – Ryan Kinal Jul 07 '10 at 03:09
  • BTW — personally, I think that if the official spec's answer is that you shouldn't write a list inside a paragraph because it's wrong, and if there was no such thorough investigation (which the Unicode authors invest a ton of time on), then this is just plain arrogance. This is why I expect the actual answer to be based on technical issues of rendering and presentation rather than deciding for me what should be considered good style. – Zee Jul 07 '10 at 03:10
  • 1
    @Zee: I'm guessing legacy is the driving factor at this point, actually, whether p should be able to contain lists or not. The concept that ul and ol should imply a paragraph break dates all the way back to HTML 1.2. – Owen S. Jul 07 '10 at 08:16
0

The answer to your question is obviously no, because the specs say you can't do it. Like you, I like to maintain semantics, so I usually go with your 2nd option (paragraph, list, another paragraph). However, off the top of my head, I can't think of any text where the list doesn't actually break the flow of text, so it seems that starting a new paragraph after a list can't hurt.

casablanca
  • 69,683
  • 7
  • 133
  • 150
  • It's probably more common for the list to be part of the preceding text (and I know that there won't be any difference in how it renders, but Im trying to figure out what the right thing is...). – Zee Jul 07 '10 at 02:11