Jump to content

What's the difference

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
24 replies to this topic

#1
r0adrunn

r0adrunn

    Newbie

  • Members
  • PipPip
  • 11 posts
Hello,

I have been making websites for a while now using tables. Now I am trying to get away from that by teaching myself css layouts. I was wondering and I looked everywhere to know the difference about a minor detail. I couldn't find an explaination or maybe there is no difference; but it is bothering me. For example:

.content{


}

versus

#content{

}

Edited by Jordan, 12 June 2008 - 04:38 PM.
Added code tags

-r0adrunn

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
One is an ID, the other is a Class. An ID (I believe with the .) is supposed to be unique on each page. A class is OK to reuse.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Guest_Jordan_*

Guest_Jordan_*
  • Guests
I think you have them backwards Winged.

# = ID
. = Class

Edited by Jordan, 12 June 2008 - 04:37 PM.
Moved to correct forum


#4
r0adrunn

r0adrunn

    Newbie

  • Members
  • PipPip
  • 11 posts
Cool thanks! So whats an example using an ID to make something unique on a page versus another page? Sorry if these sound like simple minded questions and silly to you, but it would be very helpful to me.
-r0adrunn

#5
v0id

v0id

    Retired

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,936 posts
It simply means that you can only use an ID once in the whole document. So if you've a div-tag with the ID, simpleStyle, in the beginning of your document (or wherever you place it)
<div id="simpleStyle">Stuff here...</div>
Then you can't have another one later in the document. The id="simpleStyle" must only occur once! You can still use other names for ID's, though, so having two ID's but with different names works perfectly, like the following shows.
<div id="simpleStyle">Stuff here...</div>
...
<div id="anotherStyle">Some other stuff here...</div>


Note that if you're sitting making some code using the same ID's twice, it will work in the browser, but if you try it in a validator it will fail to pass.

#6
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Here is an example with CSS:

<html>
<head>
<style>

p#exampleID1 { background-color: blue; } 
p.exampleClass { text-transform: uppercase; } 

</style>
</head>
<body>
<p id="exampleID1">This paragraph has an ID name of "exampleID1" and has a blue CSS defined background.</p>
<p class="exampleClass">This paragraph has class of "exampleClass" and has had its text transformed to uppercase letters.</p>
</body>
</html>


#7
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Your example does not highlight the most important fact of a class - that it can be reused. R0adrunn, see this:
<html>
<head>
<style>

p#exampleID1 { background-color: blue; } 
p.exampleClass { text-transform: uppercase; } 

</style>
</head>
<body>
<p id="exampleID1">This paragraph has an ID name of "exampleID1" and has a blue CSS defined background.</p>
<p class="exampleClass">This paragraph has class of "exampleClass" and has had its text transformed to uppercase letters.</p>
<p class="exampleClass">This is another paragraph - it is also in uppercase, as it is tied to a class, which can be used across multiple elements.</p>
</body>
</html>
You see the extra line? A class can be used on more than element - but an ID can only refer to one specific element.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Good point Xav.

#9
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
All my points are good. ;)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#10
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
I always thought that it was best to use id's with JavaScript like:


<script language="javascript">

document.getElementById("footer").innerHTML  = i;

</script>

<span id="footer"></span>


But with CSS you can reuse a class where as an ID, i don't think you can but I haven't used ID's much with CSS.

#11
TcM

TcM

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 11,147 posts
I think you can use an ID more than once, but if you try to validate it, it will fail validation.

#12
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Yes, some templates actually use IDs instead of classes (but they're not very good templates). My original template assigned the same IDs to every item in the nav menu, instead of using classes. Web browsers will display it correctly, but it's not good code and should be replaced with classes.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums