Html 我可以在列表项中使用 div 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6449772/
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
Can I use a div inside a list item?
提问by Vimal Basdeo
Why is the following code valid when I am using a <div>
inside a <li>
?
为什么当我<div>
在 a 中使用 a 时以下代码有效<li>
?
<ul>
<li class="aschild">
<div class="nav">Test</div>
</li>
</ul>
回答by Jawad
Yes you can use a div inside a li and it will validate.
是的,您可以在 li 中使用 div,它会进行验证。
<!ELEMENT li %Flow;>
<!ENTITY % Flow "(#PCDATA | %block; | form | %inline; | %misc;)*">
<!ENTITY % block "p | %heading; | div | %lists; | %blocktext; | fieldset | table">
回答by kapa
Inside a <li>
you can have anything you could naturally put inside a <div>
. They are no different in this sense.
在 a 中,<li>
您可以拥有可以自然放入 a 中的任何内容<div>
。从这个意义上说,它们没有什么不同。
It should be valid in HTML4, XHTML and HTML5 as well.
它也应该在 HTML4、XHTML 和 HTML5 中有效。
This is NOTvalid though (so the sources you found about "no divs in lists" could refer to this situation):
这是不是有效的,但(这样你就“在列表中没有申报单”发现有关人士可以参考这种情况):
<ul>
<li></li>
<div></div>
<li></li>
</ul>
So:Lists (ul
, ol
) can only have li
s as their children. But li
s can have anything as their children.
所以:Lists ( ul
, ol
) 只能有li
s 作为他们的孩子。但是li
s 可以拥有任何东西作为他们的孩子。
回答by twsaef
Because <li>
is a block element, not an inline element like <span>
or <a>
.
因为<li>
是块元素,而不是像<span>
or那样的内联元素<a>
。
回答by Adam
An <li>
is a block element, and will work perfectly fine with other block elements inside.
An<li>
是一个块元素,并且可以与内部的其他块元素完美配合。
回答by Jose Faeti
Yes, you can. As much as you want.
是的你可以。随心所欲。