Html 如何在 Web 标准中混合链接(<a> 标签)和标题(<h1> 标签)?

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

how to mix links ( <a> tag ) and headings ( <h1> tag ) in web standard?

htmlweb-standards

提问by ahmed

What is the correct code to create a link with heading 1 according to web standards?

根据网络标准创建标题为 1 的链接的正确代码是什么?

is it

是吗

<h1><a href="http://stackoverflow.com"> stackoverflow </a></h1>

or

或者

<a href="http://stackoverflow.com"><h1> stackoverflow </h1></a>

Thanks

谢谢

回答by Darko Z

According to web standards you aren't allowed to put block elements into inline elements.

根据 Web 标准,不允许将块元素放入内联元素中。

As h1is a block element and ais an inline element the correct way is:

由于h1是块元素并且a是内联元素,正确的方法是:

<h1><a href="#">This is a title</a></h1>

Here is a link so you can learn more: w3 Visual formatting model

这是一个链接,您可以了解更多信息:w3 可视化格式模型

However, there is an exception that in HTML5 it is valid to wrap block-level elements (like div, por h*) in anchor tags. Wrapping block-level elements in inline elements other than anchors still goes against the standards.

但是,有一个例外,即在 HTML5 中,将块级元素(如divph*)包装在锚标记中是有效的。将块级元素包装在锚点以外的内联元素中仍然违反标准。

回答by f055

HTML5 updates this subject: it is now OK to wrap block-level elements with A's, as stated under another question: https://stackoverflow.com/a/9782054/674965and here: http://davidwalsh.name/html5-elements-links

HTML5 更新了这个主题:现在可以用 A 包装块级元素,如另一个问题所述:https: //stackoverflow.com/a/9782054/674965和这里:http: //davidwalsh.name/html5-元素链接

回答by Kip Deeds

As far as I understand HTML5 does allow you to wrap block level elements in link tags. However, bugs may show up in older browsers. I encountered this with Firefox 3.6.18 and got moz-rs-heading="" inserted into my code. Thus my styles broke. If you care about a work around, you can then wrap the link tags in divs. The following provides a better explanation of why the additional code works http://oli.jp/2009/html5-block-level-links/

据我了解,HTML5 确实允许您将块级元素包装在链接标签中。但是,错误可能会出现在较旧的浏览器中。我在 Firefox 3.6.18 中遇到了这个问题,并将 moz-rs-heading="" 插入到我的代码中。因此我的风格打破了。如果您关心解决方法,则可以将链接标签包装在 div 中。下面提供了一个更好的解释为什么额外的代码工作http://oli.jp/2009/html5-block-level-links/