Website Design - CSS Classes Vs Id's

As a beginner using CSS I had trouble understandingone with id="p_1" and a different p tag with class
the difference between classes and ids. Both are="p_arial" the css would be:p.p_arial {"arial";}
used as an identifier of an element, and technically#p_1 { color: red; } /* makes the font color red */
you can get the exact same affect using either. TheCombining Classes and Ids
difference is fairly simple, an id is unique, it is used forNow in this previous example we have two p tags
an individual element of a webpage. A class howeverone that uses a class to make the font Arial and one
is not unique; multiple elements in a webpage canthat uses an id that specifies red text. What if we
share a class and 'inherit' (gain) that classeswanted the p with the red text to also be Arial? This
specifications.can be done by placing both the id="p_1" and the
Classesclass="p_arial". This p tag will inherit the properties of
If you're not at the point where you're planning theseboth the class and the id. If there is a conflict
things in advance you'll know when to use a classbetween the class and the id, i.e. one says
instead of an id when you find yourself repeatingfont-color:"blue" and the other say font-color:"red",
yourself in your CSS code. For example let's say youthe id will always override the class. This is called
have a few divs on your page and you want thespecificity, and an id is the most specific of any CSS
text inside the page to be a certain size and font.selector.
Instead of doing "" 5 times for each paragraph,It is possible to have multiple classes, this is done by
instead you can create a class. This would lookleaving a white space between class names i.e.
something like:p.class_name {"arial";}class="p_red p_black" if the CSS code is as
Anywhere you have a p tag you sayfollows:p.p_red{color: "red";}p.p_black{color: "black";}
class="class_name" its font will use arial.The last class will override the first class. So the
When you become a CSS master you'll plan all ofparagraphs font color will end up being black in this
these things ahead of time, but until that point justexample.
be aware of when you find yourself duplicating codeOne last way to specify elements
in your CSS files for similar elements. Using classes isBesides specifying by Id and Class CSS can tell a
useful because not only does it reduce the amountdocument that all of an HTML element should be
of code you have to write, but it can keep your CSSstyled a certain way. This is the most general way to
files significantly smaller which can increase the loadspecify styles and is overridden by both classes and
time of your pages.ids. This is done by specifying in the css file the
IDsHTML element tag name. This should make sense
So if classes are for common CSS specificationsbecause it's like you are specifying a class without a
across multiple elements, when to use ids is prettyclass. So to make all of the paragraphs in a document
straight forward. Anytime you want to style ause Arial font type you would use:p {"arial";}
specific element you would use an ID. This is prettyAnother example is if you want to change the font
common when you're doing absolute positioning, orsize, family, color etc.. in an entire document you can
nuances like bolding something or making it areference the body tag:
different color etc. Ids are useful if you're in the habitBody{color:black;"arial"; font-size:.6em;}
of doing inline styles. Modern web-design dictates thatReview
you keep your relative programming languagesThere are three methods to style elements in CSS
separate, so inline styles are frowned upon, insteadusing an element, using a class, and using an ID. An Id
use an id.overrides Classes and Classes override HTML
To do the same thing as above, but make a specificelements. Using classes rather than Ids can minimize
paragraph have arial text we would usefile size and speed up web page load times. Ids
#id_name{"arial";}should be used to style/position individual HTML
In the p tag we would then have id="id_name".elements.
Difference in syntaxOnce you have a pretty good understanding of
Class and Id have slightly different syntax, but theythese concepts you should be able to plan your web
are fairly straight forward: a class is referenced by itspages in advance before you start coding the HTML.
HTML element i.e. img, p, hr, h, div, span, etc..Being able to create all of your classes beforehand
whereas an id is unique so it does not need to bewill speed up your development process as you'll see
associated to an HTML element and is specified withhow things are suppose to look from the beginning
the #.rather than trying to make it look the way you want
So given an html page where there are two p tagsas you go along.