我应该使用 <section><aside> 内的标签?

2022-01-18 00:00:00 tags semantics html semantic-markup

有这样的标记:

<aside>
    <div class="widget">
        <h2>Latest news</h2>
        <ul>...</ul>
        <a>more news</a>
    </div>
    <div class="widget">
        <h2>Choose site theme</h2>
        <input type="select" />
    </div>
    <div class="widget">
        <h2>Newsletter subscription</h2>
        <form>...</form>
    </div>
    <div class="widget">
        <h2>Related articles</h2>
        <ul>...</ul>
    </div>
</aside>

这里哪个标签更合适:<div>还是<section>?
部分是否应该只在 <article> 标记内使用,而不能在 <aside> 内使用?

Which tag is more appropriate here: <div> or <section>?
Is section supposed to be used only inside <article> tag and never inside <aside>?

推荐答案

HTML 5 规范不禁止您在 aside 元素内使用 section 元素.aside 元素允许在里面有流内容,并且包括 section 元素.

The HTML 5 spec does not prohibit you from using the section element inside of an aside element. The aside element is allowed to have flow content inside, and that includes the section element.

然而,即使 div 元素现在在 HTML5 中被认为是最后的手段"的分段元素,在这种情况下使用 section 与元素的意图.从我们看到的规范来看:

However, even though the div element is now, in HTML5, supposed to be the sectioning element of "last resort", using section in this context goes against the intent of the element. From the spec we see:

注意:section 元素不是通用容器元素.当一个元素仅用于样式目的或为了方便编写脚本时,鼓励作者使用 div 元素.一般规则是,仅当元素的内容将在文档的大纲中明确列出时,section 元素才是合适的.

Note: The section element is not a generic container element. When an element is needed only for styling purposes or as a convenience for scripting, authors are encouraged to use the div element instead. A general rule is that the section element is appropriate only if the element's contents would be listed explicitly in the document's outline.

现在,旁白的小部分"绝对不属于整个文档的大纲,所以对您的问题的简短回答是使用 div.或者,因为你的 side 看起来里面有四个项目",你可以考虑一个带有四个 liul,然后使用规则 aside ul li 设置样式.

Now, little "sections" of an aside are definitely not something that belong in the outline of the entire document, so the short answer to your question is to use div. Alternatively, because your aside looks like it has four "items" inside, you might consider a ul with four lis and then style with the rule aside ul li.

相关文章