对许多 DIV 使用相同的 CSS 类

发布于 2025-01-02 10:22:11 字数 91 浏览 0 评论 0原文

我正在尝试对多个 DIV 使用一个 CSS 类。这是一个好的做法吗?否则这样做有什么缺点吗?因为在 VS 中它说“另一个对象已经使用这个 ID”,不是错误消息而是警告。

I am trying to use one CSS class to many DIV. Is it a good practice? or else are there any disadvantages of doing that. because in VS it says "another object already uses this ID", not an error message but a warning.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自演自醉 2025-01-09 10:22:11

CSS 类是为了重用。 CSS ID 旨在在组件上唯一使用。你确定你使用的是 css 类吗?

CSS classes are meant to be reused. CSS IDs are meant to be used uniquely on a component. Are you sure you're using a css class?

愁以何悠 2025-01-09 10:22:11

同时设置多个 div 的样式并没有什么问题。这实际上是预料之中的事情,而且没有多少人这样做。为所有 div 设置样式;

body {Body Code Here;}
div {Div styling here;}

用一个类来设计所有 div 的样式;

body {Body Code Here;}
div.YourClass {Div Styling Here;}

请注意 div 和 selector(.) 之间没有空格,它在任何 div 中指定一个类,其中 div .YourClass 将在所有 div 中搜索某个类。 ID 与类不同,页面上可以有多个类,但页面上只能有 1 个 ID。要修复该消息,请确保您使用的是 Class 而不是 ID,并在页面中搜索该 ID,如果它出现两次或更多次,那就是您的问题。同样,类是可重用的,ID 是单元素选择器。希望我有帮助!

There is nothing wrong with styling multiple divs at the same time. This is actually expected and not many people do that. To style all divs;

body {Body Code Here;}
div {Div styling here;}

Styling all div's with one class;

body {Body Code Here;}
div.YourClass {Div Styling Here;}

Notice how there is no space between the div and selector(.) That specifies a class in any div where as div .YourClass will search in all divs for a class. IDs are different than Classes, you can have multiple Classes on the page but only 1 ID on the page. To fix that message, make sure you are using Class instead of ID and search the page for that ID and if it comes up twice or more, thats your problem. Again Classes are reusable and IDs are single-element selectors. Hope I helped!

浸婚纱 2025-01-09 10:22:11

在css中,class是为了重用的,所以没问题。如果你不知道 id,那么 css 中的 id 就应该被使用。但如今,我看到很少有人到处使用类,即使他们只使用一次。因此,只要保持标记和样式清晰,如果您单独工作[个人项目],请以您的方式使用类或id,并在单独工作或在团队中工作时保持持久性。

html

<div class="class1 class2">afd</div>
<div class="button class1">asffsdf</div>

css

.class1 { /* styling your class1 here */ }

In css, class is meant to be reused, so it is ok. In case you don't know about id, id in css is meant to be used noce. But nowadays, I've seen few people use class all over place even they just use it once. So, just keep your markup and styling clear, use class or id in your way if you work alone [ personal project ] and keep it persistence when working alone or in a team.

html

<div class="class1 class2">afd</div>
<div class="button class1">asffsdf</div>

css

.class1 { /* styling your class1 here */ }
旧时模样 2025-01-09 10:22:11

对于 CSS 中的许多元素使用同一个类是绝对没问题的。请注意不要对“id”属性执行相同的操作,该属性实际上只应用于单个元素,因为它被视为唯一 ID。

It is absolutely fine to use the same class for many elements in CSS. Be careful not to do the same for the "id" attribute, which really should only be used for a single element as it is considered a unique ID.

亣腦蒛氧 2025-01-09 10:22:11

您可以对任意数量的元素使用相同的 CSS 类,但页面中的每个元素的 ID 必须是唯一的

You can use same CSS class for any number of elements, but ID must be unique for each and every element in the page

一梦浮鱼 2025-01-09 10:22:11

ID与班级有些不同。页面上可以有多个类,但 ID 只能使用一次。请遵循最佳实践:)

我知道...这些信息并不是您想要的答案,但可能会有一些帮助:)

示例:

div.className { } //covers all div with a particular classname

ID is somewhat different from class. You can have multiple classes on page but ID should only be used once. Do follow best practices :)

i know... this information is not what exactly you want as answer but may be of some little help :)

example:

div.className { } //covers all div with a particular classname
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文