具有相同名称的 CSS 类和 id

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3253775/
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 22:23:30  来源:igfitidea点击:

CSS class and id with the same name

cssclass

提问by Spencer

Is there anything wrong with having a css class and id with the same name? Like .footer for the article/post's footer and #footer for the page footer.

使用同名的 css 类和 id 有什么问题吗?像 .footer 表示文章/帖子的页脚,#footer 表示页面页脚。

回答by djdd87

Nope, perfectly acceptable.

不,完全可以接受。

A class is defined using a .and an ID is defined using a #. So as far as the browser is concerned, they're two totally separate items.

类使用 定义.,ID 使用 定义#。所以就浏览器而言,它们是两个完全独立的项目。

The only thing to be careful of is generating any confusion for yourself. It's probably best practise to keep the names different purely for code readability, but there's no harm in them being the same.

唯一要小心的是给自己带来任何混乱。纯粹为了代码可读性而保持名称不同可能是最佳实践,但它们相同也没有坏处。

回答by Thariama

No, there is nothing wrong, but it might be better to use different names to not confuse other developers. It is more (human) error prone to use different names.

不,没有错,但最好使用不同的名称以免混淆其他开发人员。使用不同的名称更容易(人为)错误。

回答by Anuj Sharma

Yes, you can use same name for both idand classbecause both parameters have their own significance. Thanks

是的,您可以对idclass使用相同的名称,因为这两个参数都有自己的意义。谢谢

回答by SuspectBloom993

The only problem with having same name for class and id is code readability and maintainability. They can have the same names, the browser will render the correct CSS as classes and ids are marked differently (. for class and # for id), but an upgrade or bug fix can be a problem for the developer.

class 和 id 具有相同名称的唯一问题是代码可读性和可维护性。它们可以具有相同的名称,浏览器将呈现正确的 CSS,因为类和 id 的标记不同(. 表示 class 和 # 表示 id),但升级或错误修复对开发人员来说可能是一个问题。