HTML The Definitive Guide (98 page)

Read HTML The Definitive Guide Online

Authors: Chuck Musciano Bill Kennedy

BOOK: HTML The Definitive Guide
8.84Mb size Format: txt, pdf, ePub

Chapter 9

Cascading Style Sheets

 

9.5 Applying Styles to Documents

There are several issues you should consider before, during, and after you use styles in your HTML

documents and document collections. The first, overarching issue is whether to use them at all.

Frankly, few of the style effects are unique; most can be achieved, albeit less easily and with much less consistency, via the physical and content-based style tags like and and the various tag attributes like color and background.

9.5.1 To Style or Not to Style

We think CSS is a winner, not only over just JavaScript-based ones, but also for the convenience and effectiveness of your HTML documents. Currently however, CSS style sheets are a bittersweet solution; they can create more work than they are worth. The majority of browsers in use today do not support all, and some not even any, styles. That means that to achieve the special effects for most users, you have to prepare redundant collections - mixed or separate - containing both styles and style-related tags and attributes, if you use styles at all. We're right back in the same boat we were in a year or so ago when deciding whether to use the many new Netscape extensions to HTML. We believe that this too will pass. The choice is not whether to use CSS style sheets, but when, and when should be soon.

9.5.2 Which Type of Style Sheet and When
Once you have decided to use HTML cascading style sheets (for pain or pleasure), the next question is which type of style sheet - inline, document-level, or external - should you apply and when? Each has its pros and cons; each is best applied under certain circumstances.

9.5.2.1 The pros and cons of external styles
Since style sheets ostensibly provide consistency in the presentation of your documents, external style sheets are the best and the easiest way to manage styles for your entire document collection. Simply place the desired style rules in a style sheet and apply those styles to the desired documents. Since all the documents are affected by a single style sheet, conversion of the entire collection to a new style is as simple as changing a single rule in the corresponding external style sheet.

Even in cases where documents may differ in style, it is often possible to collect a few basic style rules in a single sheet that can be shared among several otherwise different documents, including: Background color


Background image


Font sizes and faces


Margins


Text alignment


Another benefit of external style sheets is that other web authors who want to copy your style can easily access that sheet and make their pages look like yours. Imitation being the sincerest form of flattery, you should not be troubled when someone elects to emulate the look and feel of your pages.

More to the point, you can't stop them from linking to your style sheets, so you might as well learn to like it. Like conventional HTML documents, it is not possible to encrypt or otherwise hide your style sheets so that others cannot view and use them.

The biggest problem with external style sheets is that they increase the amount of time needed to access a given web page. Not only must the browser download the page itself, it must also download the style sheet before the page can be displayed to the user. While most style sheets are relatively small, their existence can definitely be felt when accessing the Web over a slow connection.

Without appropriate discipline, external style sheets can become large and unwieldy. When creating style sheets, remember to include only those styles that are common to the pages using the sheet. If a set of styles is needed only for one or two sheets, you are better off isolating them in a separate sheet or adding them to a document using document-level styles. Otherwise, you may find yourself expending an exorbitant amount of effort counteracting the effects of external styles in many individual documents.

9.5.2.2 The pros and cons of document-level styles
Document-level styles are most useful when creating a custom document. They let you easily override one or more rules in your externally defined style to create a slightly different document.

You might also want to use document-level styles to experiment with new style rules before moving them to your style sheets. By adding and changing rules using document-level styles, you eliminate the risk of adding a broken style to your style sheets, breaking the appearance of all the documents that use that sheet.

The biggest problem with document styles is that you may succumb to using them in lieu of creating a formal, external style sheet to manage your document collection. It is easy to simply add rules to each document, cutting and pasting as you create new documents. Unfortunately, managing a collection of documents with document-level styles is tedious and error-prone. Even a simple change can result in hours of editing and potential mistakes.

As a rule of thumb, any style rule that impacts three or more documents should be moved to a style sheet and applied to those documents using the tag or @import command. Adhering to this rule as you create your document families will pay off in the long run when it is time to change your styles.

9.5.2.3 The pros and cons of inline styles
And at the end of the cascade, inline styles override the more general styles. Get into the habit now of using inline styles rarely and just for that purpose, too. Inline styles cannot be reused, making style

management difficult. Moreover, such changes are spread throughout your documents, making finding and altering inline styles error-prone. (That's why we might eschew tag-and attribute-based styles in the first place, no?)

Any time you use an inline style, think long and hard as to whether the same effect might be accomplished using a style class definition. For instance, you are better off defining:

and later using:



instead of:


Your styles are easier to find and manage and can be easily reused throughout your documents.

9.4 Tag-less Styles: The

10. Forms
Tag

Chapter 10
10. Forms
Contents:

Form Fundamentals

The

Tag

A Simple Form Example

Using Email to Collect Form Data

The Tag

Multiline Text Areas

Multiple Choice Elements

General Form Control Attributes

Labeling and Grouping Form Elements

Creating Effective Forms

Forms Programming

Forms, forms, forms, forms: we fill 'em out for nearly everything, from the moment we're born, 'til the moment we die. Pretty mundane, really. So what's to explain all the hoopla and excitement over HTML forms? Simply this: they make HTML truly interactive.

When you think about it, HTML's interactivity is basically a lot of button pushing: click here, click there, go here, go there - there's no real user feedback, and it's certainly not personalized. Applets provide extensive user-interaction capability, but they can be difficult to write and are still not standardized for all browsers. Forms, on the other hand, are supported by almost every browser and make it possible to create documents that collect and process user input, and to formulate personalized replies.

Other books

In Pieces by Nick Hopton
Cold Blood by Alex Shaw
Small-Town Mom by Jean C. Gordon
Street Magic by Pierce, Tamora
Before by Nicola Marsh
Dreams in a Time of War by Ngugi wa'Thiong'o
Epic: Book 03 - Hero by Lee Stephen

© 100 Vampire Novels 2015 - 2024    Contact for me [email protected]