在幕后,HTML5 标签(文章、部分、旁白、页眉、页脚)是否像 div 一样实现?

发布于 2024-12-22 22:14:49 字数 231 浏览 6 评论 0原文

我知道所有这些标签都有不同的语义含义,例如 article 代表一个独立的故事; section 用于页面的独立部分...

它们在很多方面都是良好且有用的标签。但是,它们只是语法糖吗?因为我没有看到他们之间有任何风格差异。当不应用任何 CSS 规则时,它们就像 div 一样。

希望一些webkit/gecko专家能在代码层面上澄清一下。

I understand all these tags have different semantic meanings, like article is for a story that stand apart; section is for self-contained part of the page...

They are good and useful tags, in many ways. But, are they just syntax sugar? Because I didn't see any style differences among them. When not applied by any CSS rules, they are just like divs.

Hope some webkit/gecko experts can clarify it on the code level.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞慾 2024-12-29 22:14:49

大多数情况下是的。它们实际上并不是 div,而是块级元素(微妙但不同)。

http://diveintohtml5.info/semantics.html#new-elements

HTML5 中定义了几个新元素,它们是块级元素。也就是说,它们可以包含其他块级元素,并且兼容 HTML5 的浏览器将默认将它们设置为 display:block 样式。

所以是的,目前它们只是为了缩短代码长度并创建更清晰的标记,但这就是块级元素的作用。如果从

标记中删除默认样式,它是不是就像

一样?

Mostly yes. They're not actually divs, but block level elements (subtle but different).

http://diveintohtml5.info/semantics.html#new-elements

There are several new elements defined in HTML5 which are block-level elements. That is, they can contain other block-level elements, and HTML5-compliant browsers will style them as display:block by default.

So yes, for now they are just there for shortening your code length and creating cleaner markup, but that's what block level elements do. If you remove the default styling from a <p> tag isn't it just like a <div>?

白芷 2024-12-29 22:14:49

我没有发现它们之间有任何风格差异。当不应用任何 CSS 规则时,它们就像 div 一样。

每个浏览器都有自己的应用于页面内容的本机样式表。这就是我们通常使用 CSS 重置样式表的原因 - 标准化不同浏览器样式表之间的所有内容。样式和内容是两个完全独立的事物,看起来像与它是什么无关,因此您不应该根据您想要的方式标记页面出现。

有一天,浏览器向某些 HTML5 元素添加默认样式可能很常见,例如

上的一些填充,但这并不重要(实际上不太可能)。

它们只是语法糖吗?

一点也不。虽然看起来可能没有太大区别,但它允许屏幕阅读器或搜索引擎以更有意义的方式理解您的内容。

正如 Grillz 的回答提到的,所讨论的元素都是块级元素,因此在这方面 - 它们基本上呈现为 < code>div,但具有语义值,并不意味着用作通用容器元素。

I didn't see any style differences among them. When not applied by any CSS rules, they are just like divs.

Each browser has it's own native stylesheet that is applied to the page content. This is why we commonly use CSS reset stylesheets - to normalize everything between different browser stylesheets. Style and content are two completely separate things, what something looks like has no bearing on what it is, therefore you should not be marking up your page based on how you want it to appear.

Some day, it may be common for browsers to add default style to certain HTML5 elements, like some padding on <section>s, but it doesn't matter (and is actually unlikely).

Are they just syntax sugar?

Not at all. While there may not seem to be much of a difference, it allows your content to be understood by, for example, screen readers or search engines, in a more meaningful way.

As Grillz's answer mentions, the elements in question are all block level elements, so in that respect - they are basically rendered like divs, but have semantic value and aren't meant to be used as generic container elements.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文