HTML 标记中的链接内的标题或标题内的链接?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11145861/
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 01:11:57  来源:igfitidea点击:

Header inside Link or Link inside Header in HTML markup?

html

提问by Renato Dinhani

Possible Duplicate:
Which is more correct: <h1><a>..</a></h1> OR <a><h1>..</h1></a>
Semantically, which is more correct: a in h2, or h2 in a?

可能的重复:
哪个更正确:<h1><a>..</a></h1> OR <a><h1>..</h1></a> 在
语义上,哪个更正确:a in h2,还是在a中的h2?

This is a simple question about HTML. What is the right/most preferable way to put a link in a header, paragraph or another HTML element?

这是一个关于 HTML 的简单问题。在标题、段落或其他 HTML 元素中放置链接的正确/最可取的方法是什么?

This way:

这边走:

<h2><a href="">Some Title</a></h2>

<h2><a href="">Some Title</a></h2>

Or this way:

或者这样:

<a href=""><h2>Some Title</h2></a>

<a href=""><h2>Some Title</h2></a>

回答by Mateusz Rogulski

Better is

更好的是

<h2><a href="">Some Title</a></h2>

because his element with display block and is not good practice to have blockin aelement.

因为h是与显示模块元素,是不是很好的做法,blocka元素。

回答by animuson

To note: You can't put the header inside the link in HTML4.

注意:您不能将标题放在 HTML4 中的链接内。

In HTML5, it depends on the effect and semantic meaning you are going for. Putting the entire header inside the link will cause the clickable area to span the entire width, whereas putting the link inside the header will cause only the text to be clickable. I've used both versions before, but it completely depends on where I'm putting the header.

在 HTML5 中,这取决于您想要的效果和语义。将整个标题放在链接内将导致可点击区域跨越整个宽度,而将链接放在标题内将导致只有文本可点击。我以前使用过这两个版本,但这完全取决于我将标题放在哪里。

回答by Cleverbot

They both work just fine.

他们都工作得很好。

You can check for yourself at http://validator.w3.org/if you like.

如果您愿意,您可以在http://validator.w3.org/ 上自行检查。

回答by MCSI

I prefer the second one :

我更喜欢第二个:

<a href=""><h2>Some Title</h2></a>

because you have more control for example if you want to do something like this:

因为你有更多的控制权,例如,如果你想做这样的事情:

<a href=""><h2>Some Title</h2> <h1>biggest</h1></a>

回答by pete

Technically speaking, <a>is an inline-element, and <h2>is a block-level element, so:

从技术上讲,<a>是行内元素,并且<h2>是块级元素,因此:

<h2><a href="">Some Title</a></h2>

is the correct nesting.

是正确的嵌套。

That said, the browser will generallyignore incorrect nesting like that and allow you to do it the way you want to.

也就是说,浏览器通常会忽略不正确的嵌套,并允许您按照自己的方式进行。

Me, I don't like tempting fate when it comes to a browser choosing to run my page in Standards-Compliance mode or Quirks mode so I'll choose the correct nesting.

我,当浏览器选择在标准合规模式或 Quirks 模式下运行我的页面时,我不喜欢诱人的命运,所以我会选择正确的嵌套。

回答by OscarSan

<h1><a href="#">text here</a></h1>

is correct, as HTML does not allow a block element () within an inline element () (src). your second example will fail validation.

是正确的,因为 HTML 不允许在内联元素 () (src) 中包含块元素 ()。您的第二个示例将无法通过验证。

Generally, block-level elements may contain inline elements and other block-level elements. Generally, inline elements may contain only data and other inline elements. Inherent in this structural distinction is the idea that block elements create "larger" structures than inline elements.

通常,块级元素可能包含行内元素和其他块级元素。通常,内联元素可能只包含数据和其他内联元素。这种结构区别的固有思想是块元素创建比行内元素“更大”的结构。

I just copied this from another question ...

我刚刚从另一个问题复制了这个...