Html 在 HTML5 中 span 标签可以包含哪些元素?

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

What elements can a span tag contain in HTML5?

htmlw3c-validationweb-standards

提问by Ghost

So I have been a CLI or Cron developer in PHP for most my career and now I am learning why I did not like UI work in school ;) I am dealing with HTML5 validation here and I am not finding an answer to a question that is for curosity. I ran the W3C HTML5 validation on my home page and I got several errors around a div being within a <span>and that is not allowed. I tried changing all <div>within spans to a <p>but recieve pretty much the same error. http://www.w3schools.com/html5/tag_span.aspdoes not explicitly say what elements are allowed within a <span>and neither does the error from the validator:

因此,在我的大部分职业生涯中,我一直是 PHP 的 CLI 或 Cron 开发人员,现在我正在了解为什么我不喜欢学校的 UI 工作;) 我在这里处理 HTML5 验证,但我没有找到问题的答案出于好奇。我在我的主页上运行了 W3C HTML5 验证,我在 div 中遇到了几个错误<span>,这是不允许的。我尝试将所有<div>跨度更改为 a<p>但收到几乎相同的错误。 http://www.w3schools.com/html5/tag_span.asp没有明确说明 a 中允许哪些元素<span>,验证器的错误也没有:

Line 85, Column 69: Element p not allowed as child of element span in this context. (Suppressing further errors from this subtree.)

第 85 行,第 69 列:在此上下文中,不允许元素 p 作为元素跨度的子元素。(抑制来自此子树的更多错误。)

2 questions:

2个问题:

1) What elements if any are allowed within a <span>?

1) 在 a 中允许哪些元素(如果有)<span>

2) Where can I reference for what elements are allowed within another? I have googled but see nothing that has a "W3C" stamp on it.

2)我在哪里可以参考另一个元素中允许的元素?我用谷歌搜索但没有看到任何带有“W3C”标记的东西。

回答by Hymanwanders

Only inline elements may be contained within inline elements. spanis an inline element. So, tags like a, img, sup, etc. can go within a span, but block level elements like divand pcannot.

内联元素只能包含内联元素。span是一个内联元素。所以,像aimgsup等标签可以在一个跨度内,但块级元素像divp不能。

UPDATE

更新

In reality, different elements which default to inline display behave differently. Some "inline" elements may allow block elements (afor example), while others don't (like span).

实际上,默认为内联显示的不同元素的行为不同。一些“内联”元素可能允许块元素(a例如),而另一些则不允许(例如span)。

If you're interested in what an HTML tag may contain, your most official source is the WHATWG page on HTML elements. You can check any HTML element and see what content is permitted (see 'Content Model' for each element):

如果您对 HTML 标记可能包含的内容感兴趣,那么最官方的来源是 HTML 元素的 WHATWG 页面。您可以检查任何 HTML 元素并查看允许的内容(请参阅每个元素的“内容模型”):

http://www.whatwg.org/specs/web-apps/current-work/multipage/#auto-toc-4

http://www.whatwg.org/specs/web-apps/current-work/multipage/#auto-toc-4

Here's the definition of the span tag, which indicates that only 'phrasing' content is allowed.

这是 span 标签的定义,它表示只允许“短语”内容。

http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-span-element

http://www.whatwg.org/specs/web-apps/current-work/multipage/text-level-semantics.html#the-span-element