侧边栏的最佳HTML5标记

mrw*_*web 62 html5 semantic-markup

我正在为HTML5主题设置我的WordPress侧边栏,并且真的想要使用before_widgetafter_widget正确.

所以我的问题是:两种标记模式中哪一种更合适?以下代码完全在<article>元素之外.

选项1:除了部分

<aside id="sidebar">
    <section id="widget_1"></section>
    <section id="widget_2"></section>
    <section id="widget_3"></section>
</aside>
Run Code Online (Sandbox Code Playgroud)

选项2:Div with Asides

<div id="sidebar">
    <aside id="widget_1"></aside>
    <aside id="widget_1"></aside >
    <aside id="widget_1"></aside >
</div>
Run Code Online (Sandbox Code Playgroud)

我想辅助问题是每个小部件标题使用的标题.如果我将每个小部件包装在一个<section>那么<h1>似乎是最合适的.如果我使用<aside>,我不确定.

欢迎所有意见.魔鬼的拥护者鼓励.

aWe*_*per 56

首先,ASIDE仅用于表示主要内容的相关内容,而不是通用侧边栏.第二,仅限每个侧边栏一个

每个侧边栏只有一个.侧边栏的元素是div或部分在旁边.

我会选择选项1:除了部分

<aside id="sidebar">
    <section id="widget_1"></section>
    <section id="widget_2"></section>
    <section id="widget_3"></section>
</aside>
Run Code Online (Sandbox Code Playgroud)

这是规范https://developer.mozilla.org/en-US/docs/Web/HTML/Element/aside

再次使用部分,如果他们有一个页眉页脚,否则普通div

  • 我认为上面给出的解决方案过分依赖于旁边元素和#sidebar的视觉样式之间的等价.典型的WordPress侧边栏将包含blogroll,归档链接,可能是登录小部件.它们都与页面/站点相关,但只有在这个容量中它们彼此相关.它们可以在逻辑上放在许多不同的地方,但它们碰巧在一个侧边栏中组合在一起.所以我觉得@Waiting for Dev ...下面更接近了.如果这些小部件在视觉上组合在一起,请使用div. (3认同)

Dav*_*ury 25

两者都不正确.您应该可能正在使用DIV.<section>不得用于侧边栏,而是用于相关内容的一部分(例如术语词汇表或关于该主题的附加说明).

另见:http://html5doctor.com/understanding-aside/

更新:正如乔丹在下面指出的那样,本文已经更新,应该引用而不是我原来发布的那篇.和往常一样,如有疑问,请参阅规格!

http://html5doctor.com/aside-revisited/

更新17/07/27:由于这是投票最多的答案,我应该更新此选项以在本地包含当前信息(包含引用的链接).

从规范[1]:

aside元素表示页面的一部分,该部分由与父母分区内容的内容相切的内容组成,并且可以被认为与该内容分开.这些部分通常在印刷字体中表示为侧边栏.

大!正是我们正在寻找的东西.此外,最好也检查一下<article>.

section元素表示文档或应用程序的通用部分.在这种情况下,一节是内容的主题分组.应标识每个部分,通常通过将标题(h1-h6元素)包括为section元素的子元素.

...

一般规则是,只有元素的内容在文档的大纲中明确列出时,section元素才是合适的.

优秀.正是我们正在寻找的.与用于"自包含"内容的<section> [2]相反,<div>允许相关内容不是独立的,或者对于<aside>元素来说足够通用.

因此,规范似乎表明使用选项1,<section><section>孩子一起是最佳实践.

参考

  1. https://www.w3.org/TR/html51/sections.html#the-aside-element
  2. https://www.w3.org/TR/html51/sections.html#elementdef-article
  3. http://html5doctor.com/aside-revisited/

  • 相关链接(http://html5doctor.com/aside-revisited/)另有说明.OP确实提到过这是在`<article>`之外使用的. (3认同)
  • 对不起,但我不同意!:)你引用的文章是[2009年10月重新访问](http://html5doctor.com/aside-revisited/)(这是在你发布答案之后!)注意到"`aside`现在可以用于辅助内容当没有嵌套在文章元素中时." 这符合[W3C HTML5](http://www.w3.org/TR/html5/the-aside-element.html)和[WHATWG HTML](http://www.whatwg.org/specs) /web-apps/current-work/multipage/sections.html#the-aside-element)规范,两者都明确建议将其用于此目的,并包括如何执行此操作的示例. (2认同)

Wai*_*... 11

请参阅以下示例,来自HTML5规范aside.

它清楚地表明,目前推荐的内容(2012年10月)是将aside元素组合在元素内部.然后,每个小部件都是最好的代表它,a nav,serie blockquotes

以下摘录显示了如何在博客上使用blogrolls和其他内容:

<body>
 <header>
  <h1>My wonderful blog</h1>
  <p>My tagline</p>
 </header>
 <aside>
  <!-- this aside contains two sections that are tangentially related
  to the page, namely, links to other blogs, and links to blog posts
  from this blog -->
  <nav>
   <h1>My blogroll</h1>
   <ul>
    <li><a href="http://blog.example.com/">Example Blog</a>
   </ul>
  </nav>
  <nav>
   <h1>Archives</h1>
   <ol reversed>
    <li><a href="/last-post">My last post</a>
    <li><a href="/first-post">My first post</a>
   </ol>
  </nav>
 </aside>
 <aside>
  <!-- this aside is tangentially related to the page also, it
  contains twitter messages from the blog author -->
  <h1>Twitter Feed</h1>
  <blockquote cite="http://twitter.example.net/t31351234">
   I'm on vacation, writing my blog.
  </blockquote>
  <blockquote cite="http://twitter.example.net/t31219752">
   I'm going to go on vacation soon.
  </blockquote>
 </aside>
 <article>
  <!-- this is a blog post -->
  <h1>My last post</h1>
  <p>This is my last post.</p>
  <footer>
   <p><a href="/last-post" rel=bookmark>Permalink</a>
  </footer>
 </article>
 <article>
  <!-- this is also a blog post -->
  <h1>My first post</h1>
  <p>This is my first post.</p>
  <aside>
   <!-- this aside is about the blog post, since it's inside the
   <article> element; it would be wrong, for instance, to put the
   blogroll here, since the blogroll isn't really related to this post
   specifically, only to the page as a whole -->
   <h1>Posting</h1>
   <p>While I'm thinking about it, I wanted to say something about
   posting. Posting is fun!</p>
  </aside>
  <footer>
   <p><a href="/first-post" rel=bookmark>Permalink</a>
  </footer>
 </article>
 <footer>
  <nav>
   <a href="/archives">Archives</a> —
   <a href="/about">About me</a> —
   <a href="/copyright">Copyright</a>
  </nav>
 </footer>
</body>
Run Code Online (Sandbox Code Playgroud)


mrw*_*web 7

根据这个HTML5 Doctor图表,我认为这可能是最好的标记:

<aside class="sidebar">
    <article id="widget_1" class="widget">...</article>
    <article id="widget_2" class="widget">...</article>
    <article id="widget_3" class="widget">...</article>
</aside> <!-- end .sidebar -->
Run Code Online (Sandbox Code Playgroud)

我认为很明显,<aside>只要它在主要元素之外,它就是合适的<article>元素.

现在,我认为这<article>也适用于旁边的每个小部件.在该W3C的话:

article元素表示文档,页面,应用程序或站点中的自包含组合,并且原则上可独立分发或可重用,例如在联合中.这可以是论坛帖子,杂志或报纸文章,博客条目,用户提交的评论,交互式小部件或小工具,或任何其他独立的内容项.

  • 这完全取决于小部件的内容.在许多情况下,使用`article`是不正确的. (3认同)