HTML5 `figure` 和 `aside` 的正确用法

发布于 2024-09-25 03:18:54 字数 2154 浏览 2 评论 0 原文

我有一个页面的一大块,从语义上看是这样的:

标题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>

哪种方法在语义上是正确的?

I have a chunk of a page that looks semantically this:

Heading A

Textual information related to heading A.
Blah blah blah blah blah.

[image gallery related to heading A]

I can think of a couple of ways to mark this up:

Method 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>

Method 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>

Method 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>

Method 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>

Which is the semantically correct way of doing it?

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

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

发布评论

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

评论(2

一梦浮鱼 2024-10-02 03:18:54

您不太可能就标记此内容的“唯一正确方法”达成完全一致,但我会选择方法 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:

  • A figure element cannot have multiple figcaption children, so method 1 is invalid
  • A figure is already a type of aside, 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)
  • Unless the two figures are themselves related somehow, other than both being related to the section they're in, no need to group them further than they are already
红墙和绿瓦 2024-10-02 03:18:54

在这种情况下,将

插入到

  • 内容本身有意义 可以移动到页面的不同部分,甚至移动到单独的页面(例如附录),但传达重要信息。如果是这样,请选择
    。 // 示例有:插图、图表、代码块、图表、音频/视频等(主要是图形内容),所有这些都伴随着文档的主要主题,并且通常作为单个单元引用。
  • 内容并不是理解 >它所在的部分可能会被完全牺牲,或者不会对该部分的主题做出贡献。此外,内容只有在周围的上下文中才有意义。如果是这样,请选择

考虑到这一点,我也会接受方法2

  • 方法1已经正确被@robertc排除
  • 方法3a被我排除了(您的示例不满足上述
  • 方法 3b – 只要
    没有额外的 (

    ) 标题,就无需嵌套在另一个

    中代码><图>。

  • 方法 4 也被我排除在外 - 与方法 3 相同的论点加上
    不应放置在与相关文本不同的部分中,并且标题是。

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:

  • The content make sense on its own and could be moved to a different part of the page or even to a separate page (e.g appendix) but conveys important information. If so, choose <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.
  • The content is not essential to understand the section in which it is placed, could be completely sacrificed or doesn't contribute to the main topic of that section. Also, the content makes sense only within the surrounding context. If so, choose <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:

  • Method 1 has been correctly excluded by @robertc.
  • Method 3a is excluded by me (your example doesn't meet the requirements for <aside> stated above, i.e. there is no reason to wrap the <figure>s with <aside>).
  • Method 3b – no need for nesting in another <section> as long as there is no additional (<h2>) heading for <figure>s.
  • Method 4 is also excluded by me – the same argument as for Method 3 plus that the <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.

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