|
|
||||||||||||||
|
|
Advanced Methods | Classes |
|
|
|
||||||||||
|
Classes So far we've only looked at using css rules to customize HTML tags. We've used rules to create new default versions of tags and to modify tag behavior on a case-by-case basis. But, in fact, we can do more than that with style sheets. CSS also lets us define classes. On the one hand, these can be used to customize existing tags on a "intermediate" basis (providing a middle ground between the approaches we've seen thus far that either alter all occurrences or only a single instance of a tag). But classes can also be used in such a way that we can, effectively, define our own "HTML environments".A rule for a class is similar in form to the ones we've already seen for tags. The only difference is that the rule doesn't begin with the name of an HTML tag. Instead it begins with a period followed immediately by a name of our own choosing. The period identifies this as a rule for a class, not a tag; the name after the period is whatever we choose as the name for the new class we are creating.
Now let's look at some examples of classes and how they can be used.
Using a Class to Modify an HTML Tag
As a simple example, let's define the following two classes:
We now have at our disposal three different versions of the <h3> tag: 1) a version that's used if we specify class="blueslant", 2) a version that's used if we specify class="orangeline", and 3) the default version that's used if don't specify a class (in the present case, the new default that we created when we reconfigured the tag with a css rule for h3). If we place all three of these rules in an external file, we can use any of the definitons on any HTML page linked to to the external file. This was a very simple example, but we can similarly use classes to define alternate versions of other tags.
Classes and IDs
In many ways, classes and IDs work alike. In fact, here's our previous example modified to use IDs instead of classes: Example 10. The major difference is that a given ID is often used only once on any single HTML page. In this way, each tag can be "tagged with an identity". This can be useful when implementing some of the more advanced features of css, for example to control the positioning of elements on a page.
Creating Custom HTML Environments
The DIV tag is almost a blank slate: it doesn't have any visible effect except to mark off a rectangular area of a page and put a blank line before and after it. SPAN is a complete tabula rasa and has absolutely no formatting characteristics of its own. Classes and IDs only take effect when they are applied to an HTML tag; because of the "blankness" of DIV and SPAN, you can think of these two tags as empty containers that let you bring in a customized set of definitions whenever you want. This is not to say that these tags don't already have a set of default attributes that you can specify. In fact, they do, including attributes that allow you to control the exact positioning on a page and the "visibility properties" of items contained inside DIV and SPAN tags. These attributes, when integrated with Javascript, make it possible to create animations and produce truly dynamic HTML elements on a page. But these topics can be considerably more complicated than what we've described for css here. (Stay tuned for future updates to webTeacher for an introduction to some of these more advanced capabilities of style sheets.)
|
||||||||||||||
|
|
||||||||||||||