Html:<a> 和 <p> 标签的正确顺序是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6101711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:44:19  来源:igfitidea点击:

Html: What is the correct order of <a> and <p> tags?

htmlanchor

提问by j_syk

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.

我知道它们的功能相同,但这是一个最佳实践/标准问题。这也适用于 ul/ol。

The only reason I think to favor the <a>tag inside is with a situation like:

我认为支持<a>内部标签的唯一原因是以下情况:

<p>This is a longer sentence with a <a href=''>short link here</a></p>

<p>This is a longer sentence with a <a href=''>short link here</a></p>

Thanks!

谢谢!

回答by edeverett

The first example is only allowed in HTML5.

第一个示例只允许在 HTML5 中使用。

The second example is allowed in all versions of HTML/XHMTL.

在所有版本的 HTML/XHMTL 中都允许使用第二个示例。

回答by ChristianB

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>

<p><a href=''>Link Description</a></p>

This is W3C standard. Check this!

这是 W3C 标准。检查这个

回答by Karl Nicoll

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.

从 HTML 5 开始,锚标记 ( <a></a>) 几乎可以环绕任何内容,包括段落。所以任何一个例子都是有效的,尽管我更喜欢在段落内有锚点。

回答by George Kastrinis

anchors (a) should be inside a block element. So

锚点 (a) 应该在块元素内。所以

<p><a>...</a></p>

回答by mrkva

I would say the second one, than the <p>is not inheriting attributes of <a>and keeping it's original formatting.

我会说第二个,而<p>不是继承属性<a>并保持其原始格式。

回答by George Cummins

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.

内联元素<a>通常包含在块级元素中,例如<p>. 块元素提供了呈现数据所需的结构。

回答by Mertis

I always put the <p>tags inside unless the amount of text is large.

我总是把<p>标签放在里面,除非文本量很大。