CSS中的id和类之间的差异和何时使用它 [英] Difference between id and class in CSS and when to use it

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

问题描述

<style>
    #main{
        background: #000;
        border: 1px solid #AAAAAA;
        padding: 10px;
        color: #fff;
        width: 100px;
    }
</style>

<div id="main">
    Welcome
</div>

这里我给了 id code> div 元素,它会应用相关的CSS。

Here I gave an id to the div element and it's applying the relevant CSS for it.



OR

<style>
    .main{
        background: #000;
        border: 1px solid #AAAAAA;
        padding: 10px;
        color: #fff;
        width: 100px;
    }
</style>
<div class="main">
    Welcome
</div>

现在这里给了 class div ,它也为我做同样的工作。

Now here I gave a class to the div and it's also doing the same job for me.

那么 id class 和何时应该使用 id code> class 。我是CSS和网页设计的新手,在处理这个时有点困惑。

Then what is the exact difference between id and class and when should I use id and when should I use class. I am a newbie in CSS and Web design and a little confused while dealing with this.

推荐答案

> 点击此处 / strong>

For more info on this click here.

<div id="header_id" class="header_class">Text</div>

#header_id {font-color:#fff}
.header_class {font-color:#000}

(请注意,CSS对于ID使用前缀,对于类使用。)

(Note that CSS uses the prefix # for IDs and . for Classes.)

颜色是一个HTML 4.01 < font> 属性在HTML 5中已弃用。
在CSS中没有font-color,样式为 color ,因此上述内容应为:

However color was an HTML 4.01 <font> tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style is color so the above should read:

示例

<div id="header_id" class="header_class">Text</div>

#header_id {color:#fff}
.header_class {color:#000}

文字将为白色。

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

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