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.
For more info on this click here.
Example
(Note that CSS uses the prefix # for IDs and . for Classes.)
However
color
was an HTML 4.01<font>
tag attribute deprecated in HTML 5. In CSS there is no "font-color", the style iscolor
so the above should read:Example
The text would be white.
ID's have the functionality to work as links to particular sections within a webpage. a keyword after # tag will take you to a particular section of the webpage. e.g "http://exampleurl.com#chapter5" in the address bar will take you there when you have a "section5" id wrapped around the chapter 5 section of the page.
id:
It will identify the unique element of your entire page. No other element should be declared with the same id. The id selector is used to specify a style for a single, unique element. The id selector uses the id attribute of the HTML element, and is defined with a "#".
class:
The class selector is used to specify a style for a group of elements. Unlike the id selector, the class selector is most often used on several elements.
This allows you to set a particular style for many HTML elements with the same class.
The class selector uses the HTML class attribute, and is defined with a "."
A class can be used several times, while an ID can only be used once, so you should use classes for items that you know you're going to use a lot. An example would be if you wanted to give all the paragraphs on your webpage the same styling, you would use classes.
Standards specify that any given ID name can only be referenced once within a page or document. Use IDs when there is only one occurence per page. Use classes when there are one or more occurences per page.
id
s are uniqueid
id
class
es are NOT uniqueclass
on multiple elements.class
es on the same element.Javascript cares
JavaScript people are already probably more in tune with the differences between
class
es andid
s. JavaScript depends on there being only one page element with any particularid
, or else the commonly usedgetElementById
function couldn't be depended on.Unlike the
id
selector, theclass
selector is most often used on several elements.This allows you to set a particular style for many HTML elements with the same class.
The
class
selector uses the HTML class attribute, and is defined with a "."A simple way to look at it is that an id is unique to only one element.
class
is better to use as it will help you to execute what ever you want.