|< Day Day Up >|
6.6 Advanced Styles
Tag and class styles are the most common types of styles, but they aren't the only ones available. There are many other types with various functions and purposes, but Dreamweaver lumps them together under the term advanced styles. To be technically accurate, tag and class styles aren't really styles per se. In CSS-lingo, they're known as different types of selectors.
A CSS selector is an instruction that tells a Web browser what it should apply the CSS formatting rules to. For example, a tag selector tells a browser to apply the formatting to any instance of a particular tag on the page. Thus, an <h1> tag style will apply to all <h1> tags on a page. A class selector, on the other hand, only applies when the browser encounters the class name attached to an element on a page.
Note: For a detailed discussion of selectors, visit www.westciv.com/style%5Fmaster/academy/css_tutorial/ selectors/index.html .
There are a variety of selectors in the CSS arsenal (a few of the most common and useful are mentioned below), but in Dreamweaver you go about creating them all the same way. Start by creating a CSS style, following the instructions on Section 6.2. But when you get to the New CSS Style window (Figure 6-2), instead of selecting the Tag or Class selector type, choose the Advanced option.
With the exception of four "pseudo-classes" (discussed next) in the advanced selector drop-down menu, you must type the selector name in the text field at the top of that window. As described below, each type of advanced selector has a different syntax. (The remainder of the process of creating the style is just like creating a tag or class style [Section 6.2.2], and the process of editing or deleting these styles is also identical.)
When you select Advanced in the New CSS Style menu, Dreamweaver lets you select one of four pseudo-classes from the Selector menu, as shown in Figure 6-12. These four options (a:link, a:active, a:visited, and a:hover) correspond to the types of links described in step 13 on Section 1.3.3.
For example, a:link represents the formatting state of an unvisited link on the page; a:active represents the look of the link as the visitor clicks it; a:visited represents the link's look after the visitor has already been to the linked page; and a:hover describes the look of the link as the mouse moves over it. This last option is particularly useful, since it works like a rollover image, but for text.
To use these pseudo-classes, select each in turn from the Selector menu and set its formatting characteristics (font color, size, underscore, and so on). You must, however, create these pseudo-classes in the order that they appear in the menu.
Using these styles, you could make your link text appear red and underscored before it's clicked, blinking when the mouse moves over it, purple boldface when clicked, and pale pink after it's been visited. (All right, you might never be hired to do Martha Stewart's Web site, but you get the point.)
When you use the Page Properties window to set colors for your links (Section 1.3.3), behind the scenes, Dreamweaver actually creates these pseudo-class styles in the page's internal style sheet.
These link pseudo-classes have one drawback: setting them affects all links on a page. In that respect, they're like tag styles, except they change the different states of a link (clicked, active, and so on).
If you want to use these styles for only certain links on a page, you could do the following: First, create a class style and apply it to each link you wish to have a special appearance. Then create another new style, click the Advanced radio button, and type a.className:link (or :active, or :hover, or whichever state you wish to define) where className is the name of the class you applied to the link. (Alternatively, you could use contextual selectors to achieve the same goal, an advanced technique described on Section 6.6.3.)
For example, say you have a set of five links that act as a navigation bar on the left side of a page. You want each of these text links to have a border and a background color, and you want the background color to change when a visitor moves his mouse over the link. Create a new class style named, say, mainNav, then apply this style to each link (<a> tag). Then create your pseudo-classes for each of the different link states. For example, for the main link state, you could simply name the style a.mainNav: link; the visited link would be a.mainNav:visited, and so on.
An ID is a type of selector that lets you format a unique item on a page. You use an ID style to identify an object (or an area of a page) that appears only once梐 banner, copyright notice, or main navigation bar, for example. IDs are frequently used with CSS-based layouts like those presented in Chapter 8.
An ID style is very similar to a class style, in that you supply the style's name and you apply the style manually. However, while class styles begin with a period (.company, for example), ID styles must begin with the # symbol (#banner, for example). Also, while you can apply a class style many times on a page, an ID style can appear only once per page.
IDs are often assigned to a <div> tag. The <div> tag, which indicates a logical division of a page, is discussed on Section 8.2.
To create an ID style, choose the Advanced option in the New CSS Style window (see Figure 6-2), type the # symbol and then the style name?span class="docEmphasis">#banner, for example. Applying IDs can also be a little tricky, but the easiest method is to use the tag selector to apply an ID to a particular tag as shown in Figure 6-13.
6.6.3 Contextual Selectors (Descendent Selectors)
Tag styles have their drawbacks. While a tag style for the <p> tag makes simple work of formatting every paragraph on a page, it's also indiscriminate. You may not want every paragraph to look the same.
Maybe, for example, you may want to divide a Web page into different sections梐 sidebar and a main content area, for instance, using a smaller size text for paragraphs and headings in the sidebar. You could create two class styles梥idebarText and mainText, for example梐nd then apply them to the appropriate paragraphs in the respective parts of your design (<p class="sidebarText"> for sidebar paragraphs and <p class="mainText"> for body text). But who has that kind of time?
What you really need is a "smart" tag style, one that can adapt to its surroundings, like a chameleon, using the appropriate formatting depending on where it is in the page. Fortunately, CSS offers just such a type of style?span class="docEmphasis">contextual selector.
"Contextual selector" is the name given in the original CSS standards. In the most recent incarnations of these rules, the name has been changed to "descendent selector." But because "contextual selector" is more descriptive, you'll see that term in this book.
A contextual selector lets you limit the application of a style to a certain context. For example, you can create a style that formats a <p> tag to be blue and 9 pixels tall, but only when that tag appears in a sidebar. How does a Web browser know the tag is in a sidebar? You tell it梪sually by using another style.
For example, the Web page shown in Figure 6-14 has a common table-based layout. The gray box on the left side of the page is a single table cell, while the white box to the right is another cell, containing the page's main content. By creating a class called .main and applying it to the <td> tag of the main cell, you set the context for all other HTML placed inside that cell. Now a heading tag (<h1>) that appears inside that cell is within the context of .main.
The Cascading Style Sheets tutorial includes an example of the power of contextual selectors. See Section 6.8.5.
Contextual selectors can reduce the amount of code used in a Web page. They also speed production by eliminating time-consuming formatting chores. In this example, the HTML code that appears within the left table cell is the same as the code in the right-hand cell; only their contexts differ. The sidebar content is contained within a cell with a class style?sidebar梐pplied to it. Thus the <h1>, <p>, and <li> tags are within the context of the .sidebar style.
184.108.40.206 Creating the "Context"
There are a variety of ways that you can define the context of text, images, or other page content. Some are automatic, and some you'll craft yourself:
220.127.116.11 Creating Contextual Selectors
Creating contextual selectors is no more difficult than creating any other type of style. You follow the same process as described on Section 6.2 for creating a style梑ut in the New CSS Style window, select the Advanced option. You then type the contextual selector in the selector box. List the context for the style first, followed by a space and a tag or other selector type. For example:
After you name the contextual selector, click OK in the New CSS Style window. You're ready to start adding the CSS properties that define the formatting of your contextual selector style. In other words, proceed as you normally would when creating any type of style.
|< Day Day Up >|