CSS:类和id是否可互换?

编程入门 行业动态 更新时间:2024-10-28 12:28:51
本文介绍了CSS:类和id是否可互换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道其他人已经询问过在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是否可互换?

本文发布于:2023-10-07 20:03:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1470398.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:CSS   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!