html header 总是必须从 1 开始计数吗?
我说的是正确的语义标记。
我知道通常没有理由不从 h1 开始,然后从那里开始往下走。
但是,如果我正在构建类似侧边栏的东西,它有自己的稍微独立的层次结构,那么最佳实践是什么?我知道我可以从 h1 开始并使用 css 规则来区分侧边栏 h1 和主 h1,但如果我从 h3 开始,可能更有可能我不必编写那么多 css,因为它会直接匹配,或者至少更接近主 h3。
同时,这也不是严格意义上的语义,不是吗?
这里有最佳实践吗? (我主要关心屏幕阅读器看起来最好的)
I'm talking about proper semantic mark-up.
I know normally there's no reason not to start at h1 and work your way down from there.
But, if I am building something like a sidebar that has its own somewhat separate hierarchy, what is the best practice? I know I could start at h1 and use css rules to distinguish sidebar h1 from main h1, but if I start instead at h3, there might be a better chance that I don't have to write as much css because it would match up directly, or at least closer, to the main h3.
At the same time, that is not strictly semantic, is it?
Is there a best practice here? (I'm primarily concerned with what looks best to a screen-reader)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有些人认为跳过标题级别是不好的做法。这种观点非常普遍,以至于它实际上在 HTML 4.01 中提到过规格(参见绿色文本)。但规范并没有说你不应该跳过级别。
事实上, HTML5 规范 甚至包含一个示例,其中所有标题都是
,使用新的
标记相互区分。几年前,这看起来很疯狂。
我仍然认为至少有一个包含文章标题的
是一种很好的做法。但侧边栏通常不需要任何特定级别的标题。没有人关心 - 甚至搜索引擎也不关心 - 无论您是从侧边栏中的
还是
开始。只要确保有某种层次感即可,以帮助使用屏幕阅读器的人。除此之外,想做什么就做什么吧。
Some people think it's bad practice to skip heading levels. This view is so prevalent that it's actually mentioned in the HTML 4.01 spec (see green text). But the spec doesn't say you shouldn't skip levels.
In fact, the HTML5 spec even contains an example where all the headings are
<h1>
, distinguished from one another using the new<section>
tag. That would have looked crazy a few years ago.I still consider it good practice to have at least one
<h1>
with the article title in it. But the sidebar usually doesn't need any specific level of headings. Nobody cares -- not even search engines -- whether you start from<h1>
or<h4>
in a sidebar. Just make sure there's some sense of hierarchy, to help people with screen readers. Other than that, do whatever the heck you want to do.当您考虑标题时,请忽略 CSS 和样式。这完全取决于页面标题的重要性。
就我个人而言,我最多会在大约
h3
处开始侧边栏标题,有些甚至从h5
开始。文档中标题的权重应决定相应的级别。例如,相当不重要的标题“Navigation”不应该是h1
。对于 Google 和其他搜索引擎来说,如果“Navigation”和“My Amazing title”都是
h1
,那么它们同等重要,但语义上并非如此。一旦确定了标题的语义级别,您就可以深入研究并使用 CSS 设计它们的样式,以获得您认为适合您的设计的尺寸。为了避免开发人员产生错误的印象,他们确实应该将所有标题的默认字体大小设置为相同。
Disregard CSS and styling when you are thinking about headers. It is completely down to the importance of the heading in your page.
Personally I would start a sidebar heading at about
h3
at the most, some even start ath5
. The weight of the heading in your document should determine the respective level. For example a fairly unimportant header "Navigation" should not be ah1
.To Google and other search engines, if both "Navigation" and "My Amazing title" are
h1
s they are equally important, which is semantically not the case.Once you have determined the semantic level of you headings, you can dive in and style them with CSS in order to get the size which you deem correct for your design. They really should have made the default font size the same for all headings for the sake of developers getting the wrong impression.
依赖。我认为有些搜索引擎认为 h1 比 h2、h3 等更重要。但对于编码来说这并不重要。
Depents. I think some search engines think h1 is more important as h2,h3, etc. But for coding it doesn't matter.
我认为使用 CSS 后代将是推荐的方式。不用担心编写“太多”CSS。使用 CSS 定义的布局越明确,布局就越灵活。我会使用侧边栏 ID 作为主选择器。
并在代码中定义它,如下所示:
此外,出于可访问性的原因,我建议仅使用
h1
一次,仅将其用于主要内容的标题。同样,仅将h2
-h6
用于内容副标题。 (请参阅 WAI-ARIA角色
s 了解菜单-具体标记)。I think using CSS descendents would be the recommended way. Don't worry about writing "too much" CSS. The more explicitly you define your layout using CSS, the more flexible it will be. I would use the sidebar ID as main selector.
And define it in code as such:
Additionally, I recommend using
h1
only once for accessibility reasons, using it only for the title of the main content. Similarly, useh2
-h6
only for content subheadings. (See WAI-ARIArole
s for menu-specific markup).您可以使用列表,然后使用类在 CSS 中格式化它们,即使这也不是很语义化:
段落标签可能更语义化:
You could use list and then use classes to format them in CSS, even though that would also not be very semantic:
Paragraph tags may be more semantic: