Html <li> 中允许使用哪些标签?

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

What are the allowed tags inside a <li>?

htmlhtml-lists

提问by petsagouris

I have been searching for the list of tags that are available inside a <li>, but I couldn't find any reference.

我一直在搜索 a 中可用的标签列表<li>,但找不到任何参考。

Is it possible that any standards-compliant HTML 4+ block element is allowed in them?

是否有可能在其中允许任何符合标准的 HTML 4+ 块元素?

回答by RoToRa

TL;DR: an <li>can contain any element that is valid in <body>.

TL;DR: an<li>可以包含任何在<body>.

In the HTML 4.01 spec for listsyou'll find the relevant extract of the DTD:

列表HTML 4.01 规范中,您将找到 DTD 的相关摘录:

<!ELEMENT LI - O (%flow;)* -- list item -->

This specifies that an <li>may contain flow content, which is the collection of all blockand inlineelements.

这指定了一个<li>可能包含流内容,它是所有内联元素的集合。

The HTML5 spec for an <li>is the same in that it also allows any flow content.

HTML5 规范<li>是相同的,因为它也允许任何流内容

回答by Scott Brown

It's a block level element so pretty much anything goes. Trouble only comes when you're putting block level elements inside inline ones.

这是一个块级元素,所以几乎任何事情都会发生。只有当您将块级元素放入内联元素时才会出现问题。

回答by Nathan MacInnes

Yup, pretty much. You can have lists inside lists (either inside the <li>or just loose inside the <ol>/<ul>the inner list must be inside an <li>), block elements and inline elements.

是的,差不多。您可以在列表中包含列表(在 the 内部<li>或只是松散在<ol>/<ul>内部列表必须在 an 内部<li>)、块元素和内联元素。

To me it wouldn't make much sense to put a <table>inside an <li>, but even that's still valid.

对我来说,将 a<table>放入 an没有多大意义<li>,但即使这样仍然有效。

回答by Sarfraz

You can use the W3C's Markup Validation Serviceto test against your cases to know whether or not your markup is valid.

您可以使用W3C 的标记验证服务针对您的案例进行测试,以了解您的标记是否有效。

This validator checks the markup validity of Web documents in HTML, XHTML, SMIL, MathML, etc.

此验证器检查 HTML、XHTML、SMIL、MathML 等格式的 Web 文档的标记有效性。