Class:
The class selector allows you to style items within the same (X)HTML element differently. Similiar to what I mentioned in the introduction about inline styles. Except with classes the style can be overwritten by changing out stylesheets. You can use the same class selector again and again within an (X)HTML file.
Ex:
p { font-size: small; color: #333333 }
“Sentence” to green bold text, while leaving the rest of the sentence untouched. I would do the following to my (X) HTML file.
Ex:
To put it more simply, this
sentence you are reading is styled in my CSS file by the following.
Then in my CSS file I would add this style selector:
Ex:
.greenboldtext { font-size: small; color: #008080; font-weight: bold; }
The final result would look like the following:
To put it more simply, this sentence you are reading is styled in my CSS file by the following.
Please note that a class selector begins with a (.) period. The reason I named it “greenboldtext” is for example purposes, you can name it whatever you want. Though I do encourage you to use selector names that are descriptive. You can reuse the “greenboldtext” class as many times as you want.
ID:
IDs are similar to classes, except once a specific id has been declared it cannot be used again within the same (X)HTML file.
I generally use IDs to style the layout elements of a page that will only be needed once, whereas I use classes to style text and such that may be declared multiple times.
The main container for this page is defined by the following.
Ex:
Everything within my document is inside this division.
I have chosen the id selector for the “container” division over a class, because I only need to use it one time within this file.
Then in my CSS file I have the following:
Ex:
#container{ width: 80%; margin: auto; padding: 20px; border: 1px solid #666; background: #ffffff; }
Leave A Comment