为什么 HTML 中的块级元素和内联元素之间存在功能差异?
注意:我不是在谈论显示 - 我理解为什么它们有不同的默认值。我正在谈论块和内联元素之间的功能差异,尤其是。关于嵌套之类的事情,
我一次又一次地被告知 HTML 是关于描述内容、事物的作用和事物是什么,而不是它们的外观(除了与标签关联的默认样式元素之外,可以是改变)。
因此,这似乎是“块”级标签和“内联”级标签之间的划分(不是这些标签的显示,而是这些标签的固有差异),这会让很多新手感到困惑和复杂化。即使是更有经验的页面设计者,也是任意的并且......似乎违反原则,如果你愿意的话。这种行为似乎应该通过风格来控制,而不是作为块的内置元素存在。在某些方面,它是在风格上进行控制的 - 我们都知道 CSS 可以使 span 看起来像 div,反之亦然。
那么块和内联之间的功能区别有什么意义呢?为什么它们不像其他嵌套元素一样?为什么不是所有带有“内联”元素的“块”都简单地设置为“display:inline”?它只是为了向后兼容,还是这种看似任意的划分有我没有看到的好处?
请注意,这可能作为社区维基问题更好,因为它不太可能有一个明确的答案,但我不太确定细节。
举个例子,据我了解,以下内容是无效的 html:
<label style="display:block">
<h4>Name</h4>
<span class='sub'>Whether legal, pseudonymous, or made up</span>
<input id="name"></input>
</label>
为什么?
Note: I am NOT talking about the display - I understand why they have different defaults. I'm talking about the functional differences between blocks and inline elements, esp. in regards to things like nesting
I've been told time and again that HTML is about describing content, what things do and what things are, not what they look like (beyond default stylistic elements associated with tags, that can be changed).
As such, it seems like the division between "block"-level tags and "inline"-level tags (not the display of those tags, but the inherent difference in those tags), something that trips up and complicated a great many neophyte and even more experience page designers, is arbitrary and... seemingly against principle, if you would. This sort of behaviour seems like something that should by controlled stylistically, not exist as a built-in element of a block. And in some ways, it IS controlled stylistically - we all know there is CSS that can make a span look like a div, and vice-versa.
So what is the point of having the functional distinction between blocks and inlines? Why aren't they just like any other nested element? Why isn't everything a "block" with "inline" elements simply set to "display: inline"? Is it just for, say, backwards compatibility, or is there a benefit to this seemingly arbitrary division that I'm just not seeing?
Note, this might be better as a community wiki question, since it's unlikely to have a single clear answer, but I'm not exactly sure on the details.
As an example, from what I understand, the following would be invalid html:
<label style="display:block">
<h4>Name</h4>
<span class='sub'>Whether legal, pseudonymous, or made up</span>
<input id="name"></input>
</label>
Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,几乎是这样;只是相反。相反,一切都是“内联”的,“块”元素设置为“display:block”。
但这真的很烦人,如果对于您创建的每个页面,您都需要告诉它
div
标签应被视为块元素。因此,出于这个原因,假定了默认样式,最好在 http://www.w3 中进行说明。 org/TR/CSS2/sample.htmlWell, it almost kind of is; just the inverse. Rather, everything is "inline" with "block" elements set to "display: block".
But that would be really annoying, if for every page you create, you need to tell it that
div
tags should be treated as block elements. So for that reason, default styles are assumed, best illustrated at http://www.w3.org/TR/CSS2/sample.html