class selectorsExplanationThe control over page design that HTML element selectors give you is powerful. It's great to be able to change every paragraph, but what if you only want to change a specific paragraph, or a few related paragraphs? With HTML, you would add appearance-based mark up to just the text you want changed. With the style sheet approach that is a no-no, so how can we change just some text we want changed? First let's think about why you might want a particular paragraph to look different from other paragraphs. It's probably because the content of that paragraph is in some way different from other paragraphs on the page. Think of an FAQ page, full of questions and answers. With old appearance-based markup, you might have marked up each question with the Now, if there was an HTML element In the HTML you give each of the paragraphs that contains a question a class attribute with the value say "question". You'd probably also want to do the same thing with answers. There is a detailed explanation of how to do this below. It is also covered comprehensively in our self-paced course HTML and XHTML for CSS. With cascading style sheets you can create selectors that are associated with a specific type or class of element. In the example above, we would identify two classes of paragraph - question and answer. We would then create two statements in our style sheet, one which affects only paragraphs of class question, and one which only affects paragraphs of class answer. In short, a class selector selects any element of a given class. SyntaxThere are two forms of the class selector. The first type of class selector selects any kind of element of a particular class (so it will select both paragraphs and tables of that class, as well as any other element with the same class.) In a style sheet, the form of this type of class selector is simply In the Question and Answer example above, we would create two class selectors like this: .question {font-weight:bold} .answer {font-weight:400} The first of these selectors selects any element on a page with a class of "question". The second selects any element on a page with a class of "answer". The second type of class selector selects only a specific type of element of that class - for instance, it will select only paragraphs of that class, not tables or any other element of that class. The form of this type of selector is In the question and answer example above, we would create paragraph specific class selectors like this: p.question {font-weight:bold} p.answer {font-weight:400} The first of these selectors only selects paragraphs with a class of "question". It does not select any other kind of element with a class of "question", nor does it select any paragraphs other than those of class "question". The second only selects paragraphs of class "answer", not any other paragraphs, nor any other elements of class "answer". Note that a class name should comprise only alpha numeric characters, and hyphens. They cannot include underscores and other characters, nor spaces. A class cannot begin with a numeral. UseWe saw in the explanation above that we use class selectors to give specific parts of a page a specific appearance. In a tutorial, your explanations might have a different appearance from procedures. In a cookbook, a list of ingredients would have a different appearance from the instructions for cooking. In the first case, we would create class selectors For the cookbook, we would have class selectors All that remains is to answer the nagging question, "what do you mean by an element of a given class?" What exactly is a paragraph of class "explanation"? A very good question. HTML 4.0, the latest W3C recommendation, or HTML standard, introduced a new attribute for HTML elements, the class attribute. The format for this attribute is as follows. In your HTML document, to create a paragraph of class "instruction", you create a paragraph as usual, using the Our HTML 4.0 and XHTML for CSS includes a detailed, step by step lesson on how and why to use class selectors in your web pages. Class selectors really are one of the most difficult ideas in style sheets to get your head around. If you have made it this far, and have a feel for
If you are still struggling a little, perhaps you should move on without worrying too much, and come back to this section later. You will get it. Browser supportDetailed browser support information for this feature can be found in the Westciv CSS Guide, or in our CSS Browser Support Table. next: ID selectors(C)1997-2001 Western Civilisation Pty. Ltd. |