Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

CSS Class and ID

  • 08-09-2011 8:51pm
    #1
    Registered Users, Registered Users 2 Posts: 9,844 ✭✭✭


    Hiya,

    I am looking for a logical and simple explanation and example of the different class selectors withing CSS. I know what they do (to a certain extent) but I find that when I try to explain them to others I don't do a very good job of it.

    Anybody have a simple explanation for when each is used?

    Cheers!


Comments

  • Registered Users, Registered Users 2 Posts: 435 ✭✭doopa


    w3schools definition explains it well for me:
    http://www.w3schools.com/css/css_id_class.asp


  • Registered Users, Registered Users 2 Posts: 9,844 ✭✭✭py2006


    ID:

    { #main: color:blue; }

    [HTML]<div id="main"> This is blue text </div>[/HTML]

    Class

    .firstparagraph {color: red; }

    [HTML] <p class="firstparagraph"> Howya doin </p>[/HTML]

    Tag

    p { background-color: black;}

    [HTML] <p> This paragraph has a black background </p>[/HTML]


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    py2006 wrote: »
    Hiya,

    I am looking for a logical and simple explanation and example of the different class selectors withing CSS. I know what they do (to a certain extent) but I find that when I try to explain them to others I don't do a very good job of it.

    Anybody have a simple explanation for when each is used?

    Cheers!

    ID (hash notation prefix)
    If a university lecturer wants to call a particular pupil, they can call them by their name, but (since 2 people could have the same name) they'll call them by their ID.

    CLASS (dot notation prefix)
    If the lecturer wants to call the class (a group of people/elements) then they'll use the class, and therefore that group of multiple elements to be acted on in unison.

    TAG (no notation prefix)
    If the lecturer wants to call "all boys" or "all girls" they'll use the tag, again allowing multiple elements to be acted on in unison.


  • Registered Users, Registered Users 2 Posts: 2,120 ✭✭✭p


    What the others said. I'd add:

    IDs are things that are unique and only appear once on a page. e.g. #header or #footer, whereas classes are something that appear many times. e.g. .product or .news-item

    Overall though, I always tell beginners to just uses Classes. There's very little advantage(if any) in using IDs, except for your own organisation.


Advertisement