HTML The Definitive Guide (136 page)

Read HTML The Definitive Guide Online

Authors: Chuck Musciano Bill Kennedy

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

Document-level JSS is defined within a

First, notice that we use the standard JavaScript and HTML comments to surround our JSS

definitions, preventing noncompliant browsers from processing them as HTML content. Also notice that the syntax of the style definition is that of JavaScript, where letter case does make a difference, amongst other things.

You associate inline JavaScript-based style rules with a specific tag using the style attribute, just like CSS inline styles. The value of the attribute is a list of JSS assignments, separated by semicolons.

For example:

creates a green, boldfaced text paragraph. Notice first that you need to enclose inline style values within single quotation marks, not double quotation marks, as you might use for document-level and in external JSS styles. This is reasonable, since the style attribute value itself must be enclosed in double quotation marks.

Also note that inline JSS definitions use only the property name, not the containing tag object that owns the property. This makes sense, since inline JSS styles affect only the current tag, not all instances of the tag.

13.4.1.2 JSS values

In general, all of the values you may use for CSS may also be used in JSS definitions. For keyword, length, and percentage values, simply enclose the value in quotes and use it as you would any string value in JavaScript. Thus, the CSS value bold becomes "bold" or 'bold' for JSS

document-level or inline styles, respectively; 12pt in CSS becomes '12pt' or "12pt" in JSS.

Specify color values as the color name or a hexadecimal color value, enclosed in single or double quotes. The CSS decimal rgb notation is not supported in JSS.

JSS URL values are strings containing the desired URL. Thus, the CSS URL value url(http://www.kumquat.com) becomes 'http://www.kumquat.com' for a JSS inline style; or "http://www.kumquat.com" at the document level.

One unique power of JSS is that any value can be computed dynamically when the document is processed by the browser. Instead of statically specifying the font size, for example, you can compute it on the fly:

tags.P.fontSize = favorite_font_size();

We assume that the JavaScript function favorite_font_size() somehow determines the desired font size and returns a string value containing that size. This, in turn, is assigned to the fontSize property for the

tag, defining the font size for all paragraphs in the document.

13.4.1.3 Defining styles for tags

JavaScript defines a new document property, tags, that contains the style properties for all HTML

tags. To define a style for a tag, simply set the appropriate property of the desired style property within the tag property of the document object. For example: document.tags.P.fontSize = '12pt';

document.tags.H2.color = 'blue';

These two JSS definitions set the font size for the

tag to 12 points and render all

tags in blue. The equivalent CSS definitions are: P {fontsize : 12pt}

H2 {color : blue}

Since the tags property always refers to the current document, you may omit document from any JSS tag style definition. We could have written the previous two styles as: tags.P.fontSize = '12pt';

tags.H2.color = 'blue';

Moreover, as we mentioned previously, you may omit the tag name, as well as the document and tags properties for inline JSS using the style attribute.

Capitalization and case are significant in JSS. The tag names within the tags property must always be fully capitalized. The embedded capital letters within the tag properties are significant: any deviation from the exact lettering produces an error, and Netscape won't honor your JSS declaration.

All of the following JSS definitions are invalid, though the reason is not overly apparent: tags.p.fontsize = '12pt';

tags.Body.Color = 'blue';

tags.P.COLOR = 'red';

The correct versions are:

tags.P.fontSize = '12pt';

tags.BODY.color = 'blue';

tags.P.color = 'red';

It can be very tedious to specify a number of properties for a single tag, so you can take advantage of the JavaScript with statement to reduce your typing burden. These styles: tags.P.fontSize = '14pt';

tags.P.color = 'blue';

tags.P.fontWeight = 'bold';

tags.P.leftMargin = '20%';

can be more easily written as:

with (tags.P) {

fontSize = '14pt';

color = 'blue';

fontWeight = 'bold';

leftMargin = '20%';

}

You can apply similar styles to diverse tags just as easily: with (tags.P, tags.LI, tags.H1) {

fontSize = '14pt';

color = 'blue';

fontWeight = 'bold';

leftMargin = '20%';

}

13.4.1.4 Defining style classes

Like CSS, JSS lets you target styles for specific ways in which a tag may be used in your document.

JSS uses the classes property to define separate styles for the same tag. There are no predefined properties within the classes property; instead, any property you reference is defined as a class to be used by the current document. For example: classes.bold.P.fontWeight = 'bold';

with (classes.abstract.P) {

leftMargin = '20pt';

rightMargin = '20pt';

fontStyle = 'italic';

textAlign = 'justify';

}

The first style defines a class of the

tag named bold whose font weight is set to bold. The next style uses the with statement to create a class of the

tag named abstract with the specified properties. The equivalent CSS rules would be the following: P.bold {font-weight : bold}

P.abstract {leftmargin : 20pt;

right-margin : 20pt;

fontstyle : italic;

text-align : justify

}

Once defined, use a JSS class just like any CSS class: with the class attribute and the class name.

Like CSS, JSS also lets you define a class without defining the tag that will use the class. This lets you define generic classes that you can later apply to any tag. To create a generic style class in JSS, use the special tag property all:

classes.green.all.color = "green"; You can then add class="green" to any tag to have Netscape render its contents in green. The equivalent CSS is:

.green {color : green}

13.4.1.5 Using contextual styles

One of the most powerful aspects of CSS is its contextual style capability, wherein the browser applies a style to tags only if they appear in the document in a certain nesting. JSS supports contextual styles as well, through the special contextual() method within the tags property. The parameters to this method are the tags and classes that define the context in which Netscape will apply the style. For example:

tags.contextual(tags.UL, tags.UL, tags.LI).listStyleType = 'disc'; defines a context wherein the elements (tags.LI) of an unordered list nested within another unordered list (tags.UL, tags.UL) use the disc as their bullet symbol. The CSS equivalent is: UL UL LI {list-style-type : disc}

You can mix tags and classes in the contextual() method. For instance: tags.contextual(classes.abstract.P, tags.EM).color = 'red'; tells the browser to display in red tags that appear within paragraphs that are of the abstract class. The CSS equivalent is: P.abstract EM {color : red}

Since the tags object is unambiguously included within the contextual() method, you may omit them from the definition. Hence, our nested list example may be rewritten as: tags.contextual(UL, UL, LI).listStyleType = 'disc';
13.4.2 JavaScript Style Sheet Properties
A subset of the CSS style properties are supported in JSS. The JSS style properties, their CSS

equivalents, and the sections in which those properties are fully documented are shown in
Table 13.2.

Table 13.2: JSS Properties and Cascading Style Sheet Equivalents
JSS Property

CSS Property

See Section

align

float

9.3.6.8

backgroundImage

background-image

9.3.4.3

backgroundColor

background-color

9.3.4.2

borderBottomWidth border-bottom-width 9.3.6.4

borderLeftWidth

border-left-width

9.3.6.4

borderRightWidth

border-right-width

9.3.6.4

borderStyle

border-style

9.3.6.5

borderTopWidth

border-top-width

9.3.6.4

clear

clear

9.3.6.7

display

display

9.3.8.1

fontSize

fontsize

9.3.3.2

fontStyle

fontstyle

9.3.3.3

height

height

9.3.6.9

lineHeight

line-height

9.3.5.2

listStyleType

list-style-type

9.3.7.3

marginBottom

margin-bottom

9.3.6.10

marginLeft

margin-left

9.3.6.10

marginRight

margin-right

9.3.6.10

marginTop

margin-top

9.3.6.10

paddingBottom

padding-bottom

9.3.6.11

paddingLeft

padding-left

9.3.6.11

paddingRight

padding-right

9.3.6.11

paddingTop

padding-top

9.3.6.11

textDecoration

text-decoration

9.3.5.4

textTransform

text-transform

9.3.5.6

textAlign

text-align

9.3.5.3

textIndent

text-indent

9.3.5.5

verticalAlign

vertical-align

9.3.5.7

whiteSpace

whitespace

Other books

008 Two Points to Murder by Carolyn Keene
Another Green World by Richard Grant
Proof of Heaven by Alexander III M.D., Eben
Panda-Monium by Bindi Irwin
Triple Identity by Haggai Carmon
The Christie Curse by Victoria Abbott
Missing by Francine Pascal
The Beast by Alianne Donnelly