我正在尝试编写高质量的语义 HTML5。
以下两个选项中哪一个在语义上更好:
选项1:
通过使用 ID 选择跨度来定义样式:
%body
%header#global-header
%h1
My Startup Name
%h2
Home of the best offers in the Motor City
%section#offers
%h1
Today's Offers
%h2
Here are the current offers for your city:
%article.offer
%header.offer-header
%span.restaurant-name
Some Burger Joint
%span.offer-title
80% off a burger
%section.price-details
%ul
%li.original-price
%p
Original Price
%p
$30
%li.offer-price
%p
Price
%p
$10
%section.offer-description
%p
Some burger joint is the most popular burger joint in the Motor City. Buy a big burger or a bunch of belly bombers.
%article.offer
...another offer...
在此选项下,我将在 SASS 中选择和设置餐厅名称和优惠标题的样式:
body {
h1 { font-size: 3em; }
h2 { font-size: 2em; }
article {
.restaurant-name { font-size: 1.25em; }
.offer-title { font-size: 1.5em; }
}
}
选项 2:
使用 h1 到 h6,将样式限定在 inside 并按类或 id 标记:
%body
%header#global-header
%h1
My Startup Name
%h2
Home of the best offers in the Motor City
%section#offers
%h1
Today's Offers
%p
Here are the current offers for your city:
%article.offer
%header.offer-header
%h2.restaurant-name
Some Burger Joint
%h3.offer-title
80% off a burger
%section.price-details
%ul
%li.original-price
%h4
Original Price
%h4
$30
%li.offer-price
%h4
Price
%h4
$10
%section.offer-description
%p
Some burger joint is the most popular burger joint in the Motor City. Buy a big burger or a bunch of belly bombers.
%article.offer
...another offer...
在此选项下,我将在 SASS 中选择和设置餐厅名称和优惠标题的样式:
body {
h1 { font-size: 3em; }
h2 { font-size: 2em; }
article {
%h1 { font-size: 1.25em; }
%h2 { font-size: 1.5em; }
}
}
使用选项 1,我使用 ID 定义样式来选择元素并设置样式。使用选项 2,我再次使用 h1 到 h6 的样式来定义样式,但这些样式的范围仅限于文章而不是整个文档。封闭页面的标题和副标题有 h1 和 h2 标签。
h1 到 h6 标签是否只应该在整个站点中以一种方式使用?或者是否可以在网站的主要部分中将 h1 限定为 h6?例如,在上面的网站中,带有 id offer 的部分将成为该网站的主要部分,并且还会有其他类似重要的部分。
我的印象是选项 1 更易于维护,因为它更具描述性。但是,没有什么可以阻止我仍然使用选项 2 中的 ID 来实现相同的可维护性。选项 2 在保持代码精简和我的 html 和 css 文件更小方面让我觉得更好。
我不确定一个实际上是否比另一个在语义上更好,特别是如果我在两者中都大量使用 ID。这仍然给我们留下了一个问题,即对于我的用例,h1-h6 还是跨度在语义上更好?
我希望这一切都说得通。让我知道是否需要更清楚。