Jump to content

div class and div id!

Whether you're a seasoned veteran or a struggling beginner, Web Radiance is the web development and web design forum for you. You'll find answers to all your HTML, CSS, SEO, and Programming needs. Pull up a chair and stay awhile.

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

div class and div id! Rate Topic: -----

#1 User is offline   sho88 

  • W.R. Corporal
  • Group: Members
  • Posts: 79
  • Joined: 03-August 06
  • Interests:Web Design and Athletics!

Posted 05 January 2007 - 05:43 AM

Hey guys, I love CSS so much, I don't know why all websites on the Internet don't use CSS to some extent!!

Anyways, I was wondering, what's the different between div id and div class. I mean something like this:

(in .css file)
div.container

(in .html file)
<div class="container">



AND

(in .css file)
div#container

(in .html file)
<div id="container">


what exactly is the difference between the two? :blink: :unsure:

This post has been edited by sho88: 05 January 2007 - 05:43 AM

0

#2 User is offline   Karl Buckland 

  • A.K.A. Sirkent
  • Group: Administrators
  • Posts: 2,145
  • Joined: 04-April 06
  • Gender:Male
  • Location:Kent, UK

Posted 05 January 2007 - 05:56 AM

An id should be given to only one element in your HTML. That id can then be used to identify that element and only that element.

A class on the other hand, can apply to as many elements as you wish.

Edit: (with information brought to light later in this topic)

Ryan Fait also points out that it's possible to use more than one class at a time on one element, but each element can only have one id.

Q. What is the advantage of identifying something through a css property? Why use id instead of class? etc
A. In CSS there isn't a particular advantage (other than knowing when you see the css that there is only one element with that ID). You could argue that this would prevent you or other designers from using that same id on another element, and thus screwing up the design.
The real advantage (as others have mentioned) comes when you use Javascript, as you can easily identify the element in question by using its id.
QUOTE(benbramz @ Aug 17 2007, 07:44 AM) Ive noticed that quite a few people are now adding quotes from the board into their signature. I think its started an new web-radiance craze.. :P
0

#3 User is offline   sho88 

  • W.R. Corporal
  • Group: Members
  • Posts: 79
  • Joined: 03-August 06
  • Interests:Web Design and Athletics!

Posted 05 January 2007 - 07:00 AM

View PostSirkent, on Jan 5 2007, 10:56 AM, said:

An id should be given to only one element in your HTML. That id can then be used to identify that element and only that element.

A class on the other hand, can apply to as many elements as you wish.

As far as I know that's the only difference, but if not then I'm sure someone will correct me!


thankyou!
0

#4 User is offline   James Mitchell 

  • Legendary
  • Group: Administrators
  • Posts: 922
  • Joined: 26-July 06
  • Gender:Male
  • Location:Fort Wayne, IN

Posted 05 January 2007 - 08:51 AM

Sirkent is right and that is a pretty plain answer too, well done.
Posted Image
0

#5 User is offline   Catalyst 

  • Codesmith
  • Group: Administrators
  • Posts: 1,049
  • Joined: 04-April 06
  • Gender:Male
  • Location:San Diego

Posted 05 January 2007 - 02:56 PM

The ID can also be used to identify an element for scripting purposes.
0

#6 User is offline   TJSingleton 

  • Allegedly Legendary
  • Group: Members
  • Posts: 517
  • Joined: 31-July 06
  • Location:Winder, GA

Posted 05 January 2007 - 03:01 PM

it has replaced the name attribute in many cases.
0

#7 User is offline   marcamos 

  • W.R. General
  • Group: Administrators
  • Posts: 2,849
  • Joined: 04-April 06
  • Gender:Male
  • Location:Massachusetts - USA

Posted 05 January 2007 - 04:02 PM

View PostTJSingleton, on Jan 5 2007, 03:01 PM, said:

it has replaced the name attribute in many cases.

I sometimes use the id value of my wrapper div as the target of an anchor at the bottom of a page, linking to "Top of page".
0

#8 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

Posted 05 January 2007 - 04:56 PM

Yep sirknet is right.

You can use the id class for say your header, footer etc (as they only appear once in your page)

But use a class for a h3 element as that may appear more than once on a single page.
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#9 User is offline   moojoo 

  • Argh!
  • Group: Members
  • Posts: 449
  • Joined: 03-August 06
  • Location:Texas

Posted 05 January 2007 - 05:04 PM

View Postsypher, on Jan 5 2007, 09:56 PM, said:

Yep sirknet is right.

You can use the id class for say your header, footer etc (as they only appear once in your page)

But use a class for h3 any element as that may appear more than once on a single page.



:P

This post has been edited by moojoo: 05 January 2007 - 05:04 PM

Its not girly, its web 2.0!
0

#10 User is offline   sypher 

  • the owner3r
  • Group: Administrators
  • Posts: 1,578
  • Joined: 04-April 06
  • Location:North Wales, UK
  • Interests:Art, Boxing, MMA, Graphic Design, Web Design etc. ;)

Posted 05 January 2007 - 05:49 PM

Ah sorry i was just using an example :P but ye any element which appears more than once, alot easier to understand :)
sypher design - North Wales Web Design | Latest Work: - Scala Cinema

CSS - Can't See Sh*t
0

#11 User is offline   BigMike Sidelka 

  • W.R. Sergeant
  • Group: Members
  • Posts: 241
  • Joined: 31-July 06
  • Gender:Male
  • Location:C-Town, Ohio

Posted 06 January 2007 - 03:15 PM

Other than the ability to link to the identified elemenet on the page what other purpose is there to identify an element by its style properties?
0

#12 User is offline   marcamos 

  • W.R. General
  • Group: Administrators
  • Posts: 2,849
  • Joined: 04-April 06
  • Gender:Male
  • Location:Massachusetts - USA

Posted 07 January 2007 - 02:16 AM

View PostNewAge.Mike, on Jan 6 2007, 03:15 PM, said:

Other than the ability to link to the identified elemenet on the page what other purpose is there to identify an element by its style properties?

You should never identify an element by its style, instead try to identify an element by it's content. We've spoken a bit about this already towards the last half of this thread: CSS Selectors.

Here is a quick example of our discussion:

<div id="blueBoldText">Hello and welcome to my site...</div> is bad.

<div id="introParagraph">Hello and welcome to my site...</div> is good.
0

#13 User is offline   BigMike Sidelka 

  • W.R. Sergeant
  • Group: Members
  • Posts: 241
  • Joined: 31-July 06
  • Gender:Male
  • Location:C-Town, Ohio

Posted 07 January 2007 - 02:31 AM

View Postherkalees, on Jan 7 2007, 02:16 AM, said:

You should never identify an element by its style, instead try to identify an element by it's content. We've spoken a bit about this already towards the last half of this thread: CSS Selectors.

Here is a quick example of our discussion:

<div id="blueBoldText">Hello and welcome to my site...</div> is bad.

<div id="introParagraph">Hello and welcome to my site...</div> is good.

but still, why identify by a css class introParagraph? why use id instead of class? like
<div class="introParagraph" name="Intro" title="Intro">Hello and welcome to my site...</div>
0

#14 User is offline   marcamos 

  • W.R. General
  • Group: Administrators
  • Posts: 2,849
  • Joined: 04-April 06
  • Gender:Male
  • Location:Massachusetts - USA

Posted 07 January 2007 - 02:43 AM

Oh, I see what you're asking. When there is only one instance of any given thing, it, semantically, requires an ID. When there is more than one instance of any given thing, it needs a class.

When you get your driver's license, you get an ID (there is only one you). When you go to school, you're in a class called "student" (there is many of you, students).

Back in the world of code, this is a per document rule, not per site.
0

#15 User is offline   BigMike Sidelka 

  • W.R. Sergeant
  • Group: Members
  • Posts: 241
  • Joined: 31-July 06
  • Gender:Male
  • Location:C-Town, Ohio

Posted 07 January 2007 - 03:22 AM

View Postherkalees, on Jan 7 2007, 02:43 AM, said:

Oh, I see what you're asking. When there is only one instance of any given thing, it, semantically, requires an ID. When there is more than one instance of any given thing, it needs a class.

When you get your driver's license, you get an ID (there is only one you). When you go to school, you're in a class called "student" (there is many of you, students).

Back in the world of code, this is a per document rule, not per site.

what im asking is, what is the advantage of identifying something through a css property?
0

#16 User is offline   Karl Buckland 

  • A.K.A. Sirkent
  • Group: Administrators
  • Posts: 2,145
  • Joined: 04-April 06
  • Gender:Male
  • Location:Kent, UK

Posted 07 January 2007 - 05:27 AM

In CSS there isn't a particular advantage (other than knowing when you see the css that there is only one element with that ID). You could argue that this would prevent you or other designers from using that same id on another element, and thus screwing up the design.

The real advantage (as others have mentioned) comes when you use Javascript, as you can easily identify the element in question by using its id.
QUOTE(benbramz @ Aug 17 2007, 07:44 AM) Ive noticed that quite a few people are now adding quotes from the board into their signature. I think its started an new web-radiance craze.. :P
0

#17 User is offline   BigMike Sidelka 

  • W.R. Sergeant
  • Group: Members
  • Posts: 241
  • Joined: 31-July 06
  • Gender:Male
  • Location:C-Town, Ohio

Posted 07 January 2007 - 10:35 AM

View PostSirkent, on Jan 7 2007, 05:27 AM, said:

In CSS there isn't a particular advantage (other than knowing when you see the css that there is only one element with that ID). You could argue that this would prevent you or other designers from using that same id on another element, and thus screwing up the design.

The real advantage (as others have mentioned) comes when you use Javascript, as you can easily identify the element in question by using its id.

ok lol thanks. not sure if i worded my question wierd or somin.
0

#18 User is offline   Ryan Fait 

  • W.R. Sergeant
  • Group: Members
  • Posts: 204
  • Joined: 07-January 07
  • Location:Las Vegas

Posted 07 January 2007 - 06:30 PM

I wrote an article on this a while back. I think the only thing you guys left out is the ability to use more than one class at a time.

<style type="text/css">
	.left {
		float: left;
	}
	.bold p {
		font-weight: bold;
	}
	.half {
		width: 50%;
	}
</style>
<div class="left bold half">
	...
</div>


http://ryanfait.com/...versus-classes/
The commercial front for my freelance web design company.
0

#19 User is offline   marcamos 

  • W.R. General
  • Group: Administrators
  • Posts: 2,849
  • Joined: 04-April 06
  • Gender:Male
  • Location:Massachusetts - USA

Posted 07 January 2007 - 08:24 PM

Wonderful addition Ryan; that is another great ability of classes!
0

#20 User is offline   Spode 

  • W.R. Private
  • Group: Members
  • Posts: 28
  • Joined: 13-November 06

Posted 07 January 2007 - 08:49 PM

View Postherkalees, on Jan 8 2007, 01:24 AM, said:

Wonderful addition Ryan; that is another great ability of classes!


Very True, and something I don't do enough of! I expect I could clean up my CSS if I did.

As far as Ids replacing Names. I'm fairly sure PHP forms still rely on names?
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users