问题描述
当使用 divs
时,什么时候使用 class
和 id
最好?
When using divs
when is it best to use a class
vs id
?
是否最好使用 class
,比如 html 中的字体变体或元素?然后使用 id
作为结构/容器?
Is it best to use class
, on say font variant or elements within the html? Then use id
for the structure/containers?
这是我一直有点不确定的事情,任何帮助都会很棒.
This is something I've always been a little uncertain on, any help would be great.
推荐答案
使用 id
来标识在页面上只有一个实例的元素.例如,如果您将单个导航栏放置在特定位置,请使用 id="navigation"
.
Use id
to identify elements that there will only be a single instance of on a page. For instance, if you have a single navigation bar that you are placing in a specific location, use id="navigation"
.
使用 class
将所有行为都以特定方式的元素分组.例如,如果您希望公司名称在正文中以粗体显示,您可以使用 <span class='company'>
.
Use class
to group elements that all behave a certain way. For instance, if you want your company name to appear in bold in body text, you might use <span class='company'>
.
这篇关于Div 的类与 id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!