问题
Which of the following, if either, is correct by standards?
<!-- Do the links surround the target link object -->
<a href=''><p>Link Description</p></a>
<!-- or does the object type encapsulate the link-->
<p><a href=''>Link Description</a></p>
I know they function the same, But it's a best practice/standards question. This could apply to ul/ol too.
The only reason I think to favor the <a>
tag inside is with a situation like:
<p>This is a longer sentence with a <a href=''>short link here</a></p>
Thanks!
回答1:
The first example is only allowed in HTML5.
The second example is allowed in all versions of HTML/XHMTL.
回答2:
You can't wrap a block element with an inline element. You have to do this in that way:
<p><a href=''>Link Description</a></p>
This is W3C standard. Check this!
回答3:
As of HTML 5, anchor tags (<a></a>
) are allowed to wrap around almost anything, including paragraphs. So either example is valid, although I tend to prefer having anchors inside paragraphs.
回答4:
anchors (a) should be inside a block element. So
<p><a>...</a></p>
回答5:
I would say the second one, than the <p>
is not inheriting attributes of <a>
and keeping it's original formatting.
回答6:
Inline elements such as <a>
are often contained within block-level elements such as <p>
. The block elements provide the structure needed to present your data.
回答7:
I always put the <p>
tags inside unless the amount of text is large.
来源:https://stackoverflow.com/questions/6101711/html-what-is-the-correct-order-of-a-and-p-tags