Click here to Skip to main content
15,885,979 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

When to use "#" and "." while designing CSS??

e.g. What is difference between

html,body
{ }

#info
{ }

.infoitem
{ }

I am confused. How to include them in my html.
Posted

Simply put, there are three basic cases:

Starts with a '.' - refers to class
Starts with a '#' - refers to id
Starts with neither - refers to tag name


.myClass - this will select all items with class='myClass'
#myId - this will select the single item with id='myId'
element - this will select all items of tag element <element>

HTML
<style>
    .firstItem { color: red; }
    #secondItem { color: green; }
    p { color: blue; }
</style>

HTML
<div class='firstItem'>This should be RED</div>
<div id='secondItem'>This should be GREEN</div>
<p>This should be BLUE</p>
 
Share this answer
 
Comments
Arunprasath Natarajan 30-Aug-12 9:38am    
Well said.
comred 31-Aug-12 4:11am    
Bingo... Master class..
Thank you sir..
5*
enhzflep 31-Aug-12 6:45am    
You're most welcome. :)
enhzflep 31-Aug-12 6:44am    
Thank-you Arunprasath :)
CSS IDs :
are similar to classes in that they define a special case for an element. Below is an example of a CSS ID.
CSS Code:

p#e1 { background-color: white; }


HTML Code:

This paragraph has an ID name of
"exampleID1" and has a white CSS defined background



CSS Class:A class can be used several times, while an ID can only be used once

CSS Code:

p.myclass { background-color: #013370; color: white;}

HTML Code:

Check my class

 
Share this answer
 
Comments
comred 31-Aug-12 4:13am    
Thank you Kashinath
same for all Including in HTML. For including in html there is no difference between #info and .info.

But if declare #footer then your div's ID should be footer just like given below:-

div id="footer"

if you declare .footer then your div's class property should be footer just like given below:-

div id="div1" class="footer"
 
Share this answer
 
v2
Comments
comred 31-Aug-12 4:13am    
Thank you Sunil.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900