html - UL 在 LI 下,尽管有 block 和内联

标签 html css html-lists block inline

我的想法是在内联下放置一个 block 是不正确的。但是,在构建树结构时,我们在 li 下使用 ul。谁能解释为什么这是一个有效的 html?

最佳答案

li 属性的默认display 值不是inline,而是list-itemul 被设计为嵌套在列表中,因此将 ul 放在 li 中没有问题。

此外,从 HTML5 开始,将 block 元素放在内联元素中是有效的 HTML。

来自 MDN :

The distinction of block-level vs. inline elements is used in HTML specifications up to 4.01. In HTML5, this binary distinction is replaced with a more complex set of content categories. The "block-level" category roughly corresponds to the category of flow content in HTML5, while "inline" corresponds to phrasing content, but there are additional categories.

但是,如果您不想将 block 元素放入 rendering 的行内元素中或其他原因,您可以选择更改 display 值:

<span style="display: block">
    <div style="display: inline"> ... </div>
</span> 

关于html - UL 在 LI 下,尽管有 block 和内联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31591790/

相关文章:

javascript - 在本地主机上查看页面时 jQuery 脚本不起作用

javascript - 打字时获取输入文本宽度

css - li 标签边框底部太长

javascript - 将库 (Plyr) 与指令和 AngularJS 一起使用

html - 如何在 d3 中使用 css 设置 if-else 语句的样式

javascript - jQuery - 单击列表时替换 div 类

css - 背景大小百分比在 Firefox 和 Webkit 中显示不同

javascript - 如何在随幻灯片放映而变化的 div 中添加 div

html - 列表中的有效列表 - ul 中的 ul

html - 通常在 li 内格式化文本(列表项)