Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 8 of 8

Thread: CSS question

  1. #1
    Junior Member
    Join Date
    Apr 2004
    Location
    London,England
    Posts
    20
    Member #
    5614
    css question, what is the difference between <h2 class= and <h2 id.

    Can someone explain the difference between id and class please

    To me all i see the differnce is that one is a # and one is a . in css

  2.  

  3. #2
    Senior Member justlivyalife's Avatar
    Join Date
    Jul 2003
    Location
    Birmingham, UK
    Posts
    2,871
    Member #
    2374
    AFAIK, id's can only be used once in a HTML page, though classes can be used throughout. My CSS uses dots instead of hashes, and works perfectly well with the use of 'class'
    justlivyalife - The future depends on what we do in the present. (Mahatma Gandhi)
    WDF Resources: WDF Rules
    Non-WDF: JavascriptSource | Dynamic Drive | phpBB | HTML-Kit | Winamp | Download Firefox | Morguefile

  4. #3
    Senior Member teal's Avatar
    Join Date
    Dec 2003
    Posts
    247
    Member #
    4319
    class and id are almost interchangeable. ids are denoted with a hash (#) then the name of the element (property #thenameyouputintheid), while classes are denoted with a . (property.thenameyouputintheclass). I personally think classes are easier to code, but don't know if there is any major diffrence between them. I don't know what livyalife means by "id's can only be used once in a HTML page", maybe someone can explain that. . .
    Whoth steps forth to mock the teal? Whom couldst say a color superior!

    Proclamith me, it cannot be done.

  5. #4
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    Yes, as stated, there is a major difference in that ids must be unique on a page, classes do not. ids are also used by Javascript when referencing page elements.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  6. #5
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    id's can only be used once, i.e. this would be wrong
    Code:
    <div id="content">
    Some content
    </div>
    <div id="content">
    Ahh this is wrong!!!
    </div>
    whereas this would be correct.
    Code:
    <div class="content">
    Some content
    </div>
    <div class="content">
    Yay! This is right!
    </div>
    id's are meant for positioning elements, such as navigation menus, headers, etc while styles are used for styling text, formatting paragraphs similarly, etc.
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site

  7. #6
    Senior Member Maverick's Avatar
    Join Date
    Sep 2003
    Location
    Hoboken, NJ
    Posts
    351
    Member #
    3253
    The major difference between ID's and Classes:

    While both are used for basically the same purpose, to give individual page elements a name by which they can be referred to in your CSS, they have one big difference. While a specific class name can be used as many times as you want in a single html document, a specific id name can be used once and ONLY once with the document. So for example, we make a class called "someclass", this class can be used to define any amount of elements in your html. Now say we create an id called "someid", this specific id can only be used once within your document.

    Although you can use only classes to define elements in your html, I like to use a combination of both, using id's for certain element that only occur once in my page, such as a container div or something, while I use classes for elements that occur multiple times, such as special navigation link.

    And yeah, like teal mentioned, classes use a period before their name in the CSS, while id's use a pound sign (#), this is trivial compared to their main difference.


    EDIT: Woah, I guess filburt and Brak must've posted while I was still writing my response. Oh well, I hope this hammers the point home.
    www.stevenspoker.com
    "You can't lose what you don't put in the middle, ... but you can't win much either." -Mike McDermott (Rounders)

  8. #7
    Senior Member glyakk's Avatar
    Join Date
    Nov 2003
    Location
    USA
    Posts
    1,263
    Member #
    3828
    Liked
    6 times
    Can i take one more swing at it? lol

    EDIT: actually all above examples are 100% correct

    But there is a little more involved with classes and id's. The ID attribute sometimes will get confused with the name attribute so things can get squirrly with things like Javascript if you dont take care to name your ID's correctly. Classes on the otherhand are almost exclusive to CSS so not as much care has to be taken on naming. Also think of ID as an unique person and class as a persons race. There is only one of you(ID) but many latin/causian/asian/african/etc..(class) people. Because of that when the word asian carries certin 'properties' that you might associate. That is class. But if you said somebodys name that might carry a whole other set of properties.. you follw me? Also you can have an id and class defined in the same element.

    One more this that differenterates classes and id's is that id carry more weight then classes. So if you had a class and id defined for an element and they both defined the same property such as 'background' the ID's decleration would overrul the class decleration.


  9. #8
    Senior Member Brak's Avatar
    Join Date
    Apr 2003
    Location
    San Francisco, CA
    Posts
    3,413
    Member #
    1217
    Liked
    2 times
    Excellent metaphor glyakk (races & people)
    Kyle Neath: Rockstar extraordinare
    The blog | The poetry site | The Spore site


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:27 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com