简体   繁体   English

为什么H2比H1大?

[英]Why is H2 larger than H1?

In the following code snippet, why is the H2 content larger than the H1 content?在下面的代码片段中,为什么 H2 内容比 H1 内容大?

<article>
    <section>
    <header>
        <h1>First Header</h1>
    </header>
    </section>
    <section>
    <header>
        <h2>Second Header</h2>
    </header>
    </section>
</article>

http://jsfiddle.net/abugp/ http://jsfiddle.net/abugp/

Why is the H1 content larger in the snippet below but not the one above?为什么下面的代码段中的 H1 内容更大,而不是上面的?

<h1>First Line</h1>
<h2>Second Line</h2>

http://jsfiddle.net/59T43/ http://jsfiddle.net/59T43/

Since you haven't specified any styles, the size of the headings is determined by your browser's default style sheet.由于您没有指定任何样式,标题的大小由浏览器的默认样式表决定。 In particular, this means that the relative size of the two headers may vary depending on the viewer's browser.特别是,这意味着两个标题的相对大小可能会因查看者的浏览器而异。

Looking at your fiddle in Chrome 33, I do see the effect you describe.在 Chrome 33 中查看您的小提琴,我确实看到了您描述的效果。 Right-clicking the headings and selecting "Inspect element" reveals that the issue is cause by the presence of the <article> and/or <section> tags around the headings.右键单击标题并选择“检查元素”显示问题是由标题周围的<article>和/或<section>标记引起的。

In particular, Chrome's default style sheet normally includes the rules:特别是,Chrome 的默认样式表通常包含以下规则:

h1 { font-size: 2em }

and:和:

h2 { font-size: 1.5em }

However, the former rule is overridden inside <article> and/or <section> tags by some more specific rules, presumably designed to make section headings smaller than normal "full page" headings:然而,前一条规则在<article>和/或<section>标签内被一些更具体的规则覆盖,大概是为了使节标题比正常的“整页”标题小:

:-webkit-any(article,aside,nav,section) h1 {
    font-size: 1.5em;
}

:-webkit-any(article,aside,nav,section)
:-webkit-any(article,aside,nav,section) h1 {
    font-size: 1.17em;
}

The non-standard :-webkit-any(...) selector presumably just matches any of the tags listed inside the parentheses.非标准的:-webkit-any(...)选择器大概只匹配括号内列出的任何标签。 The effect is that any <h1> headings inside an <article> , <aside> , <nav> or <section> tags is reduced to the size of a normal <h2> heading, and any <h1> inside two such tags is shrunk further down, presumably to the size of a normal <h3> or so.效果是<article><aside><nav><section>标签内的任何<h1>标题都减小到正常<h2>标题的大小,并且两个这样的标签内的任何<h1>是进一步缩小,大概缩小到正常<h3>左右的大小。

Crucially, the Chrome default style sheet doesn't have any such special rules for <h2> tags, so they'll always (in Chrome 33, anyway) be shown at the same size.至关重要的是,Chrome 默认样式表对<h2>标签没有任何此类特殊规则,因此它们将始终(无论如何在 Chrome 33 中)以相同大小显示。 Thus, when surrounded by two or more <article> and/or <section> tags, <h1> becomes smaller than <h2> .因此,当被两个或多个<article>和/或<section>标签包围时, <h1>变得小于<h2>

If you don't specify any style, your browser will choose its default style.如果您不指定任何样式,您的浏览器将选择其默认样式。 In the first example the h1 and h2 are inside an header in a section, while in the second case they are in the root.在第一个示例中, h1 和 h2 位于节中的标题内,而在第二个示例中,它们位于根中。 Then it is admissible that the behaviour is different.那么行为不同是可以接受的。

This is part of the HTML5 spec for sections and headings :这是部分和标题HTML5 规范的一部分:

In the following CSS block, x is shorthand for the following selector: :matches(article, aside, nav, section)在下面的 CSS 块中, x是以下选择器的简写: :matches(article, aside, nav, section)

 x h1 { margin-block-start: 0.83em; margin-block-end: 0.83em; font-size: 1.50em; } xx h1 { margin-block-start: 1.00em; margin-block-end: 1.00em; font-size: 1.17em; } xxx h1 { margin-block-start: 1.33em; margin-block-end: 1.33em; font-size: 1.00em; } xxxx h1 { margin-block-start: 1.67em; margin-block-end: 1.67em; font-size: 0.83em; } xxxxx h1 { margin-block-start: 2.33em; margin-block-end: 2.33em; font-size: 0.67em; }

Curiously, there are no such rules for h2–h6.奇怪的是,h2-h6 没有这样的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM