我知道其他人已经询问过在CSS文件中使用类和ID,例如
I know others have asked about using class and id in CSS files, such as
Div:Class vs Id
所以我知道类和id之间的语义和句法差异:该id应该用于只使用一次的元素,类应该用于共享属性的元素。
So I'm aware of the semantic and syntactic differences between class and id: that id should be used for elements that are used only once and class should be used for elements that share attributes in common.
但这不是一个难以控制的规则,是吗?将id用于多个元素有什么危害?或者只使用一个类?毕竟,不是一个元素只是一个集合(类)只有一件事?
But this isn't a hard-and-fast rule, is it? What's the harm in using an id for more than one element? Or using a class for only one? After all, isn't "one element" just a set (class) with only one thing in it?
浏览器的CSS解释器会抛出一个错误,如果我打破规则?我没有看到它发生。
Will the browser's CSS interpreter throw an error if I break the rules? I haven't seen it happen.
那么为什么我们有id和类?为什么不只是一个或另一个,并称之为好?
So why do we have both id and class? Why not just one one or the other and call it good?
推荐答案在回答问题' and id for CSS,when you are allowed to have a single instance of a class?',look it it this way。
In answer to the question 'why do we use both classes and ids for CSS, when you're allowed to have a single instance of a class?', look at it this way.
我们需要ID的CSS。
We don't need IDs for CSS. We could just use single instances of classes.
但我们需要JavaScript的ID。
But we need IDs for JavaScript. So, why not use them in CSS too?
想象一下,一个世界里有ID,但只用于JavaScript。
Imagine a world in which IDs were there, but only used for JavaScript.
您必须像下面这样编码:
You'd have to code like this:
<div id="wrapper" class="wrapper"> <div id="nav" class="nav"> </div> </div>等等。
更多推荐
CSS:类和id是否可互换?
发布评论