ID和类之间的差异 [英] Difference between ID and Class

查看:144
本文介绍了ID和类之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览 CSS:Class and ID 。当我尝试他们给出的例子时,我没有感觉到任何区别。

I was going through CSS :Class and ID . And I wont felt any differnce between them when I tried the examples given by them.

<!DOCTYPE html>
<html>
<head>
<style>
#para1
{
text-align:center;
color:red;
} 
</style>
</head>

<body>
<h1 id ="para1">Hai</h1>
<p id="para1">Hello World!</p>
<p>This paragraph is not affected by the style.</p>
</body>
</html>

给我输出,因为我希望说的 id选择器用于指定

gives me the output as I wish where the say The id selector is used to specify a style for a single, unique element.

我是在错误的角度寻找这个吗?

Am I looking to this in a wrong angle ?

推荐答案

首先, w3schools是一个可怕的资源。它是写得很差,混乱,不专心,经常误导。您应该在Mozilla开发人员网络中使用更好的资源集。 (它设置实现相同的事情)。

First of all, w3schools is a terrible resource. It is badly written, cluttered, unfocused, and frequently misleading. You should use this much better set of resources at the Mozilla Developer Network instead (it sets out to achieve the same thing).

您的示例根本不使用类。你有CSS调用一个ID,和2个HTML元素的ID(你不应该做的 - ID应该是唯一的!)。 2:

Your example doesn't use classes at all. You have CSS invoking an ID, and 2 HTML elements with that ID (which you shouldn't do — IDs are supposed to be unique!). The key difference between the 2:


  1. 之间的主要区别是引用唯一元素。应该只有1个任何1个ID的实例。

  2. 使用ID的CSS规则将会覆盖带有类的CSS规则,如果它们都尝试分配相同的属性。

  3. ID用于所有种类的原生应用程序:锚引用,表单,iframe,而类仅用于CSS样式。

我修改了您的代码作为这些点的示例。这里有一些代码使用类来引用:

I modified your code as an example of these points. Here's some of that code using classes for reference:

.paragraph {
    font-style: italic;
    color: green;
}

HTML:

<p id="para1" class="paragraph">Blah blah blah</p>

这篇关于ID和类之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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