Html 我可以使用 div 作为 UL 的直接子代吗?

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

Can I use div as a direct child of UL?

htmlcss

提问by Rocky Singh

I'm having this code:

我有这个代码:

<ul>
   <div>
   </div>
</ul>

I feel no issue in my browser rendering it. I have read this too somewhere that lishould only be used as direct child of ul.

我觉得我的浏览器渲染它没有问题。我在某个地方也读过这篇文章,它li应该只用作ul.

Is this correct? Can't I use divas a direct child of UL? Is there any documentation for the above confusion?

这样对吗?我不能div用作 的直接子代UL吗?有没有关于上述混淆的文件?

Edit: This link says I can http://css-tricks.com/forums/discussion/11593/divs-inside-uls/p1

编辑:这个链接说我可以http://css-tricks.com/forums/discussion/11593/divs-inside-uls/p1

回答by Quentin

No. The only element that may be a child of <ul>is <li>.

不。唯一可能是 的子元素的元素<ul><li>

HTML 4:

HTML 4

<!ELEMENT UL - - (LI)+                 -- unordered list -->

(See also how to read a content model definition in a DTD)

(另请参阅如何读取 DTD 中的内容模型定义

HTML 5:

HTML 5

Content model: Zero or more li elements.

内容模型:零个或多个 li 元素。

回答by Shikiryu

For HTML 5 :

对于 HTML 5 :

http://www.w3.org/TR/html-markup/ul.html

http://www.w3.org/TR/html-markup/ul.html

Permitted contents

Zero or more li elements

允许内容

零个或多个 li 元素

For HTML 4 :

对于 HTML 4 :

http://www.w3.org/TR/html401/struct/lists.html#h-10.2

http://www.w3.org/TR/html401/struct/lists.html#h-10.2

<!ELEMENT UL - - (LI)+

EDIT :

编辑 :

I forget the other HTML5 :D (which have the same specification on this than the W3C's one)

我忘记了另一个 HTML5 :D(在这方面的规范与 W3C 的规范相同)

http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#the-ul-element

http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#the-ul-element

回答by RaBz

No the div cannot be nested inside the element, only < li> can be used as child element. instead of wrapping div inside ul element. you can do something like this

否 div 不能嵌套在元素内,只有 <li> 可以用作子元素。而不是将 div 包裹在 ul 元素中。你可以做这样的事情

<ul class="class1">
 <li class="child1">
   <ul>
     <li>item1</li>
     <li>item2</li>
     <li>item3</li>
   </ul>
 </li>
<ul> 

回答by jitendra varshney

you can use div tag under like this ul-> li -> divbecause you can only use li tag after the ul tag ,your code will be run fine but there would be validation error

您可以在这样的情况下使用 div 标签, ul-> li -> div因为您只能在 ul 标签后使用 li 标签,您的代码将运行良好但会出现验证错误

so you can use like this

所以你可以像这样使用

 <ul>
     <li>
         <div></div>
     </li>
 </ul>

回答by Andy

No. If you want valid markup a div should never be inside a , sorry. Some modern browsers will "autoclose" the ul tag before you open the div so watch out for that

不。如果你想要有效的标记, div 永远不应该在 a 内,抱歉。一些现代浏览器会在您打开 div 之前“自动关闭”ul 标签,因此请注意这一点

回答by Ahsan Khurshid

The HTML unordered list element <ul>represents an unordered list of items, namely a collection of items that do not have a numerical ordering, and their order in the list is meaningless. Typically, unordered-list items are displayed with a bullet, which can be of several forms, like a dot, a circle or a squared. The bullet style is not defined in the HTML description of the page, but in its associated CSS, using the list-style-type property.

HTML无序列表元素<ul>表示一个无序列表的项目,即没有数字排序的项目集合,它们在列表中的顺序是没有意义的。通常,无序列表项目用项目符号显示,项目符号可以有多种形式,如点、圆形或正方形。项目符号样式不是在页面的 HTML 描述中定义的,而是在其关联的 CSS 中使用 list-style-type 属性定义的。

Permitted content:

允许内容:

zero or more <li> elements