CSS中的类和ID有什么区别?解释在哪里使用的例子 [英] What is the difference between classes and IDs in CSS? Explain with example of where to use

查看:116
本文介绍了CSS中的类和ID有什么区别?解释在哪里使用的例子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里可以使用ids和类?它们之间有什么区别?是否有必要在我们的CSS中强制使用ids?

Where can I use ids and classes? What is the difference between them? Is there any need that we should compulsorily use ids in our CSS?

推荐答案

ID是唯一的,

所以你可能有这样的:

<ul id="menu">
....
</ul>

因为您的网站上可能只有一个主菜单。

Because you will probably only have 1 main menu on your site.

但是,对于类,你可能有类似的:

For classes, however, you might have something like:

<span class='author'>Paolo Bergantino</span>

或者可以对包含 div 在此网站上的答案:

Or perhaps to style the div that contains an answer on this site:

<div class='answer'>....</div>

因为每页会有多个,所以它们是一类元素。将ID视为元素的社会保障号。每当一个元素足够重要并且是唯一的,你给它一个ID。这也有助于动态网站作为选择元素的ID是迄今为止最快的方式,如果你有多个元素具有相同的ID(因此违反了这一做法)Javascript将无法正常工作。

Because there will be multiple of these per page, they are a class of elements. Think of an ID as the social security number of an element. Whenever an element is important enough and is unique, you give it an ID. This also helps with dynamic websites as selecting elements by ID is by far the fastest way, and if you have multiple elements with the same ID (thus violating this practice) Javascript won't work as intended.

这篇关于CSS中的类和ID有什么区别?解释在哪里使用的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆