Class Selectors
Well let's welcome into the fold a special CSS selector known as a class. A CSS class selector will allow you to put one or more style declarations into a group and give it any name you like so that you can apply it to specific web page components. Let's say, for example, you want to apply a green italic font to only 2 paragraphs on your web page. Using a rule set
p {color: green; font-style: italic;} wouldn't work because it would render all your paragraphs with a green italic font. Not what you want, right? Well here's how to fix that...First of all, you would use a class selector to start your CSS rule set. The syntax you use is simply a "." followed by any name you like (can't start with a number though). Here's an example of a CSS rule set whose class name is "GreenItalic":
.GreenItalic {
color: green;
font-style: italic;
}
This rule set uses the property/value pairs
color: green; and font-style: italic; to apply, you guessed it, green colored text with an italic font style. You would then insert the above into an embedded or external style sheet. Now to get any HTML element to use this class, all we have to do is apply the class attribute and use the name we selected as the value. So, for example, let's apply it to a paragraph tag:
<p class="GreenItalic">It was a dark and stormy night.</p>
And there you have it. A green italic font applied to a paragraph of your choosing.
Below is an example of an embedded style sheet using the above defined
GreenItalic class in first two paragraphs but not the third:Example 1 - AN EMBEDDED STYLE SHEET USING THE CLASS SELECTOR
<html>
<head>
<title></title>
<style type="text/css">
<!--
.GreenItalic {
color: green;
font-style: italic;
}
-->
</style>
</head>
<body>
<p class="GreenItalic">Text rendered in green italic font.</p>
<p class="GreenItalic">Text rendered in green italic font.</p>
<p>Text NOT rendered in green italic font.</p>
</body>
</html>
You may define as many classes as you like so, in fact, you could just chuck out a whole bevy of HTML presentational attributes and create a brand new personalized web language using just some basic HTML elements, the
class attribute and your embedded or external style sheet. But keep in mind that you can use the same CSS class in many different HTML elements (provided the CSS properties being used are applicable to the targetted element). For example, the above defined
GreenItalic class could also be used in an h1 element, a span element or a div element. This makes classes versatile and reusable so remember to economize whenever you can otherwise you'll end up with humongous style sheets. Also be advised that class selectors are case-sensitive. Hence, if you define
GreenItalic as a selector in your style sheet, using class="greenitalic" in your HTML code won't work. It has to be class="GreenItalic"
So would you like to get real picky about which HTML elements you apply your style declarations to? Then you'll want to learn how to use ID selectors...
SEE ALSO:
| <~ BACK | TOP | NEXT ~> |
