<li><a>... or <li><p><a>

When do we need to wrap <p> around a, span and other tags?

As for example inbox messages list:


<li>
<a href="#">First name</a><strong>Message title lorem ipsum</strong><span>12.02.2010</span>
</li>
<!--or should be:-->
<li>
<p><a href="#">First name</a></p><p><strong>Message title lorem ipsum</strong></p><span>12.02.2010</span>
</li>
<!--or even like this:-->
<li>
<p><a href="#">First name</a></p><p><strong>Message title lorem ipsum</strong></p>12.02.2010
</li>

Which is the most appropriate? Could I just skip span from the last example? Do I need p in the second example? I hope anybody can explain this.

You don’t need to do anything. The only two things you need to make sure are: mark up needs to be semantic AND correct.

The last two examples are not correct. The <li> content model is %flow, which means either %block either %inline, but not both.

In the second example you have both %block and %inline:


<li>
<p><a href="#">First name</a></p> [COLOR="Blue"](%block)[/COLOR]
<p><strong>Message title lorem ipsum</strong></p> [COLOR="Blue"](%block)[/COLOR]
<span>12.02.2010</span> [COLOR="Red"](%inline)[/COLOR]
</li>

and the same is true for the third example:


<li>
<p><a href="#">First name</a></p> [COLOR="Blue"](%block)[/COLOR]
<p><strong>Message title lorem ipsum</strong></p> [COLOR="Blue"](%block)[/COLOR]
12.02.2010 [COLOR="Red"](%inline)[/COLOR]
</li>

The first example could be re-written like this:


<li>
<a href="#">First name</a>[COLOR="Blue"] (%inline (%special) )[/COLOR]
<span><strong>Message title lorem ipsum</strong>12.02.2010</span> [COLOR="Blue"](%inline (%special) )[/COLOR]
</li>

EDIT: WARNING: <p> is not a wrapping element, it’s an element with semantic meaning. The way I see it, from the last two examples you provided, you’ve seen it used wrong because, being a block-level element, it will act like a building block, starting on a new line AND making elements that follow start on a new line, below.

For that effect, to start with, when there is no CSS (yet) applied, in the initial mark up the alternative is the <br> element:


<li>
<a href="#">First name</a>[COLOR="Blue"] (%inline (%special) )[/COLOR]
<br>[COLOR="Blue"] (%inline (%special) )[/COLOR]
<span><strong>Message title lorem ipsum</strong>12.02.2010</span> [COLOR="Blue"](%inline (%special) )[/COLOR]
</li>

but <br> should be ruled display:none in CSS, and you should issue a display:block for <a> or <span>, if you want them to start on new lines.

just use the first one. that is correct.

there’s nothing inherently wrong with a list consisting of paragraphs

but if you’re going to use paragraphs as your list items, make sure that everything in each list item is a paragraph


<ul>
<li><p>This is a paragraph in list item 1.</p>
    <p>This is another paragraph in list item 1.</p></li>
<li><p>This is a paragraph in list item 2.</p>
    <p>This is another paragraph in list item 2.</p></li>
</ul>

Ahem… no!

Make sure the content for each list is either %inline, either %block. Just because one <li> has %block content, it doesn’t mean that ALL should follow that rule and have %block content. It has to be consistent only at <li> level, not at <ul>/<ol> level.

The following is valid:


<ul>
<li>
[COLOR="Blue"]    <p>This is a paragraph in list item 1.</p>
    <p>This is another paragraph in list item 1.</p>[/COLOR]
</li>
<li>
[COLOR="RoyalBlue"]    <span>This is a text in list item 2.</span>
    <span>This is another text in list item 2.</span>[/COLOR]
</li>
</ul>

And paragraphs should be used ONLY if the text is a paragraph, not to format text, e.g.


[COLOR="Blue"]<li>
    <span>This is a text.</span>
    <br>
    <span>This is another text I want it to appear on a new line.</span>
</li>[/COLOR]

instead of


[COLOR="Red"]<li>
    <p>This is a text.</p>
    <p>This is another text I want it to appear on a new line.</p>
</li>[/COLOR]

thank you for confirming that what i posted is valid

i did not really need your confirmation, though

:cool:

I’m sorry if you understood I agreed with you. :cool:

I don’t! You’ll notice that there is something different in my code. :slight_smile:

is WRONG! On so many levels. :stuck_out_tongue:

What you’ve posted doesn’t make sense and it’s not valid explanation!

And I didn’t do it for you, dear r937 :slight_smile: I did it for the OP to understand the errors and the traps in your post.

there goes noonnope being noonnope again

i do realize your first language isn’t english so i will cut you some slack, suffice to say that you consistently come across as confrontational if not outright hostile

but thanks for at least raising the issue, as it allows me to make a correction…

but if you’re going to use paragraphs as your list items, make sure that everything in each list item is a block-level item

so this would also be okay –


<li><p>This is a paragraph in list item 3.</p>
    <p>This is another paragraph in list item 3.</p>
    <ul><li>curly</li><li>larry</li><li>moe</li></ul></li>

Me correcting you again:

If you’re going to use %block (paragraphs) content IN SOME of your list items, YOU DON’T HAVE to make sure that EVERYTHING in EACH list item is a block-level item, BUT just in THOSE PARTICULAR <li>s.

Is it clear enough now? <li>s MUST have EITHER %block EITHER &inline content, BUT they CAN be different from each other to that concern. Whilst you imply that if ONE has %block content ALL must do so. WRONG!

Off Topic:

Yeah, personal issues of r937 all over again. (Yeah, I understand Latin better than you, so? :wink: Especially because I’m ESL.) :slight_smile:

If you have a problem with me correcting you (again), feel free to report me or PM me. But let’s keep this post technically accurate and useful for the OP. OK? :cool:

But I’m sure you’ll see my tone if far less confrontational and hostile than yours. Look in this thread alone.

plonk

noonnope thank you for answer, but now I am really confused and 2 extra questions have appeared :slight_smile:

Why to not mix inside li inline and block content? Is it about validation? I thought that since li is block level element, it can contain both. Is that rule only for li, to not mix block and inline items?
2.
You wrote:“And paragraphs should be used ONLY if the text is a paragraph, not to format text, e.g.”
What exactly do you mean by not to format text? Author of the the sitepoint css book (Build Your Website the Right Way Using HTML&CSS) also used p to “wrap” labels and inputs:


<p>
<input type="checkbox" name="terms" id="terms"/>
<label for="terms">I have read the terms and conditions.<label>
</p>
<p>
<input type="checkbox" name="newsletter" id="newsletter"/>
<label for="terms">Subscribe me to your weekly newsletter.<label>
</p>

So this is not correct since it is not strictly pharagraph?
3.
And finally back to my origin question. Can be just <li>lorem ipsum</li> without span or p?

Thank you again!

yes :slight_smile:

My understanding is that, on a technical level, you can have adjacent <li>s within the same <ul>/<ol> that have different content models, ie one can have block elements and the next can have inline text and elements. It’s just within each individual <li> that you have to decide on either inline or block content.

I don’t like that, I think it’s messy and I think that from a purist point of view the content model should be the same throughout the list, but I’ve not seen anything that actually spells out that it has to be. Unless you can say otherwise?

Let’s start with the final question:

Yes.

Let’s get a little technical. Looking at the DTD for <ul>, we see this:

<!ELEMENT UL - - (LI)+                 -- unordered list -->

which is to say ul can ONLY have as descendants, li elements.

Let’s take a look at what DTD says about li:

<!ELEMENT LI - O (%flow;)*             -- list item -->

which is to say that li elements must have opening tags, have optional closing tags, and the content they allow is repeated %flow content.

We need to see what DTD says about %flow:

<!ENTITY % flow "%block; | %inline;">

which is to say %flow means EITHER %block EITHER %inline.

Looking at %inline definition:

<!ENTITY % inline "#PCDATA | %fontstyle; | %phrase; | %special; | %formctrl;">

the first type of allowed content is #PCDATA, loosely meaning character data (see this http://www.sitepoint.com/forums/showthread.php?t=612147).

From the top:
<li>lorem ipsum</li> = <li>CDATA</li> = <li>%inline</li> = CORRECT!

The above answers point 1. also.

Using p as a wrapper just to make elements or group of elements start on a new line. p is just not for that.

No, it’s not semantic, it’s used only to make a group of elements start on a new line, a feature %block elements like p have. So, no, it’s not correct. p is not a wrapper or a container for simulating style.

EDIT: The correct element there would be fieldset. And, if you have inline elements you’d like to start on a new line, <br> is the answer.

great, it is clear now :slight_smile:

Ok :slight_smile:

Off Topic:

  1. Plonk

  2. The sound your poo makes when it hits water

squeeze plonk!

http://www.urbandictionary.com/define.php?term=plonk

:rofl:

noonnope really hit it on the head with the “it’s not a paragraph” – something people coming from typography often fail to grasp is P doesn’t mean typographical paragraph mark, no matter how much they want it to.

P means it’s a grammatical paragraph of text… You don’t have enough related text forming a coherent thought there to be using paragraphs in the first place!

Is it a list item, or is it a paragraph – good rule of thumb? Pick one, not both. If it’s enough text to warrant multiple paragraphs and it’s coherent thoughts/sentences, it’s probably too big to be in a UL in the first place… Since at that point it stops being a bullet point list item. (and before someone says it, I mean grammatical bullet point NOT the presentational “happens to have a bullet in front of it”!!!) – but to put that in perspective I actually LIKED the rules for LI under the deprecated MENU tag, and wish that was the rules for all LI as it would make the point of a “list item” clearer, and explain why just because you have a list it does NOT neccesarily have to be shoe-horned into a UL, OL or DL – particularly when you are applying the WRONG semantic meaning by doing so.

Bottom line remember, the point of GOOD HTML is to say what things are, NOT how they are going to appear. When you write your initial markup you should NOT be thinking appearance at ALL. Logical document structure, logical heading orders to divide up that documnet, DIV’s to group like elements together – all saying what things ARE.

Appearance? That’s either the user agent or CSS’ job.

Chiming in an supporting what Noonnope and death have said… but in a user friendly way:

  1. its ok to use block elements ( a P for example) OR inline elements as CHILDREN( direct descendants) of an LI, but you CANT use elements AND inline elements; pick one or the other according to what s most semantically correct.

  2. So that leaves WHEN to use what. This is echoing shadow, then you think about it your example is a quasi sentence, for lack of a better term. it would read sort of like this:

First name[/I]Message title lorem ipsum[/I]12.02.2010

(ignore the styling, I put it so you could see the segments).

I disagree with shadow is that it is possible to have one sentence paragraphs, even one SHORT sentence paragraphs. not common in tech writing, but look at advertising… or fiction or even biographies (ughh).

you you could wrap your “sentence” in a P, but it would become obvious that without something else as part of the content of the LI , the P tag is superfluous and can be removed. and thus your first example is the best.

but do note, that there might be a situation in which you want to have a short paragraph of text along with the meta info…

<li>
<p><a href=“#”>First name</a><strong>Message title lorem ipsum</strong><span>12.02.2010</span></p>
<p> this would be the first paragraph of the message. maybe a teaser of the article… btw why would you put a link to the name and not the title… but whatever</p>
</li>

in that case you WOULD have to wrap BOTH in a paragraph… or considering using a DL , using DTs to contain your name link, title and time, and the DD for the message… ( ironically enough: inline, inline, inline, block!

lol

Two questions:

  1. What?
  1. What?

But really expecting a documented answer to the second one only : )

I may be wrong on this one, but it seems to me you’re suggesting this?


<li>
  <p>
    <p><a href="#">First name</a><strong>Message title lorem ipsum</strong><span>12.02.2010</span></p>
    <p> this would be the first paragraph of the message. maybe a teaser of the article... btw why would you put a link to the name and not the title.. but whatever</p>
  </p>
</li>

If not, then just forget about it : ) My bad. Apologies in advance.

Of course, the first paragraph is just wrong. Those a, strong and span elements should not be in a p, and should be like this:


    <a href="#">First name</a>
    <span>
       <strong>Message title lorem ipsum</strong> 12.02.2010
    </span>

You can’t justify the use of p as a wrapper for them only because a p is the next element to follow. Not that it should, but hey, let’s say that a p would follow ; )