Jump to content

Class/# Vs Id/.


smerny
 Share

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...