其中哪一个在语义上更好?
为了使网站更易于访问,我被鼓励使用 HTML5 标签
、
一个例子:
<header>
<div class="center">
<h1>Title</h1>
<nav>
...
</nav>
</div>
</header>
<div>
<section>
...
</section>
</div>
<footer>
<div class="center">
...
</div>
</footer>
.center {
max-width: 70em;
margin: 0 auto;
}
header {
width: 100%
background-color: red;
}
footer {
width: 100%
background-color: green;
}
body > div {
width: 100%
background-color: blue;
}
这样真的更好吗?
<div id="head">
<header>
<h1>Title</h1>
<nav>
...
</nav>
</header>
</div>
<div>
<section>
...
</section>
</div>
<div id="foot">
<footer>
...
</footer>
</div>
In order to make websites more accessible I have been encouraged to use HTML5 tags <header>
, <footer>
, etc... to only surround the actual content, but I have a feeling that I might be doing something wrong.
An example body:
<header>
<div class="center">
<h1>Title</h1>
<nav>
...
</nav>
</div>
</header>
<div>
<section>
...
</section>
</div>
<footer>
<div class="center">
...
</div>
</footer>
.center {
max-width: 70em;
margin: 0 auto;
}
header {
width: 100%
background-color: red;
}
footer {
width: 100%
background-color: green;
}
body > div {
width: 100%
background-color: blue;
}
Is it actually better like this?
<div id="head">
<header>
<h1>Title</h1>
<nav>
...
</nav>
</header>
</div>
<div>
<section>
...
</section>
</div>
<div id="foot">
<footer>
...
</footer>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
至于哪个更好 -
DIV
位于HEADER
/FOOTER
之类的结构元素内或DIV
内的结构元素,它确实没关系,因为DIV
是通用容器,根本没有任何语义意义。在第一个示例中真正不语义/不好的做法是
center
类名。类名称应反映块的用途(内容
、产品
等),而不是其表示形式(中心
、红色
> 等)。As for what is better —
DIV
inside structural elements likeHEADER
/FOOTER
or structural elements insideDIV
, it does not matter sinceDIV
is common container without any semantic sense at all.What is really unsemantic/bad-practice in your first example is
center
class name. Class names should reflect purpose of block (content
,products
, etc.), not its presentation (center
,red
, etc.).基本上,从语义上讲,
div
元素不是必需的(也许您需要它们来设置样式?)。div
是一个没有语义的元素(作为内联元素span
的对应元素),您必须在没有更好的地方使用它们。即使您通过其 id 属性给它们一些语义,该语义也只有您知道,而不是任何网络搜索引擎(谷歌)或任何屏幕阅读器(对于盲人,对于例如),因为对于id
或class
值没有任何明确的约定。如果您使用
header
、footer
等,那么您就为它们赋予了语义。也许您想使用role
的某些值来增加它们的语义属性。顺便说一下,这个
部分
肯定是不需要的。看看 HTML5 Doctor 的人们怎么说:有了
,您就拥有了所需的一切。
Basically, that
div
elements are not required semantically speaking (maybe you need them for styling?).div
is an element without semantic (as its counterpart for inline elementsspan
) and you have to use them where there isn't anything better. Even if you give them some semantic with itsid
attribute, that semantic is only known by you and not for, for example, any web search motor (google) or any screen reader (for blind people, for example), because there aren't any definitive conventions aboutid
orclass
values.If you use
header
,footer
etc, you are giving them semantics. Maybe you want to increase their semantic using some value for therole
attribute.By the way, that
section
surely it isn't needed. Look at what people from HTML5 Doctor say:With a
<div role="main">
you have everything you need.最好是这样:
或者:
为什么你被告知要添加那些额外的包装
div
元素?It'd be better like this:
Or alternatively:
Why are you being told to add those extra wrapper
div
elements?尝试
一下,摆脱所有那些愚蠢的 div,现在您已经将页眉和页脚链接到您的部分,就像它们应该的那样。
Try
This get's rid of all those silly divs and now you have your header and footer linked to your section like they should be.