What is the difference between id and class in CSS, and when should I use them? [duplicate]

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

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

OR

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

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

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 bit confused while dealing with this.

15 Answers
15

Leave a Comment