Cascading Style Sheets
Cascading style sheets, or CSS, can make your life as a Web designer much simpler. CSS allows far greater control over the objects in your document and can adjust attributes that aren't accessible from standard HTML.
Because of their complexity, editing a CSS manually isn't much fun. Understanding the syntax isn't difficult, but remembering all the attributes that you can set is. For example, here is a simple style sheet that alters the <td> and <th> tags:
<style type="text/css"> <!-- td { font-family: Arial, Helvetica, sans-serif; font-size: 10pt; color: #0000CC} th { font-family: Arial, Helvetica, sans-serif; font-size: 12pt; font-weight: bold; color: #660000} --> </style>
This style sheet sets the font rendering preference for <td> elements to Arial, Helvetica, sans serif and also assigns a default size and color. Likewise, the <th> tag is also redefined to the same font face, but a larger font size and a different color. Any page that contains this style sheet or is linked to a style sheet containing these attributes will inherit the properties of these tags.
NOTE
When you see a list of fonts used in style sheets or within a font tag, it indicates the preferred font rendering order. If the first font in the list is available, it is used, if not, the browser moves to the second, and so on.
The Dreamweaver MX interface makes defining tag attributes very simplejust a few points and clicks and you're set.
Working with Style Sheets
The CSS Styles panel, found under the Window menu or in the Design panel group, is the one-stop shop for creating, modifying, and linking to style sheets. Go ahead and open the CSS Styles panel now. It is shown in Figure 3.14.
Figure 3.14 CSS Styles can be applied across a single page or an entire Web site.
The CSS panel starts out empty. If you have an existing style sheet, you can immediately attach it to your document by clicking the first icon in the lower-right of the style sheet panel (rectangle with chain line). You'll be prompted to choose the style sheet within your site directory.
To create a new style, click the New icon in the lower-right corner (the pad of paper with "+" symbol), or choose New Style from the panel's pop-up menu in the upper-right corner. You should now be at the New CSS Style dialog box, shown in Figure 3.15.
Figure 3.15 Choose the type of style you want to define and click OK.
Three types of styles you can create are as follows:
The most powerful of these tools is the ability to redefine an HTML tagsuddenly your document can inherit a look without having to change any of the HTML.
You can create your styles in two placesan external file, or in the document that you are working in. If you want to add the style to a new style sheet, choose New Style Sheet File from the pop-up menu in the Define in section of the style creation screen. If you've already attached a style sheet to your document, you can select that sheet instead of creating a new one.
If you prefer to keep the styles defined directly within a document, make sure that This Document Only is selected.
Creating a Style
To create your first style, try redefining a tag like <b>. Click the Redefine HTML Tag button and choose <b> as the tag name. Click OK to continue:
- Eight categories of attributes can be set for each style. The most common one is Type, which controls font attributes. If the type category is not selected, choose it now. The CSS Style definition dialog box is shown in Figure 3.16.
Figure 3.16 You can control every aspect of your style's font definition here.
-
Set a sans serif font face, forced to a 72 pixel size, with a bold weight. If you don't want to follow this example exactly, you can choose any of the attributes you prefer to use, including color, style, and line height. In fact, the number of attributes is quite overwhelming. Because of the volume of options, I'm including a short reference immediately following this simple tutorial.
-
When you've finished deciding what your bold tag is going to look like, click the OK button.
-
Before trying the style, go through the same process again; except this time define a custom style for yourself.
-
The process of defining a custom style is exactly the same, except for choosing Make Custom Style (class) instead of Redefine Existing Tag. Name the new custom style .heading, and set it as you did the previous font. I choose a size of 128 pixels and an oblique style.
-
Close the style definition window when finished.
After your two styles are created, you should be able to look at the HTML source code for your document and see the style definitions in the <head> section:
<style type="text/css"> <!-- b { font-family: Arial, Helvetica, sans-serif; font-size: 72px; line-height: normal; font-weight: bold} .heading { font-family: Arial, Helvetica, sans-serif; font-size: 128px; font-style: oblique; font-weight: bold } --> </style>
Open the style panel again and take a look at the listed CSS styles that are available. Notice that only superbold is listed. The reason for this is that the only listed styles are custom styles. Styles based on redefined HTML tags are applied automatically and don't need to be listed.
Applying a Style
To apply a CSS-based style that redefines an existing HTML tag, you don't need to do anything except use that tag in your document. Try using the HTML tag you redefined in the previous example. If you followed the example and defined the <b> tag, just enter some text in the design view and choose Style menu under Text.
Notice that all of a sudden the bold tag doesn't behave the way it used to. Anytime you use it in the document where it has been redefined, it will appear using the attributes that you set.
To set text to a custom style, you can use either the CSS Styles panel or the CSS Styles menu under Text. Simply select what you want to apply a style to, and then click the style in the panel or choose it from the appropriate menu. If the auto-apply check box in the lower-right corner of the panel is not checked, you'll need to click the Apply button in the panel before the style will be applied. The None style will remove any applied styles from the current selection.
For precise control over where the style is applied, you can use the pop-up menu at the top of the CSS Style panel to choose any of the containing tags of the current selection.
TIP
If you've set up styles that aren't appearing correctly in your document design view, it might be that Dreamweaver MX doesn't support rendering that particular style attribute. Check the CSS definition screens for an * by your missing attribute. If you see an *, you'll need to preview your document in a browser to see that style attribute rendered correctly.
Editing and Linking to External Style Sheets
Defining styles within a document is perfectly fine, but doesn't make the best use of style sheets. Instead of redefining styles each time they need to be used, you can create a master style sheet that is linked to by every page that needs it. This is a more efficient means of keeping your site consistent. The most direct way to link to a style sheet is using the Attach Style Sheet icon in the lower-right corner of the CSS panel, as mentioned earlier. This, however, doesn't give you the greatest amount of control over the style sheetsyou can use the Edit Style Sheet panel menu to manage linked styles in your document.
To create an external style sheet, use the panel menu, or choose CSS Styles, Edit Style Sheet from the Text menu. The Style Sheet editor seen in Figure 3.17 should appear.
Figure 3.17 You can edit and link to style sheets from this window.
From this view, you can create new styles or duplicate, edit, and remove selected styles. The item that we're mostly interested in right now is the Link button. Go ahead and click it now. You'll be prompted for a file or URL to link to and given the option to link or import the targeted style sheet. The Link External Style Sheet dialog box is shown in Figure 3.18.
Figure 3.18 Link to an existing style sheet or provide a new name to create a new file.
If you have a style sheet created, enter its location in the URL. If not, supply a new filename in the URL field and click OK. This creates a new empty style sheet.
NOTE
The Style Sheet import function only works on Internet Explorer. You should always link to external style sheets so that you maintain the greatest cross-platform compatibility.
The Edit Style Sheet dialog box will reappear listed with the linked-to style sheet. To start working with the new style sheet file, select it in the list and click Edit. An empty copy of the edit window will appear. This window references the linked style sheet. Here you can add all the styles you prefer. They'll be saved to the external file, but will be accessible in your current document. On subsequent Web pages, you can simply link to this newly established style sheet and instantly have access to all your styles.
TIP
Dreamweaver MX does not currently offer the ability to copy styles between sheets. If you want to merge two existing style sheets, open them in a text editor and copy the contents from one into the other.
A reference to the supported Dreamweaver MX CSS elements is included in Appendix C.
A Final Word on Styles
When you start using CSS, don't get carried away. CSS is a great idea and a fantastic tool, but it does not always work the way you would think. There are inconsistencies between Internet Explorer and Netscape, and between the same browsers on different platforms.
In my experiences with CSS, I've found that it is very useful for maintaining consistent text attributes across pages and keeping sites as cross-platform as possible. Don't expect things to look exactly as they do in the Dreamweaver MX design view, however. Be sure to preview in your browser frequently and have more than one browser platform to use in your tests.