Web Design Stuff
HTML Tutorials    CSS Tutorials    Web Hosting   Resources
Create a Web Page 101
Making Web Pages Intro What is a Web Page? Why Make a Web Page? The History of HTML Learn HTML or XHTML?
Basic HTML
Basic HTML Tutorials Basic HTML Necessities How to Make a Web Page How to Edit a Web Page The Basics of HTML Tags Basic HTML Page Structure HTML Attributes
HTML Font Codes
HTML Font Codes Intro HTML Font Color Codes HTML Font Size Codes HTML Font Style Codes HTML Bold/Italic Codes Combining Font Codes
Formatting Text
Formatting Text Intro Making Paragraphs Miscellaneous Formatting Headings & Subheadings Creating Hyperlinks
Using Graphics
Using Graphics on the Web Add Graphics to Your Pages Graphics and Accessibility How to Align Graphics Page Color & Background Graphics as Hyperlinks Horizontal Rules
Creating Tables
HTML Tables Tutorials HTML Table Fundamentals Background & Border Color Table Frames & Rules Table Width and Alignment Cells 1 -Space & Alignment Cells 2 -Row Column Span Cells 3 -Width & Height
Making Lists
HTML Lists Tutorials Bulleted Lists Numbered Lists Definition Lists
HTML Frames
HTML Frames Tutorials Using Frames for Layout Advanced Frame Layouts Putting Hyperlinks in Frames Frame Border Width Color, Margin and Control Problems with Frames SmartFrames: A Solution SSI: An Alternative to Frames
Web Page Forms
Making Feedback Forms A Simple Feedback Form Installing NMS FormMail Debugging Your Setup My Web Host is Out to Lunch User Input Components Text Fields Checkboxes & Radio Buttons Dropdown Menus Push Buttons Layout and Presentation
Basic CSS
Basic CSS Tutorials What is CSS? Why You Should Use CSS How to Use CSS Inline Styles Embedded Style Sheets External Style Sheets Class Selectors ID Selectors Combining Selectors
CSS Properties
CSS Properties Intro Font Styles Width, Height & Spacing Borders Backgrounds Position Float & Alignment Hyperlinks
All About Web Hosting
Hosting Your Own Website What is a Web Host? Your Website's Home Page Building a Website Offline About Free Web Hosting Best Free Web Hosting Commercial Web Hosting How to Get a Domain Name Ecommerce Web Hosting Web Hosting Terminology
Free Web Design Tools
Best Free Website Tools Best Free Text Editors Best Free Graphics Editors Free Website Analysis Tools
Setting Up HTML Kit
HTML Kit Introduction How to install HTML Kit Screenshot Breakdown Basic Configuration Overall Appearance Shortcuts and Startup Editing Window Customizing Toolbars Using the Favorites Tab Making a New Actions Bar Odds and Ends
Free Templates
Free Website Templates Two Column Fixed Width Three Column Liquid Layout Miscellaneous Templates Dynamic Menu Effects Two Column Experimental Terms of Use About These Templates
Website Templates Help
Getting Started Template Zip File Download How to Edit Your Template What to Edit in the HTML How to Add Your Logo Making a Website
Web Design Tips
Web Design Basics Tables vs. Tableless Using Tables for Layout Example Table Layouts World's Crappiest Web Page
Twitter Backgrounds
Twitter Backgrounds Intro Cool Twitter Backgrounds Cool Twitter Backgrounds 2 Plain Twitter Backgrounds Dark Twitter Backgrounds Best Twitter Backgrounds Cute Twitter Backgrounds Music Twitter Backgrounds Music Twitter Backgrounds 2 Twitter Backgrounds 101 TERMS OF USE
All About Web Browsers
What is a Web Browser? Mozilla Firefox Internet Explorer Opera How to Set Up Firefox Top 5 Firefox Extensions
 
Contact
Post Some Feedback Send Me An Email Iron Spider Blog About Iron Spider... Site Conventions
 
 
 

 

Class Selectors

 
Now CSS wouldn't be much good if you could only apply styles to ALL the heading tags or ALL the paragraph tags on a web page, now would it? What you really want to do is select which web page components to target while leaving others alone, right?

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:
  • What is CSS?
  • How to use CSS - An Overview
  • Inline styles
  • Embedded Style Sheets
  • CSS Properties




Best Free Stuff
for webmasters

Free Text Editors
Free Graphics Editors
Website Analysis Tools
Free Website Templates

See also:

Best Free Web Hosting
How to Make a Web Page

 

If you need a .COM web address, you can get one quick and easy at...

www.GoDaddy.com
<~ BACK TOP NEXT ~>
 
HTML Tutorials |  CSS Tutorials |  Web Hosting |  Domain Names |  Web Design Resources
Iron Spider © Copyright 2004-2011 Robert Darrell