Can you put li elements inside a div

2019-08-13 03:31发布

问题:

Ok, so I have looked around w3c and blogs and have not found an argument as to why I cannot do the following html:

<ul>
    <div><li>1</li></div>
    <div><li>2</li></div>
    <div><li>3</li></div>
</ul>

I know intuitively this doesn't look right but I can't find a reference that says you can't do it.

回答1:

If you search for "HTML Validator" you can quickly run some code through one to see if it is valid or not. Div tags aren't valid as first level child elements of a ul tag. Whatever you are trying to achieve can be accomplished by styling the li tags with CSS.



回答2:

No. This is invalid HTML.

Permittend content: zero or more <li> elements, eventually mixed with <ol> and <ul> elements.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/ul