Html Body 标签上的类或 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3919456/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:50:06  来源:igfitidea点击:

Class or ID on Body Tag

htmlcssmarkupsemantics

提问by Jo Sprague

I've been applying an ID to the body tag of my HTML documents lately to allow greater CSS control (#9). Recently the though occurred to me that I could do exactly the same thing by applying a class to the body tag. I want to know positives and negatives of each choice. If I have multiple pages where the body tag has the same ID, is it better to use a class? What do you think? Why?

我最近一直在将 ID 应用到我的 HTML 文档的 body 标记以允许更好的 CSS 控制 (#9)。最近我突然想到,我可以通过将一个类应用于 body 标签来做完全相同的事情。我想知道每个选择的正面和负面。如果我有多个页面的 body 标签具有相同的 ID,使用类是否更好?你怎么认为?为什么?

UPDATE: The class/ID is basically what I intend to use to identify to the stylesheet which page or type of page the stylesheet is being applied to. For example, is it the homepage, the contact page, one of many search results pages, an archive page, etc?

更新:类/ID 基本上是我打算用来标识样式表应用到哪个页面或页面类型的样式表。例如,它是主页、联系页面、众多搜索结果页面之一、存档页面等?

采纳答案by Dagg Nabbit

Using a class is perfectly acceptable, possibly more so than using an id. Use of ids should be limited as much as possible, because they can clash with other ids and break things like document.getElementById.

使用类是完全可以接受的,可能比使用 id 更容易接受。应尽可能限制 id 的使用,因为它们可能与其他 id 发生冲突并破坏诸如document.getElementById.

An id selector is more specific than a class selector, which usually makes it a better choice for your use case. -- David Dorward

id 选择器比类选择器更具体,这通常使它成为您用例的更好选择。 ——大卫·多沃德

However, a high "specificness" is not always desirable. Consider body#faq #col_b a {color:gray;}in master stylesheet and then someone develops a plugin with a gray background that goes on faq page, now they need to use !importantor create another id attribute to give higher specificity.

然而,高“特异性”并不总是可取的。考虑body#faq #col_b a {color:gray;}在主样式表中,然后有人开发了一个灰色背景的插件,用于常见问题页面,现在他们需要使用!important或创建另一个 id 属性以提供更高的特异性。

Also, consider the use of multiple classes in a single body element:

另外,请考虑在单个 body 元素中使用多个类:

<body class="faq two_column big_footer"> ...

回答by Quentin

An id selector is more specificthan a class selector, which usually makes it a better choice for your use case.

id 选择器比类选择器更具体,这通常使它成为您用例的更好选择。

回答by Tyler Treat

ID needs to be unique per page (if you want valid markup), meaning only one instance of a particular ID should exist on a page.

每个页面的 ID 需要是唯一的(如果您想要有效的标记),这意味着页面上应该只存在一个特定 ID 的实例。

Classes, on the other hand, can be applied to numerous elements per page. Use classes for things that reuse the same styles and use IDs for more unique elements that require their own styling.

另一方面,类可以应用于每页的多个元素。将类用于重用相同样式的事物,并将 ID 用于需要自己样式的更独特的元素。

Ideally, use classes whenever possible and limit the use of IDs.

理想情况下,尽可能使用类并限制 ID 的使用。

For more information on ID, see here, and more on classes, see here.

有关 ID 的更多信息,请参见此处,有关类的更多信息,请参见此处

回答by shaans

It is better to use a class if you plan to reuse a style in pages. IDs are good if style sheet is designed for a specific page. But still depends on your app.

如果您打算在页面中重用样式,最好使用类。如果样式表是为特定页面设计的,则 ID 是很好的。但仍然取决于您的应用程序。

回答by Nelga

You can (read: should) only use the id on the body tag.

您可以(阅读:应该)仅使用 body 标签上的 id。

ID = single use per page

ID = 每页单次使用

Class = multiple uses

类 = 多种用途

1 Body tag = 1 id tag. This can be the same on different pages. Essentially, using the class tag is probably overkill for this purpose.

1 个正文标签 = 1 个 ID 标签。这在不同的页面上可以是相同的。本质上,为此目的使用 class 标签可能是过度的。

回答by Mahdi Jazini

In HTML5, the id attribute can be used on any HTML element (it will validate on any HTML element. However, it is not necessarily useful).

在 HTML5 中,id 属性可用于任何 HTML 元素(它将在任何 HTML 元素上进行验证。但是,它不一定有用)。

In HTML 4.01, the id attribute cannot be used with: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

在 HTML 4.01 中,id 属性不能用于: <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title>.

Note: HTML 4.01 has greater restrictions on the content of id values (for example; in HTML 4.01 id values cannot start with a number).

注意:HTML 4.01 对 id 值的内容有更大的限制(例如;在 HTML 4.01 中,id 值不能以数字开头)。

Source: W3School

资料来源:W3School