Jump to content

Class/# Vs Id/.


smerny

Recommended Posts

Can someone explain the differences between using class and # in a style sheet versus using id and . in a style sheet? What each should be used for and how they should be used differently.Like <div id="something"> and #something{} compared to<div class="something> and .something{}

Link to comment
Share on other sites

First, the hash mark (#) goes with ID's and the full-stop (.) goes with classes.Both are used to identify html elements on your page for the styling to be used on those elements.Each ID should only appear once on your page whereas the class can appear multiple times.ID's are "more specific", meaning they will over-rule a class. An element identified with an ID and a class and with the style rule applied to the ID and Class will be stronger still. Read the w3c specifications on specificity to understand rules about complex relationships.Typically, I would use ID's to identify the unique structural elements of a page and classes to identify sub-elements inside the ID'd elements.Hope this helps.

Link to comment
Share on other sites

A good analogy here would be forum users name and groups.A user name is unique to each individual, there can't be 2 or more people with the exact same name or that would confuse the system; This is like ID.But several people can be in the same group like "Member" "Admin" "Moderator" etc; this is like class.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...