HTML5 `figure` 和 `aside` 的正确用法
我有一个页面的一大块,从语义上看是这样的:
标题A
与标题 A 相关的文本信息。
等等等等等等。[与标题 A 相关的图片库]
我可以想出几种标记方法:
方法 1:
<section>
<h1>Heading A</h1>
<p>Textual information related to heading A.<br />
<p>Blah blah blah blah blah.</p>
<figure>
<img />
<figcaption>Image 1</figcaption>
<img />
<figcaption>Image 2</figcaption>
</figure>
</section>
方法 2:
<section>
<h1>Heading A</h1>
<p>Textual information related to heading A.<br />
<p>Blah blah blah blah blah.</p>
<figure>
<img />
<figcaption>Image 1</figcaption>
<figure>
</figure>
<img />
<figcaption>Image 2</figcaption>
</figure>
</section>
方法 3:
<section>
<h1>Heading A</h1>
<p>Textual information related to heading A.<br />
<p>Blah blah blah blah blah.</p>
<aside> <!--Method 3b: use section here-->
<figure>
<img />
<figcaption>Image 1</figcaption>
<figure>
</figure>
<img />
<figcaption>Image 2</figcaption>
</figure>
</aside> <!--Method 3b: use section here-->
</section>
方法 4:
<section>
<h1>Heading A</h1>
<p>Textual information related to heading A.<br />
<p>Blah blah blah blah blah.</p>
</section>
<aside>
<figure>
<img />
<figcaption>Image 1</figcaption>
<figure>
</figure>
<img />
<figcaption>Image 2</figcaption>
</figure>
</aside>
哪种方法在语义上是正确的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不太可能就标记此内容的“唯一正确方法”达成完全一致,但我会选择方法 2。我的理由是:
figure
元素不能有多个Figcaption
子元素,因此方法 1 无效figure
已经是aside
的类型,因此无需包装它们:“figure 元素代表一个单元内容……可以从文档的主要流程中移出,而不会影响文档的含义。” (W3C HTML5 规范,figure 元素)It's unlikely that you're going to get complete agreement on a 'one true way' to mark this up, but I would choose method 2. My reasoning is:
figure
element cannot have multiplefigcaption
children, so method 1 is invalidfigure
is already a type ofaside
, so no need to wrap them: "The figure element represents a unit of content ... that can be moved away from the main flow of the document without affecting the document’s meaning." (W3C HTML5 spec, figure element)在这种情况下,将
。 // 示例包括:引述、补充上下文相关信息、印刷版式中的侧边栏、广告等(主要是文本内容)。我可以很好地想象 Google AdSense。
考虑到这一点,我也会接受方法2:
的要求,即没有理由将
<; figure>
与)。
没有额外的 (
) 标题,就无需嵌套在另一个
中代码><图>。
从 W3C 规范。理想情况下,页面还应该包含一些与主要主题相关的段落文本,然后
Inserting
<figure>
into<aside>
does not make much sense in this case. A rule of thumb for choosing between<figure>
and<aside>
is to differentiate whether:<figure>
. // Examples are: illustrations, diagrams, blocks of code, graphs, audio/video etc. (mostly graphical content), all of which are accompanying the main topic of the document and are often referenced as a single unit.<aside>
. // Examples are: pull quotes, complementary context related information, sidebars as in printed typography, ads, etc. (mostly textual content). I can well imagine Google AdSense.Having this in mind, I would accept the Method 2 too:
<aside>
stated above, i.e. there is no reason to wrap the<figure>
s with<aside>
).<section>
as long as there is no additional (<h2>
) heading for<figure>
s.<figure>
s should not be placed in a different section than the related text and heading are.Wheter the
<figure>
should be used within galleries is not obvious from the W3C Specification. Ideally, a page should also contain some paragraph text related to the main topic, then the<figure>
s should be referred from that text and be "depictive" only, not the whole main content itself. On the other hand, such an usage is not in any way contrary to the specification as gallery items usually are "self-contained" (not bound with the context) – in particular when enriched with<figcaption>
so their meaning is clear. I can't imagine better element for this usage.