如何在博客中正确使用HTML语义元素?
How to properly use the HTML semantic elements in a blog?
我阅读了很多关于w3schools的信息。它将 <section>
定义为:
The <section> element defines a section in a document. [...] A home page could normally be split into sections for introduction, content, and contact information.
所以,基本上,<section>
可以是 <header>
(介绍)和 <footer>
(联系信息)?
The <header> element specifies a header for a document or section.
所以我可以在里面放一个<header>
一个<section>
?但是一个<section>
可以用"for introduction",所以基本上是一个header? <footer>
元素也是如此。
问题
我应该如何使用这些元素?
我应该使用 <header>
作为 <article>
的标题还是我网站中包含徽标和导航的部分?
我应该将 <footer>
用于 <article>
的数据还是我网站中包含版权和页脚导航的部分?
我应该在 我的 <article>
标签周围放置一个 <section>
标签吗?
假设我有一个简单的网站,包含徽标、导航、几篇文章和一些页脚文本。做标记的最佳方式是什么?
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
</head>
<body>
<header>
<img src="logo.png" alt="My logo!">
<nav>
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</nav>
</header>
<section>
<article>
<header><h2>Article 1 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
<article>
<header><h2>Article 2 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
<article>
<header><h2>Article 3 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
</section>
<footer>
<p>Copyright</p>
</footer>
</body>
</html>
如果我这样做,Nu HTML Checker 会说:
Warning: Section lacks heading. Consider using h2-h6 elements to add identifying headings to all sections.
我不想 <h1>
声明 <section>
包含 <article>
标签。我应该删除 <section>
吗?貌似是个灵活的标签,但我找不到合适的地方放它。
正确的标记是什么?
A header 与标题不一样。您使用 标记作为 header(页眉),使用 到 作为标题(标题、副标题等)。
这就是 header 和标题之间的区别。除此之外,您的标记似乎足够合乎逻辑,除了 header-heading 问题,我不会去使用
HTML5 语义非常合乎逻辑 - 、
但是 可以是 的一部分。一个部分也可以是一篇文章的一部分。所以我的观点是,如果你遵循 HTML 标签的逻辑,你就不必担心语义。
哦,回答你的问题。我会这样做:
<section>
<article>
<h2>Article 1 title</h2>
<p>Posted on Foo.Bar.2017</p>
</article>
<article>
<h2>Article 2 title</h2>
<p>Posted on Foo.Bar.2017</p>
</article>
</section>
如果需要,您可以将
header
/ footer
有 sectioning content and sectioning root 个元素。 header
/footer
元素始终 "belong" 到这些分段 (content/root) 元素之一。
<body>
<header>belongs to the 'body'</header>
<article>
<header>belongs to the 'article'</header>
<footer>belongs to the 'article'</footer>
</article>
<div>
<header>belongs to the 'body'</header>
<footer>belongs to the 'body'</footer>
</div>
<footer>belongs to the 'body'</footer>
</body>
请注意,div
元素不是分段元素,这就是为什么它的 header
/footer
children 属于 body
(这是一个分段根元素),而不是 div
.
因此,您可以将 header
/footer
元素用于 site-wide 内容,方法是放置它们,使它们最近的分段 parent 元素是 body
元素.
section
一个 header
/footer
可以由一个或多个 section
元素组成,但这通常只有在 header
/footer
是(异常)复杂。
一般来说,什么时候使用 section
有意义?如果您有一个隐含的 部分 ,或者您需要在文档大纲中添加一个条目。请看我的相关回答:
- how to decide which sectioning content element to use(包含三个博客标记示例)
section
in article
vs. article
in section
(在博客上下文中)
- a rule of thumb when to use
section
请注意,当分节内容元素没有标题元素时,验证器会报告 a warning, not an error。它们不需要有标题,但它可能是分段内容元素被滥用的标志:通常只有在它可以有标题的情况下才有意义,即使你实际上没有提供标题。
我阅读了很多关于w3schools的信息。它将 <section>
定义为:
The <section> element defines a section in a document. [...] A home page could normally be split into sections for introduction, content, and contact information.
所以,基本上,<section>
可以是 <header>
(介绍)和 <footer>
(联系信息)?
The <header> element specifies a header for a document or section.
所以我可以在里面放一个<header>
一个<section>
?但是一个<section>
可以用"for introduction",所以基本上是一个header? <footer>
元素也是如此。
问题
我应该如何使用这些元素?
我应该使用
<header>
作为<article>
的标题还是我网站中包含徽标和导航的部分?我应该将
<footer>
用于<article>
的数据还是我网站中包含版权和页脚导航的部分?我应该在 我的
<article>
标签周围放置一个<section>
标签吗?
假设我有一个简单的网站,包含徽标、导航、几篇文章和一些页脚文本。做标记的最佳方式是什么?
<!DOCTYPE html>
<html>
<head>
<title>My site</title>
</head>
<body>
<header>
<img src="logo.png" alt="My logo!">
<nav>
<ul>
<li><a href="#">Item 1</a></li>
<li><a href="#">Item 2</a></li>
<li><a href="#">Item 3</a></li>
</ul>
</nav>
</header>
<section>
<article>
<header><h2>Article 1 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
<article>
<header><h2>Article 2 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
<article>
<header><h2>Article 3 title</h2></header>
<footer>Posted on Foo.Bar.2017</footer>
</article>
</section>
<footer>
<p>Copyright</p>
</footer>
</body>
</html>
如果我这样做,Nu HTML Checker 会说:
Warning: Section lacks heading. Consider using h2-h6 elements to add identifying headings to all sections.
我不想 <h1>
声明 <section>
包含 <article>
标签。我应该删除 <section>
吗?貌似是个灵活的标签,但我找不到合适的地方放它。
正确的标记是什么?
A header 与标题不一样。您使用 到
作为标题(标题、副标题等)。
这就是 header 和标题之间的区别。除此之外,您的标记似乎足够合乎逻辑,除了 header-heading 问题,我不会去使用
HTML5 语义非常合乎逻辑 -
但是
哦,回答你的问题。我会这样做:
<section>
<article>
<h2>Article 1 title</h2>
<p>Posted on Foo.Bar.2017</p>
</article>
<article>
<h2>Article 2 title</h2>
<p>Posted on Foo.Bar.2017</p>
</article>
</section>
如果需要,您可以将
header
/ footer
有 sectioning content and sectioning root 个元素。 header
/footer
元素始终 "belong" 到这些分段 (content/root) 元素之一。
<body>
<header>belongs to the 'body'</header>
<article>
<header>belongs to the 'article'</header>
<footer>belongs to the 'article'</footer>
</article>
<div>
<header>belongs to the 'body'</header>
<footer>belongs to the 'body'</footer>
</div>
<footer>belongs to the 'body'</footer>
</body>
请注意,div
元素不是分段元素,这就是为什么它的 header
/footer
children 属于 body
(这是一个分段根元素),而不是 div
.
因此,您可以将 header
/footer
元素用于 site-wide 内容,方法是放置它们,使它们最近的分段 parent 元素是 body
元素.
section
一个 header
/footer
可以由一个或多个 section
元素组成,但这通常只有在 header
/footer
是(异常)复杂。
一般来说,什么时候使用 section
有意义?如果您有一个隐含的 部分 ,或者您需要在文档大纲中添加一个条目。请看我的相关回答:
- how to decide which sectioning content element to use(包含三个博客标记示例)
section
inarticle
vs.article
insection
(在博客上下文中)- a rule of thumb when to use
section
请注意,当分节内容元素没有标题元素时,验证器会报告 a warning, not an error。它们不需要有标题,但它可能是分段内容元素被滥用的标志:通常只有在它可以有标题的情况下才有意义,即使你实际上没有提供标题。