This question already has an answer here:
<style>
#main{
background: #000;
border: 1px solid #AAAAAA;
padding: 10px;
color: #fff;
width: 100px;
}
</style>
<div id="main">
Welcome
</div>
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>
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 confused while dealing with this.
You can assign a
class
to many elements. You can also assign more than oneclass
to an element, eg.in Bootstrap. You can assign the
id
only to one. So if you want to make many elements look the same, eg. list items, you chooseclass
. If you want to trigger certain actions on an element using JavaScript you will probably useid
.The
class
attribute can be used with multiple HTML elements/tags and all will take the effect. Where as theid
is meant for a single element/tag and is considered unique.Moreoever the
id
has a higher specificity value than theclass
.This is very simple to understand :-
id is used when we have to apply CSS property to one attribute only.
class is used when we have to use CSS property in many locations within the same page or different.
General :- for unique structure like staring div and buttons layout we use id .
for same CSS throughout the page or project we use class
id is light and class is little heavy
If there is something to add to the previous good answers, it is to explain why
id
s must be unique per page. This is important to understand for a beginner because applying the sameid
to multiple elements within the same page will not trigger any error and rather has the same effects as aclass
.So from an HTML/CSS perspective, the uniqueness of
id
per page does not make a sens. But from the JavaScript perspective, it is important to have oneid
per element per page becausegetElementById()
identifies, as its name suggests, elements by theirid
s.So even if you are a pure HTML/CSS developer, you must respect the uniqueness aspect of
id
s per page for two good reasons:Use a class when you want to consistently style multiple elements throughout the page/site. Classes are useful when you have, or possibly will have in the future, more than one element that shares the same style. An example may be a div of "comments" or a certain list style to use for related links.
Additionally, a given element can have more than one class associated with it, while an element can only have one id. For example, you can give a div two classes whose styles will both take effect.
Furthermore, note that classes are often used to define behavioral styles in addition to visual ones. For example, the jQuery form validator plugin heavily uses classes to define the validation behavior of elements (e.g. required or not, or defining the type of input format)
Examples of class names are: tag, comment, toolbar-button, warning-message, or email.
Use the ID when you have a single element on the page that will take the style. Remember that IDs must be unique. In your case this may be the correct option, as there presumably will only be one "main" div on the page.
Examples of ids are: main-content, header, footer, or left-sidebar.
A good way to remember this is a class is a type of item and the id is the unique name of an item on the page.
A simple way to look at it is that an id is unique to only one element.
A class is not unique and applied to multiple elements.
For example:
Content is a class since it'll probably apply to some other tags aswell where as "veryImportant" is only being used once and never again.