Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The attributes "ID" and "Class"

Status
Not open for further replies.

seaport

MIS
Jan 5, 2000
923
US
"ID" and "Class" are two of the most important attributes of objects in a web page. However, I am pretty confused of how to use them.

I understand that "class" is for a style. How about ID? In the HTML books I read, sometimes ID can be used for a style and sometimes ID can be used for identifying the object.

Also, what will happen if I use both "ID" and "Class" for different styles?

Thanks in advance.

Seaport
 
As I understand it, ID will be replacing NAME when identifing an object. You see this change when writing pages that comply with XHTML standards. Hope that helps a little.

--Caffeinerusher
 
ID can be used as a type of style to assign additional attributes to an html entity. To use it you put a # in front of the class name in your style sheet. Then you use ID="name" (without the #) in the html tag. You should be able to use both a class and an ID on the same tag. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
I understand that ID can be used to refer to a style. But in the following example, it seems that "ID" is used to refer to an object in the document, while "class" is used to refer to a style. Am I right?

Seaport

******************************
<HTML>
<HEAD>
<TITLE>Multiple Layers</TITLE>

<STYLE type=&quot;text/CSS&quot;>

.card {position:absolute; top:75px; left:75px; width=150; background:white}

</STYLE>
<SCRIPT language=&quot;javascript&quot;>

var lastDiv='two';
function showLayer( thisDiv ) {
document.all[lastDiv].style.zIndex = 1;
document.all[thisDiv].style.zIndex = 2;
lastDiv = thisDiv;
}

</SCRIPT>

</HEAD>

<BODY>
<FORM>
<INPUT type=&quot;button&quot; value=&quot;Show Layer 1&quot; onClick=&quot;showLayer('one')&quot;>
<INPUT type=&quot;button&quot; value=&quot;Show Layer 2&quot; onClick=&quot;showLayer('two')&quot;>
</FORM>
<DIV id=&quot;one&quot; class=&quot;card&quot;>
<P>This is layer number 1.</P>
</DIV>
<DIV id=&quot;two&quot; class=&quot;card&quot;>
<P>This is layer number 2.</P>
</DIV>
</BODY>
</HTML>

******************************************
 
Heres the deal:
In CSS, items can be given styles based on a fixed set of rules. The &quot;Cascade&quot; in CSS refers to the way that a style from a parent object is automatically inherited by a child object (ie: it &quot;cascades&quot; to the child and so on.).<BR>
In CSS, you can define what elements are affected by a set of styles in a few ways- you can define a style for a given tag, say, <SELECT>. When the sheet is applied to a page, that style will then change the appreance off all <SELECT> items on the page.
Heres where the ID and class come in. CLASS allows you to define a given style for a group of tags that are not neccesarily of the same type. Lets say you wanted to make every font on the page Verdana(My fav.). You can create a class (Say, .FONT?) and apply it to each item you want to be Verdana.
ID is a specific identifier unique to an item (if the document is properly formatted). Therefor, if you create a CSS specification for ID &quot;Text1&quot;, the only item on the page that will be affected by it it Text1 (Unless you have more than one &quot;Text1&quot;, which currently, browsers are allowing)
In a nutshell, Tag specifications are for all tags f that type, Class Specifications are for a group of items, not neccesarily of the same type, and ID specifications are for a single item only.

Hope I helped :)
V
 
Hi, Vampyre66,

What a wonderful explanation. That is just what I need. Thank you very much.

Seaport
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top