Skip navigation
Currently Being Moderated

What type of selector should I use with ccs rules

Feb 13, 2012 8:16 PM

I am new to html and css and looking for a good place that explains when I should use classes, ID's, Tags, compound identifiers. I just started a web page design using mostly classes for the div's and now I see a lot references to using the ID # for container div's. Is there some place that I can find some general guidelines when to use each type. I am looking to build a website with 50+ pages. Thanks

 
Replies
  • Currently Being Moderated
    Feb 13, 2012 9:42 PM   in reply to dvollmer57

    This is a common source of confusion for CSS newbies.

     

    The golden rule is that IDs can only be used once per page. Classes can be used as often as you like.

     

    IDs have greater specificity in CSS calculations than classes.

     

    So, in the event of a rule clash, IDs always trump classes e.g. #header beats .header

     

    You can use this principle to your advantage in your stylesheets.

     

    e.g. name major page components with an ID such as #col1, #col2, #col3

     

    You can then assign classes to elements within those components and use compound selectors to precisely target specific elements of your page using the column ID (#col1 etc) as the starting point for the 'hook' for the CSS.

     

    e.g.

     

    #col1 p = any paragraph in column 1 only

     

    #col2 ul li = any unordered list item in column 2 only

     

    #col3 .header table tr td = any table cell inside an HTML element where class="header" inside column 3 only

     
    |
    Mark as:
  • Currently Being Moderated
    Feb 13, 2012 11:24 PM   in reply to John Waller
     
    |
    Mark as:
  • Currently Being Moderated
    Feb 14, 2012 12:59 AM   in reply to dvollmer57

    Hi dvollmer57

     

    http://www.w3schools.com/css/

    This link will help you to learn the basic of the css.

    "Try it youself" button is Awesome to experiment with the diffrent css

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points