a_krishnam_raju Posted January 25, 2006 Share Posted January 25, 2006 Hi,I am Krishnam from India.I am new to CSS and i have read about the 3 types of CSS Style Selectors.1) TAG2) CLASS3) IDI am confused over the use of CLASS and ID. Can you help me in explaining about when to use CLASS and when to use ID, and its advantages.Thank you,Krishnam Link to comment Share on other sites More sharing options...
DarkElf Posted January 25, 2006 Share Posted January 25, 2006 Only one element on your page can have any specific ID, therefore you should only use id to define an elements formatting if that is the only element on the page you want that specific formatting to apply to. Note that the page will still work in all major browsers even if several elements have the same id, and the formatting will be applied to all of them, however this is not valid html.As many elements can have the same class as you want, therefore if you want to apply the same formatting to lots of elements then you should use class - for instance several seperate paragraphs which all need to have the same formatting but which are formatted differently from the general paragraph tag. Link to comment Share on other sites More sharing options...
a_krishnam_raju Posted January 25, 2006 Author Share Posted January 25, 2006 Only one element on your page can have any specific ID, therefore you should only use id to define an elements formatting if that is the only element on the page you want that specific formatting to apply to. Note that the page will still work in all major browsers even if several elements have the same id, and the formatting will be applied to all of them, however this is not valid html.As many elements can have the same class as you want, therefore if you want to apply the same formatting to lots of elements then you should use class - for instance several seperate paragraphs which all need to have the same formatting but which are formatted differently from the general paragraph tag.<{POST_SNAPBACK}> Thanks a lot for the explanation. What i understood is;1) Class can be used with a number of elements and any number of times, where as2)ID can be used to a single elements ( and the element can repeate any number of times) Is it OK? Link to comment Share on other sites More sharing options...
Jonas Posted January 25, 2006 Share Posted January 25, 2006 <html><head><title>Untitled document</title><link rel="stylesheet" type="text/css" href="style.css" /></head><body><div id="menu"><a class="menu" href="#">Link desc.</a><a class="menu" href="#">Link desc.</a><a class="menu" href="#">Link desc.</a><a class="menu" href="#">Link desc.</a><a class="menu" href="#">Link desc.</a></div></body></html> Here you have one element ("<div></div>") with the attribute id="menu". For a menu you would want to apply the same formatting to each link. That's where you use the class attribute.style.css div#menu {width: ?px;height: ?px;background: #??????;}a {color: #??????:}a.menu {font-size: ??px;color: #??????;} You can have a different formatting for the anchor elements in your menu div by using class, while you have a general anchor formatting for your page. ID's are used on one element only, in this case the <div> element. Classes can be applied to several elements for formatting. You can also specify further: div.menu {css info}a.menu {css info}.menu {css info} <div class="menu"> and <a class="menu"> can now have different formatting than the rest of the elements you put class="menu" on. But it's always better to use more than one classname, so as not to get confused. Link to comment Share on other sites More sharing options...
Eric Posted January 25, 2006 Share Posted January 25, 2006 Here's another "cool" little tid bit of knowledge:You can also use ID when adding "bookmark links" within your page.So ID can be used for more than just CSS styling.Example:<a href="#a">A</a> | <a href="#b">B</a> | <a href="#c">C</a><p id="a">Items with A</a>Apple<br />etc.</p><p id="b">Balloons for sale</p><p id="C">C you get the idea</p> Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now