Style Sheets
Prior to the introduction of style sheets for HTML documents, web page authors had limited control over the presentation of their web pages. For example, you could specify text to be displayed as headings, but you could not set margins for your pages or specify the line heights or margins for text.
Style sheets give you greater control over the presentation of your web documents. Using style sheets, you can specify many stylistic attributes of your web page, such as text color, margins, element alignments, font styles, font sizes, font weights and more.
Popular browsers supports two types of style sheet syntax. They supports style sheets written in cascading style sheet (CSS) syntax. They also supports style sheets written in JavaScript that use the document object model. In the document object model, a document is an object that has properties. Each property can in turn be an object that has further properties, and so on.
When you define a style sheet, you must declare its type as either
"text/CSS" or "text/JavaScript". To try to keep things straight, this manual uses the term CSS syntax to refer to the syntax for style sheets whose type is "text/CSS". It uses the term JavaScript syntax to refer to the syntax for style sheets whose type is "text/JavaScript".Using Cascading Style Sheets to Define Styles
style sheets specification are authored by the World Wide Web Consortium, you can go to:
http://www.w3.org/pub/WWW/TR/REC-CSS1
A style sheet consists of a one or more style definitions. In CSS syntax, the property names and values of a style are listed inside curly braces following the selection criteria for that style. The selection criteria determines which elements the style is applied to, or which elements it can be applied to. If the selection criteria is an HTML element, the style is applied to all instances of that element. The selection criteria can also be a class, an ID, or it can be contextual. Each of these kinds of selection criteria are discussed in this document.
Each property in the style definition is followed by a colon then by the value for that property. Each property name/value pair must be separated from the next pair by a semicolon. For example, the following cascading style sheet defines two styles definitions. One style definition specifies that the font size for all
<P> elements is 18 and the left margin for all <P> elements is 20. The other style definition specifies that the color for all <H1> elements is blue.<STYLE TYPE="text/css">
<!--
P {font-size:18pt; margin-left:20pt;}
H1 {color:blue;}
-->
</STYLE>
You can include the contents of the style sheet inside a comment (
<!-- ...-->) so that browsers that do not recognize the <STYLE> element will ignore it.Important: When specifying values for cascading style sheet properties, do not include double quotes.
Cascading style sheets require strict adherence to correct syntax. Be sure not to omit any semicolons between name/value pairs. If you miss a single semicolon, the style definition will be ignored. Similarly if you accidentally include a single extraneous character anywhere within a style definition, that definition will be ignored.
Using JavaScript and the Document Object Model to
Define Styles
Using JavaScript, you can define style sheets that use the document object model. In this model, you can think of a document such as a web page as an object that has properties that can be set or accessed. Each property can in turn be an object that has further properties. For example, the following code sets the
color property of the object in the H1 property of the object in the tags property of the document:document.tags.H1.color = "red";
The
tags property always applies to the document object for the current document, so you can omit document from the expression document.tags. The following example uses JavaScript and the document object model to define a style sheet that has two style definitions. One style definition specifies that the font size for all <P> elements (tags) is 18 and the left margin for all <P> elements is 20. The other style definition specifies that the color for all <H1> elements is blue.<STYLE TYPE = "text/javascript">
tags.P.fontSize = "18pt";
tags.P.marginLeft = "20pt";
tags.H1.color = "blue";
</STYLE>
Do not wrap the contents of the style sheet in a comment (
!-- ... -->) for style sheets that use JavaScript syntax.You can also use the
with (tags.element) syntax to shorten the style specification for elements that have several style settings. The following example specifies that all <P> elements are displayed in green, bold, italic, Helvetica font.with (tags.P) {
color="green";
font-weight="bold";
font-style="italic";
font-family="helvetica";
}
Javascript Accessible Style Sheets 19
Introductory Example
Introductory Example
Using style sheets, you can specify many stylistic attributes of your web page. The stylistic characteristics you can set for font and text include text alignment, text color, font family (such as Garamond), font style (such as italic), font weight (such as bold), line height, text decoration (such as underlining), horizontal and vertical alignment of text, and text indentation (which allows indented and outdented paragraphs). You can specify background colors and images for elements. You can specify the color and style to use for the bullets and numbers in lists.
You can set margins and also specify borders for block-level elements. You can set the padding for elements that have borders, to indicate the distance between the element’s content and its border. The following code shows a simple style sheet in both CSS syntax and JavaScript syntax. This style sheet specifies that all
<P> elements have left and right margins, and their text is centered between the margins. All <H4> elements are green and underlined. All <H5> elements are uppercase. They have a red border that is four points thick. The border is in outdented 3D style and the padding between the text and the border is four points. The text color is red and the background color is yellow. All <BLOCKQUOTE> elements are blue italic, with a line height that is 150% larger than the font size. The first line is indented by 10% of the width of the element.CSS Syntax
<STYLE TYPE="text/css">
P {
textAlign:center; margin-left:20%; margin-right:20%;}
H4 {
text-decoration:underline; color: green;}
H5 {
text-transform:uppercase; color: red;
border-width:4pt; border-style:outset;
background-color:yellow; padding: 4pt;
border-color:red;}
BLOCKQUOTE {
color:blue; font-style:italic;
line-height:1.5; text-indent:10%;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
with (tags.P) {
textAlign = "center"; marginLeft="20%". margin-right="20%";}
with (tags.H4) {
textDecoration = "underline; color = "green";
textTransform = "uppercase;}
with (tags.H5) {
color = "red";
borderWidths="4pt"=; borderStyle="outset";
backgroundColor="yellow"; paddings("4pt");
borderColor="red";}
with (tags.BLOCKQUOTE) {
color="blue"; fontStyle="italic";
lineHeight = 1.5; textIndent = "20pt";}
</STYLE>
Style Sheet Use
<H4>Underlined Heading 4</H4>
<BLOCKQUOTE>
This is a blockquote. It is usual for blockquotes to be indented, but the first line of this blockquote has an extra indent. Also the line height in this blockquote is bigger than you usually see in blockquotes.
<h5>uppercase heading 5 with a border</H5>
</BLOCKQUOTE>
<P>This paragraph has a text alignment value of center. It also has
large margins, so each line is not only centered but is also inset on
both sides from the element that contains it, which in this case is the
document.</P>
Inheritance of Styles
An HTML element that contains another element is considered to be the parent element
of the element it contains, and the element it contains is considered to be its child
element.
For example, in the following HTML text, the
<BODY> element is the parent of the<H1>
element which in turn is the parent of the <EM> element.<BODY>
<H1>The headline <EM>is</EM> important!</H1>
</BODY>
In many cases, child elements acquire or inherit the styles of their parent elements. For
example, suppose a style has been assigned to the
<H1> element as follows:<STYLE type="text/css">
H1 {color:blue;}
</STYLE>
<BODY>
<H1>The headline <EM>is</EM> important!</H1>
In this case, the child
<EM> element takes on the style of its parent, which is the <H1>element, so the word is appears in blue. However, suppose you had previously set up a
style specifying that
<EM> elements should be displayed in red. In that case, the word iswould be displayed in red, because properties set on the child override properties
inherited from the parent.
Inheritance starts at the top-level element. In HTML, this is the
<HTML> element, whichis followed by the
<BODY> element.To set default style properties for all elements in a document, you can specify a style for
the
<BODY> element. For example, the following code sets the default text color togreen.
CSS Syntax
<STYLE TYPE="text/css">
BODY {color: green;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
tags.BODY.color="green";
</STYLE>
A few style properties are not inherited by the child element from the parent
element, but in most of these cases, the net result is the same as if the property
was inherited. For example, consider the background color property, which is
not inherited. If a child element does not specify its own background color,
then the parent’s background color is visible through the child element. It will
look as if the child element has the same background color as its parent
element.
Creating Style Sheets and Assigning Styles
This part looks at each of the different ways you can defines styles, and
shows how to apply styles to HTML elements.
• Defining Style Sheets with the <STYLE> Tag
• Defining Style Sheets in External Files
• Defining Classes of Styles
• Defining Named Individual Styles
• Using Contextual Selection Criteria
• Combining Style Sheets
A style sheet is a series of one or more style definitions.You can define a style
sheet directly inside the document that uses it, or you can define a style sheet
in an external document. If the style sheet is in an external document, then it
can be used by other documents. For example, a series of pages for a particular
site could all use a single externally defined style sheet that sets up the house
style.
If the style sheet is unlikely to be applicable to other documents, it can be more
convenient to define it directly in the document that uses it, since then you
have the style sheet and the content in one place.
Defining Style Sheets with the <STYLE> Tag
To define a style sheet directly inside a document, use the
<STYLE> tag in theheader part of your document. The
<STYLE> tag opens the style sheet, and the</STYLE>
tag closes the style sheet. Be sure to use the <STYLE> tag beforethe
<BODY> tag.When you use the
<STYLE> tag, you can specify the TYPE attribute to indicateif the type is
"text/css" or "text/javascript". The default value forTYPE
is "text/css".The following example defines a style sheet that specifies that all level-one
headings are uppercase blue, and all blockquotes are red italic.
CSS Syntax
<HEAD>
<STYLE TYPE="text/css">
H1 {color: blue; text-transform: uppercase;}
BLOCKQUOTE {color: red; font-style: italic;}
</STYLE>
</HEAD>
<BODY>
JavaScript Syntax
<HEAD>
<STYLE TYPE="text/javascript">
tags.H1.textTransform = "uppercase";
tags.H1.color = "blue";
tags.BLOCKQUOTE.color = "red";
tags.BLOCKQUOTE.font-style: italic;
</STYLE>
</HEAD>
<BODY>
Style Sheet Use
<H1>This Heading Is Blue</H1>
<B>BLOCKQUOTE>This blockquote is displayed in red.</B>
Defining Style Sheets in External Files
You can define style sheets in a file that is separate from the document and
then link the style sheet to the document. The benefit of this approach is that
the style sheet can be used by any HTML document. You could think of an
externally defined style sheet as a style template that can be applied to any
document. For example, you could apply a style template to all pages served
from a particular web site by including a link to the style sheet file in each
page.
The syntax for defining styles in external files is the same as for defining styles
inside a document file, except that you do not need the opening and closing
<STYLE>
and </STYLE> tags. Here is an example:CSS Syntax
/* external style sheet mystyles1.htm */
all.BOLDBLUE {color:blue; font-weight: bold;}
H1 {line-height: 18pt;}
P {color: yellow;}
/* end of file */
JavaScript Syntax
/* external style sheet mystyles1.htm */
classes.BOLDBLUE.all.color = "blue";
classes.BOLDBLUE.all.fontWeight = "bold";
tags.H1.lineHeight="18pt";
tags.P.color="yellow";
/* end of file */
To use an externally defined style sheet in a document, use the
<LINK> tagto link to the style sheet, as in this example:
CSS Syntax
<HTML>
<HEAD>
<TITLE>A Good Title</TITLE>
<LINK REL=STYLESHEET TYPE="text/css"
HREF="http://style.com/mystyles1.htm">
</HEAD>
JavaScript Syntax
<HTML>
<HEAD>
<TITLE>A Good Title</TITLE>
<LINK REL=STYLESHEET TYPE="text/javascript"
HREF="http://style.com/mystyles1.htm">
</HEAD>
Defining Classes of Styles
If a document includes or links to a style sheet, all the styles defined in the style
sheet can be used by the document. If the style sheet specifies the style of any
HTML elements, then all the HTML elements of that kind in the document will
use the specified style.
There may be times when you want to selectively apply a style to HTML
elements. For example, you may want some of the paragraphs (
<P> elements)in a document to be red, and others to be blue. In this situation, defining a style
that applies to all
<P> elements is not the right thing to do. Instead, you candefine classes of style, and apply the appropriate class of style to each element
that needs to use a style.
To apply a style class to an HTML element, define the style class in the style
sheet, and then use the
CLASS attribute in the HTML element.The following examples show how to define a class called
GREENBOLD, whosecolor is a medium shade of green and whose font weight is bold. The example
then illustrates how to use the style in HTML text.
Javascript Accessible Style Sheets 27
Defining Classes of Styles
CSS Syntax
<STYLE TYPE="text/css">
all.GREENBOLD {color:#44CC22; font-weight:bold;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
classes.GREENBOLD.all.color = "#44CC22"
classes.GREENBOLD.all.fontWeight = "bold"
</STYLE>
Style Sheet Use
<H1 CLASS=GREENBOLD>This Heading Is Very Green</H1>
<P CLASS = GREENBOLD>This paragraph uses the style class GREENBOLD. You
can use the CLASS attribute to specify the style class to be used by an
HTML element.</P>
<BLOCKQUOTE CLASS = GREENBOLD>
This blockquote uses the style class GREENBOLD. As a consequence, it is
both green and bold. It can be useful to use styles to make blockquotes
stand out from the rest of the page.
</BLOCKQUOTE>
In JavaScript syntax, you cannot use hyphens inside class names. A hyphen is
actually a minus sign, which is a JavaScript operator. Class names In JavaScript
syntax cannot include any JavaScript operators, including but not limited to -, +,
*, /, and %.
When defining a style class, you can specify which HTML elements can use this
style, or you can use the keyword
all to let all elements use it.For example, the following code creates a style class
DARKYELLOW, which canbe used by any
HTML element. The code also creates a class called RED1,which can be used only by
<P> and <BLOCKQUOTE> elements.CSS Syntax
<STYLE type="text/css">
all.DARKYELLOW {color:#EECC00;}
P.RED1 {color: red; font-weight:bold;}
BLOCKQUOTE.red1 {color:red; font-weight:bold;}
</STYLE>
JavaScript Syntax
<STYLE type="text/javascript">
classes.DARKYELLOW.all.color="#EECC00";
classes.RED1.P.color = "red";
classes.RED1.P.fontWeight = "bold";
classes.RED1.BLOCKQUOTE.color = "red";
classes.RED1.BLOCKQUOTE.fontWeight = "bold";
</STYLE>
Style Sheet Use
<BODY>
<P CLASS=red1>This paragraph is red.</H1>
<P>This paragraph is in the default color, since it does not use the
class RED1.</P>
<BLOCKQUOTE CLASS="red1">This blockquote uses the class RED1.
</BLOCKQUOTE>
<H5 CLASS=red1>This H5 element tried to use the style RED1, but was not
allowed to use it.</H5>
<P CLASS=darkyellowclass>This paragraph is dark yellow.
<H5 CLASS=darkyellowclass>This H5 element tried to use the style
DARKYELLOW and was succesful.</H5>
An HTML element can use only one class of style. If you specify two or more
classes of style for an HTML element, the first one specified is used. For
example, in the following code the paragraph will use the
RED1 style andignore the
DARKYELLOW style:<P CLASS="RED1" CLASS="DARKYELLOW">Another paragraph.</P>
Defining Named Individual Styles
You can create individual named styles. An HTML element can use both a class
of style and a named individual style. Thus you can use individual named styles
to express stylistic exceptions to a class of style. For example, if a paragraph
uses the
MAIN style class, it could also use the named style BLUE1 which couldexpress some differences to the
MAIN style.Individual names styles are also useful for defining layers of precisely
positioned HTML content. For more details of layers, see the
Part 2. PositioningHTML Content
.To define named styles in CSS syntax, precede the name of the style with the
#sign. In JavaScript syntax, use the
ids property.To apply the style to an element, specify the style name as the value of the
element’s
ID attribute.The following codes defines a style class called
MAIN. This style class specifiesa a line height of 20 points, a font size of 18 points; a font weight of bold, and
a color of red. The code also defines a named style BLUE1 whose color is blue.
CSS Syntax
<STYLE TYPE="text/css">
all.STYLE1 {line-height: 20pt; font-size: 18pt;
font-weight: bold; color: red;}
#BLUE1 {color: blue;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
with (classes.STYLE1.all) {
lineHeight = "20pt";
fontSize = "18pt";
fontWeight = "bold";
all.color = "red";
}
ids.BLUE1.color = "blue";
</STYLE>
Style Sheet Use
<P CLASS="STYLE1">Here you see some tall red text. The text in this
paragraph is much taller, bolder, and bigger than paragraph text
normally is.</P>
<P CLASS="STYLE1" ID="BLUE1">This paragraph has tall, bold, blue text.
Although this paragraph is in class STYLE1 1, whose members are normally
red, it also has a unique ID that allows it to be blue.</P>
Using Contextual Selection Criteria
You can define the style to be used by all HTML elements of a particular kind.
If you need more control over when a style is used, you can define a style class
that you can selectively apply to any element. Sometimes however, even that
level of control is not enough. You might, for example, want to specify a green
color for all
<EM> elements inside level-one headings.You can achieve this level of control over the application of styles by using
contextual selection criteria in your style definition. Contextual selection criteria
allow you to specify criteria such as "this style applies to this kind of element
nested inside that kind of element nested inside the other kind of element."
To specify contextual selection criteria in CSS syntax, list the HTML elements in
order before the opening curly brace of the properties list. In JavaScript syntax,
use the predefined method
contextual().The following example shows how to specify a green text color for all
<EM>elements inside
<H1> elements.CSS Syntax
<STYLE TYPE="text/css">
H1 EM {color:green;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
contextual(tags.H1, tags.EM).color = "green";
</STYLE>
Style Sheet Use
<H1>This is<EM> green, emphasized text,</EM> but this is plain headingone
text</H1>
Consider another example, given first in CSS syntax then in JavaScript syntax:.
UL UL LI {color:blue;}
contextual(tags.UL, tags.UL, tags.LI).color = "blue";
In this case, the selection criteria match
<LI> elements with at least two <UL>parents. That is, only list items that are two levels deep in an unordered list will
match this contextual selection and thus be displayed in blue.
You can use contextual selection criteria to look for tags, classes, IDs, or combinations
of these. For example, the following example creates a class called
MAGENTACLASS
. Everything in this class is magenta colored. All paragraphs inMAGENTACLASS
that are also inside <DIV> elements are italic. All text inside<B>
tags nested inside paragraphs in MAGENTACLASS that are inside <DIV>elements is extra large.
CSS Syntax
<STYLE TYPE="text/css">
all.MAGENTACLASS {color: magenta;}
DIV P.MAGENTACLASS {font-style: italic;}
DIV P.MAGENTACLASS B {font-size:18pt;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
classes.MAGENTACLASS.all.color = "magenta";
contextual(tags.DIV, classes.MAGENTACLASS.P).fontStyle = "italic";
contextual(tags.DIV, classes.MAGENTACLASS.P, tags.B).fontSize = "18pt";
</STYLE>
Style Sheet Use
<DIV CLASS=MAGENTACLASS>
<H3>Heading 3 in the MAGENTACLASS</H3>
<P>Is this paragraph magenta and italic? It should be. Here comes some
<B>big bold text.</B> We achieved this result with contextual
selection.</P>
<P>This paragraph should be magenta too.</P>
</DIV>
<P>This paragraph is still magenta colored, but since it is not inside a
DIV block, it should not be italic.</P>
Specifying Styles for Individual Elements
As well as defining styles in style sheets, you can also use the
STYLE attributeof an HTML tag to define a style for use by that individual tag, and that tag
only. This approach basically defines the style on the fly, and can be useful in
situations where you want an element to use a style in a unique situation,
where you do not need to reuse the style elsewhere in the document.
In general though, it is better to define all the style used by a document in a
single place (be it at the top of the document or in a separate style sheet file) so
that you know where to make changes to the style. If a style is defined in a
style sheet, any element in the document can use that style. If you want to
change the properties of the style, you need to make the change only once and
it is automatically applied to all elements that use that style.
Sometimes, however, youmay want to specify the style of an individual
element, and an easy way to do this is to use the
STYLE attribute. Thefollowing example specifies the style of an individual
<P> element. It alsoshows how to use the
STYLE attribute with the <SPAN> tag to apply a style toa piece of arbitrary content.
CSS Syntax
<P STYLE="color:green; font-weight:bold;
margin-right:20%; margin-left:20%;
border-width:2pt; border-color:blue;">
This paragraph, and only this paragraph is bold green with big margins
Javascript Accessible Style Sheets 33
Combining Style Sheets
and a blue border.</P>
<P>This paragraph is in the usual color, whatever that may be, but this
<SPAN STYLE="color:red; font-style:italic;">word </span> is different
from the other words in this paragraph.</P>
JavaScript Syntax
<P STYLE="color = 'green'; fontWeight='bold';
marginRight='20%' marginLeft='20%';
borderWidth='2pt'; borderColor='blue;">
This paragraph, and only this paragraph is bold green with big margins
and a blue border.</P>
<P>This paragraph is in the usual color, whatever that may be, but this
<SPAN STYLE="color=’red’; fontStyle=’italic’">word </span> is different
from the other words in this paragraph.</P>
Combining Style Sheets
You can use more than one style sheet to set the styles for a document. You
might want to do this when you have several partial styles sheets that you wish
to mix and match, or perhaps where your document falls into several different
categories, each with its own style sheet.
For example, suppose you are are writing a white paper about the benefits of a
network product from a company called Networks Unlimited. You might need
to use three style sheet: one defining the company’s usual style for white
papers, another defining their usual style for documents about networking
products, and yet another defining the corporate style for Networks Unlimited.
The following example illustrates the use of several style sheets in one
document:
<STYLE TYPE="text/css"
SRC="http://www.networksunlimited.org/styles/corporate"></STYLE>
<STYLE TYPE="text/css"
SRC="styles/whitepaper"></STYLE>
<STYLE TYPE="text/javascript"
SRC="styles/networkthings"></STYLE>
<STYLE TYPE="text/css">
H1 {color: red;} /* override external sheets */
</STYLE>
For externally linked style sheets, the last one listed takes precedence over
previously listed style sheets. So in this case, if
networkthings and whitepaperspecify conflicting styles, then the styles defined in
networkthingstake precedence over styles defined in
whitepaper.Locally defined styles take precedence over styles defined in the
<STYLE>element and over styles defined in external style sheets. In general, local style
values override values inherited from parent elements, and more specific style
values override more general values, as illustrated in the following example.
CSS Syntax
<STYLE TYPE="text/css">
P {color:blue;}
B {color:green;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
tags.P.color="blue";
tags.B.color="green";
</STYLE>
Style Sheet Use
<P>This is a blue paragraph, as determined by the style sheet. But these
<B>bold words are green,</B> as you see.</P>
<P STYLE="color:red">This is a red paragraph, as determined by the local
style. However, these <B>bold words are still green,</B> since the style
defined directly for bold elements overrides the style of the parent
element.</P>
Format Properties for Block-Level Elements
This part discusses the formatting options for block-level elements. Blocklevel
elements start on a new line, for example,
<H1> and <P> are block-levelelements, but
<EM> is not.This part starts off by presenting an example that illustrates the various ways
of formatting block-level elements. After that comes a section discussing each
kind of formatting option in detail. The part and ends with a brief overview
of the inheritance behavior of properties that are used for formatting blocklevel
elements.
• Block-level Formatting Overview and Example
• Setting Margins or Width
• Setting Border Widths, Color, and Style
• Setting Paddings
• Inheritance of Block-Level Formatting Properties
Block-level Formatting Overview and Example
Style sheets treat each block-level element as if it were surrounded by a box.
Each box can have style characteristics in the form of margins, borders, and
padding. Each box can have a background image or color.
The margins indicate the inset of the edge of the box from the edges of the
document (or parent element). Each box can have a border that has a flat or
three dimensional appearance. The padding indicates the distance between the
element’s border and the element’s content.
You can also set the width of a block-level element, either to a specific value or
to a percentage of the width of the document (or parent element). As you can
imagine, it is redundant to set the left and right margin and to also set the
width.
If values are specified for the width and for both margins, the left margin
setting has the highest precedence. In this case, the value for the right margin
indicates the absolute maximum distance from the right edge of the containing
element at which the content wraps. If the value given for the width would
cause the element to run past the right margin, the width value is ignored. If
the width value would cause the element to stop short of the right edge, the
width value is used.
You can set the horizontal alignment of an element to left, right, or center. You
do this by setting the
float property in CSS syntax or setting the alignproperty in JavaScript syntax. It is also redundant to set the left and/or right
margin and then also set the element’s alignment.
The following example illustrates the use of margins, paddings, border widths,
background color, width, and alignment properties.
CSS Syntax
<STYLE TYPE="text/css">
P {
background-color:#CCCCFF;
/* margins */
margin-left:20%; margin-right:20%;
/* border widths
border-top-width: 10pt; border-bottom-width:10pt;
border-right-width:5pt; border-left-width:5pt;
/* border style and color
border-style:outset; border-color:blue;
/* paddings */
padding-top:10pt; padding-bottom:10pt;
Javascript Accessible Style Sheets 37
Block-level Formatting Overview and Example
padding-right:20pt; padding-left:20pt;
}
H3 {
/* font size and weight */
font-size: 14pt;
font-weight:bold;
background-image:URL("images/grenlite.gif");
/* center the heading with a 90% width */
width:90%;
float:center;
border-color:green;
border-style:solid;
/* all sides of the border have the same thickness */
border-width:10pt;
/* all sides have the same padding */
padding:20pt;
}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
with (tags.P) {
backgroundColor="#CCCCFF";
/* P border style and color */
borderStyle="outset"; borderColor="blue";
/* P border widths */
borderTopWidth="10pt"; borderBottomWidth="10pt";
borderLeftWidth="5pt"; borderRightWidth="5pt";
/* P paddings */
paddingTop="10pt"; paddingBottom="10pt";
paddingLeft="20pt"; paddingRight="20pt";
/* P margins */
marginLeft= "20%"; marginRight="20%";
}
with (tags.H3) {
backgroundImage ="images/grenlite.gif";
/* font size and weight */
fontSize="14pt"; fontWeight="bold";
/* H3 border style and color */
borderStyle="solid"; borderColor="green";
/* center the heading with a 90% width */
width="90%"; align="center";
/* all sides of the border have the same thickness */
borderWidths("10pt");
/* all sides have the same padding */
paddings("20pt");
}
</STYLE>
Style Sheet Use
<H3>H3 with a Solid Border</H3>
<P>Borders have their uses in everyday life. For example, borders round
a paragraph make the paragraph stand out more than it otherwise would.
</P>
<P>This is another paragraph with a border. You have to be careful not
to make the borders too wide, or else they start to take over the page.
</P>
Javascript Accessible Style Sheets 39
Setting Margins or Width
Setting Margins or Width
The margins indicate the inset of the element from the edges of the document
(or parent element.) You can set right, left, top, and bottom margins. The
"edge" of the parent is the theoretical rectangle that would be drawn round the
inside of the padding, border, or margins of the parent element, if it has any of
these properties.
You can set the values of the margins for a block-level element by specifying
the following properties (shown as CSS syntax/JavaScript syntax property
names):
• margin-top/marginTop
•
margin-bottom/marginBottom• margin-left/marginLeft
• margin-right/marginRight
• You can set all four properties at once to the same value, either by setting
the
margin property in CSS syntax or by using the margins() function inJavaScript syntax.
You can set the horizontal alignment of an element to left, right, or center. You
do this by setting the
float property in CSS syntax or setting the alignproperty in JavaScript syntax. It is redundant to set the left and/or right margin
and then also set the element’s alignment.
Instead of setting specific margin values, you can also set the
width property.You can set this to either a specific value (for example,
200pt) or to apercentage of the width of the containing element or document (for example,
60%
). If desired, you can set the width and either the left or right margin, solong as the total does not exceed 100% of the width of the parent. It is not
useful, however, to set the width and also to set both margins, since two of the
values imply the third. (For example, if the left margin is 25% and the width is
60%, then the right margin must be 15%.)
Two or more adjoining margins (that is, with no border, padding or content
between them) are collapsed to use the maximum of the margin values. In the
case of negative margins, the absolute maximum of the negative adjoining
margins is deducted from the maximum of the positive adjoining margins.
To set the default margins for everything in a document, you can specify the
margin properties for the
<BODY> tag. For example, the following code sets theleft and right margins to 20 points.
CSS Syntax
<STYLE TYPE="text/css">
BODY {margin-left:20pt; margin-right:20pt;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
with (tags.BODY) {
marginLeft="20pt"; marginRight="20pt";
}
</STYLE>
See
Block-level Formatting Overview and Example for an example of settingmargins and width.
Setting Border Widths, Color, and Style
You can set the width of the border surrounding a block-level element by
specifying the following properties (shown as CSS syntax/JavaScript syntax
values):
• border-top-width/borderTopWidth
• border-bottom-width/borderBottomWidth
•
border-left-width/borderLeftWidth• border-right-width/borderRightWidth
• You can set all four properties at once to the same value, either by setting
the
border-width property in CSS syntax or by using the border-Widths()
function in JavaScript syntax.Javascript Accessible Style Sheets 41
Setting Paddings
You can set the style of the border by specifying the
border-style (CSSsyntax) or
borderStyle (JavaScript syntax) property. You can give theborder a flat appearance by setting the border-style to
solid or double, oryou can give it a 3D appearance, by setting the border-style to
groove,ridge
, inset, or outset.You can set the color of the border by specifying the
border-color (CSSsyntax) or
borderColor (JavaScript syntax) property.For an example of each of the border styles, see:
borders.htm
StyleSheetExampleFor another example of setting border widths, border style, and border color,
see
Block-level Formatting Overview and Example.Setting Paddings
The padding indicates the distance between the border of the element and its
content. The padding is displayed even if the element’s border is not displayed.
You can set the size of the padding surrounding a block-level element by specifying
the following properties (shown as CSS syntax/JavaScript syntax values):
• padding-top/paddingTop
• padding-bottom/paddingBottom
•
padding-left/paddingLeft• padding-right/paddingRight
• You can set all four properties at once to the same value, either by setting
the
padding property in CSS syntax or by using the paddings() functionin JavaScript syntax.
See
Block-level Formatting Overview and Example for an example of settingpaddings.
Inheritance of Block-Level Formatting Properties
The width, margins, border characteristics, and padding values of a parent
element are not inherited by its child elements. However, at first glance it might
seem that these values are inherited, since the values of the parent elements
affect the child elements.
For example, suppose you set the left margin of a
DIV element to 10 points.You can think of this
DIV element as a big box that gets inset by 10 points onthe left. Assume that the
DIV element has no border width and no padding. Ifall the elements inside the
DIV have a margin of 0, they are smack up againstthe edge of that box. Since the box is inset by 10 points, all the elements end
up being inset by 10 points.
Now consider what would happen if the child elements did inherit the margin
value from their parent element. If that were the case, then the DIV block
would have a left margin of 10 points, and child elements would also each
have a left margin of 10 points, so they would be indented on the left by 20
points.
Style Sheet Reference
This section includes reference information for both CSS syntax and JavaScript
syntax. It covers style sheet functionality that is implemented in Netscape
Navigator 4.0.
This reference does not include style sheet properties that can be used to
specify positions for blocks of HTML content. These properties are discussed in
Part 2. Positioning HTML Content.
This part is organized in the following sections:
Comments in Style Sheets
New HTML Tags
• <STYLE>
• <LINK>
• <SPAN>
New Attributes for Existing HTML Tags
• STYLE
• CLASS
• ID
New JavaScript Object Properties
• tags
• classes
• ids
Style Sheet Properties
Font Properties
• Font Size
• Font Style
• Font Family
• Font Weight
Text Properties
• Line Height
• Text Decoration
• Text Transform
• Text Alignment
• Text Indent
Block-Level Formatting Properties
• Margins
• Padding
• Border Widths
• Border Style
Javascript Accessible Style Sheets 45
Comments in Style Sheets
• Border Color
• Width
• Alignment
• Clear
Color and Background Properties
• Color
• Background Image
• Background Color
Classification Properties
• Display
• List Style Type
• White Space
Units
• Length Units
• Color Units
Comments in Style Sheets
Comments in style sheets are similar to those in the C programming language.
For example:
B {color:blue;} /* bold text will be blue */
tags.B.color = "blue"; /* bold text will be blue */
JavaScript style sheet syntax also supports comments in the C++ style, for
example:
tags.B.color = "blue"; // bold text will be blue
Comments cannot be nested.
New HTML Tags
This section lists the HTML tags that are useful for working with styles.
<STYLE>
The
<STYLE> and </STYLE> tags indicate a style sheet. Inside <STYLE> and</STYLE>
you can specify styles for elements, define classes and IDs, andgenerally establish styles for use within the document.
To specify that the style sheet uses JavaScript syntax, set the
TYPE attribute to"text/javascript".
To specify that the style sheet uses CSS syntax, set theTYPE
attribute to "text/css". The default value for TYPE is "text/CSS".For example:
<STYLE TYPE="text/css">
BODY {margin-right: 20%; margin-left:20%;}
PRE {color:green;}
all.CLASS1 {float:right; font-weight: bold;}
</STYLE>
<LINK>
Use the
<LINK> element to link to an external style sheet for use in adocument. For example:
CSS Syntax
<HTML>
<HEAD>
<TITLE>A Good Title</TITLE>
<LINK REL=STYLESHEET TYPE="text/css"
HREF="http://style.com/mystyles1.htm">
</HEAD>
Javascript Accessible Style Sheets 47
New Attributes for Existing HTML Tags
JavaScript Syntax
<HTML>
<HEAD>
<TITLE>A Good Title</TITLE>
<LINK REL=STYLESHEET TYPE="text/javascript"
HREF="http://style.com/mystyles1.htm">
</HEAD>
<SPAN>
Use the inline
<SPAN> and </SPAN> elements to indicate the beginning andend of a piece of text to which a style is to be applied.
The following example applies an individual style to a piece of text.
<P>Here is some normal paragraph text. It looks OK, but would be much
better if it was<SPAN style="color:blue; font-weight:bold; fontstyle:
italic"> in bright, bold, italic blue. </SPAN>The blue text stands
out much more.</P>
You can use the
<SPAN> element to achieve effects such as a large initial letter,for example:
<STYLE TYPE="text/css">
init-letter.all {font-size:400%; font-weight:bold;}
</STYLE>
<P><SPAN class="init-letter">T</SPAN>his is...</P>
New Attributes for Existing HTML Tags
This section lists the new attributes for existing HTML tags that are useful for
working with styles. These attributes can be used with any HTML tag to specify
the style for that tag.
STYLE
The
STYLE attribute determines the style of a specific element. For example:CSS Syntax
<H3 STYLE="line-height:24pt; font-weight:bold; color:cyan;">
Cyan Heading</H3>
JavaScript Syntax
<H3 STYLE="lineHeight=’24pt’; fontWeight=’bold’; color=’cyan’">
Cyan Heading</H3>
CLASS
The
CLASSES JavaScript property allows you to define classes of styles in astyle sheet. The
CLASS attribute specifies a style class to apply to an element.Although CSS syntax and JavaScript syntax use slightly different syntax to define
classes of styles, the use of the
CLASS attribute is the same in both syntaxes.For example:
CSS SyntaxExample
<STYLE TYPE="text/css">
H3.class1 {font-style:italic; color:red;}
</STYLE>
JavaScript Syntax Example
<STYLE TYPE="text/javascript">
classes.class1.H3.fontStyle="italic";
classes.class1.H3.color="red";
</STYLE>
Style Sheet Use
<H3 CLASS="class1">This H3 is in red italic letters.</H3>
Class names are case-sensitive.
Each HTML element can use only one style class.
Javascript Accessible Style Sheets 49
New Attributes for Existing HTML Tags
To specify that a class can apply to all elements, use the element selector
allwhen you set the properties for the class. For example, the code sample below
specifies that the class
LEMON can be applied to any element, and all elementsthat use the style class
LEMON are yellow.CSS Syntax
<STYLE TYPE="text/css">
all.LEMON {color:yellow;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
classes.LEMON.all.color="yellow";
</STYLE>
Style Sheet Use
<H1 class="LEMON">A Nice Yellow Heading</P>
<P CLASS="LEMON">What a nice shade of yellow this paragraph is.</P>
For more information about creating classes of style and for more examples,
see the section
Defining Classes of Styles in Part 3, "Creating Style Sheets andAssigning Styles."
ID
When defining style sheets, you can create individual named styles.
An element can use a style class and also use a named style. This allows you to
use named styles to express individual stylistic exceptions to a style class.
To define an individual names style in CSS syntax, you use the # sign to
indicate a name for an individual style, while In JavaScript syntax, you use the
ID selector.
In both CSS syntax and JavaScript syntax, you use the ID attribute in an HTML
element to specify the style for that element.
ID names are case-sensitive.
ID styles are particularly useful for working with layers of precisely positioned
HTML content, as discussed in
Part 2. Positioning HTML Content.The following code shows an example of the use of individual named styles. In
this example, the STYLE1 class defines a style with several characteristics. The
named style A1 specifies that the color is blue. This style can be used to specify
that a paragraph has all the style characteristics of STYLE1, except that its color
is blue instead of red.
CSS Syntax
<STYLE TYPE="text/css">
P.STYLE1 {
color:red; font-size:24pt; line-height:26pt;
font-style:italic; font-weight:bold;
}
#A1 {color: blue;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
with (classes.STYLE1.P) {
color="red";
fontSize="24pt";
lineHeight="26pt";
fontStyle="italic";
fontWeight="bold";
}
ids.A1.color= "blue";
</STYLE>
Style Sheet Use
<P CLASS="STYLE1">Big red text</P>
<P CLASS="STYLE1" ID="A1">Big blue text</P>
Javascript Accessible Style Sheets 51
New JavaScript Object Properties
New JavaScript Object Properties
This section discusses the new JavaScript object properties that are useful for
defining style sheets using JavaScript syntax.
tags
When using JavaScript syntax within the
<STYLE> element, you can set stylesby using the
tags property of the JavaScript object document.The following example uses JavaScript syntax to specify that all paragraphs
appear in red:
<STYLE TYPE="text/javascript">
tags.P.color = red;
</STYLE>
In CSS syntax, this would be:
<STYLE TYPE="text/css">
P {color:red;}
</STYLE>
The
tags property always applies to the document object for the currentdocument, so you can omit
document from the expression document.tags.For example, the following two statements both say the same thing:
document.tags.P.color = "red";
tags.P.color = "red";
To set default styles for all elements in a document, you can set the desired
style on the <
BODY> element, since all other elements inherit from <BODY>.For example, to set a universal right margin for the document:
tags.body.marginRight="20pt"; /*JavaScript syntax */
BODY {margin-right:20pt;} /* CSS syntax */
classes
See the
CLASS section for a discussion of the classes JavaScript property.ids
See the
ID section for a discussion of the ids JavaScript property.Style Sheet Properties
Font Properties
Using styles, you can specify font size, font family, font style, and font weight
for any element.
Font Size
CSS syntax name:
font-sizeJavaScript syntax name:
fontSizeabsolute-size
An absolute-size is a keyword such as:
xx-small
x-small
small
medium
large
x-large
Possible values: absolute-size, relative-size, length, percentage
Initial value:
mediumApplies to: all elements
Inherited: yes
Percentage values: relative to parent element's font size
Javascript Accessible Style Sheets 53
Style Sheet Properties
xx-large
relative-size
A relative-size keyword is interpreted relative to the font size of the parent
element. Note that relative values only equate to actual values when the
element whose font size is a relative value has a parent element that has a
font size. (A relative size has to have something to be relative to.)
Possible values are:
larger
smaller
For example, if the parent element has a font size of
medium, a value oflarger
will make the font size of the current element be large.length
A length is a number followed by a unit of measurement, such as
24pt.percentage
A percentage keyword sets the font size to a percentage of the parent
element’s font size.
CSS Syntax
P {font-size:12pt;}
EM {font-size:120%};
BLOCKQUOTE {font-size:medium;}
B {font-size:larger;}
JavaScript Syntax
tags.P.fontSize = "12pt";
tags.EM.fontSize = 120%;
tags.BLOCKQUOTE.fontSize = "medium";
tags.B.fontSize="larger";
Font Family
CSS syntax name:
font-familyJavaScript syntax name:
fontFamilyfontFamily
The fontFamily indicates the font family to use, such as Helvetica or Arial. If
a list of font names is given, the browser tries each named font in turn until
it finds one that exists on the user’s system. If none of the specified font
families are available on the user’s system, the default font is used instead.
If you link a font definition file to your web page, the font definition file
will be downloaded with the page, thus guaranteeing that all the fonts in
the definition file are available on the user’s system while the user is
viewing that page. For more information about linking fonts to a document,
see
Part 3. Downloadable Fonts.There is a set of generic family names that are guaranteed to indicate a font
on every system, but that exact font is system-dependent. The five generic
font families are:
• serif
• sans-serif
• cursive
• monospace
• fantasy
CSS Syntax Example
<STYLE TYPE="text/css">
H1 {fontFamily:Helvetica, Arial, sans-serif;}
</STYLE>
Possible values
: fontFamilyInitial value: the default font, which comes from user preferences.
Applies to: all elements
Inherited: yes
Percentage values: NA
Javascript Accessible Style Sheets 55
Style Sheet Properties
JavaScript Syntax Example
<STYLE TYPE="text/javascript">
tags.H1.fontFamily="Helvetica, Arial, sans-serif";
</STYLE>
Font Weight
CSS syntax name:
font-weightJavaScript syntax name:
fontWeightThe font weight indicates the weight of the font. For example:
CSS Syntax Example
<STYLE>
BLOCKQUOTE {font-weight: bold;}
</STYLE>
JavaScript Syntax Example
<STYLE>
tags.BLOCKQUOTE.fontWeight="bold";
</STYLE>
The possible values are
normal, bold, bolder, and lighter. You can alsospecify weight as a numerical value from 100 to 900, where 100 is the lightest
and 900 is the heaviest.
Possible values:
normal, bold, bolder, lighter, 100 -900
Initial value:
normalApplies to: all elements
Inherited: yes
Percentage values: N/A
Font Style
CSS syntax name:
font-styleJavaScript syntax name:
fontStyleThis property determines the style of the font.
The following example specifies that emphasized text within
<H1> elementsappears in italic.
CSS Syntax Example
<STYLE>
H1 EM {font-style: italic;}
</STYLE>
JavaScript Syntax Example
<STYLE>
contextual(tags.H1, tags.EM).fontStyle = "italic";
</STYLE>
Text Properties
The use of style sheets allows you to set text properties such as line height and
text decoration.
Possible values:
normal, italicInitial value:
normalApplies to: all elements
Inherited: yes
Percentage values: N/A
Javascript Accessible Style Sheets 57
Style Sheet Properties
Line Height
CSS syntax name:
line-heightJavaScript syntax name:
lineHeightThis property sets the distance between the baselines of two adjacent lines. It
applies only to block-level elements.
number
:If you specify a numerical value without a unit of measurement, the line
height is the font size of the current element multiplied by the numerical
value. This differs from a percentage value in the way it inherits: when a
numerical value is specified, child elements inherit the factor itself, not the
resultant value (as is the case with percentage and other units).
For example:
fontSize:10pt;
line-height:1.2; /* line height is now 120%, ie 12pt */
font-size:20pt; /* line height is now 24 pt, */
length
:An expression of line height as a measurement, for example:
line-height:0.4in;
line-height:18pt;
percentage
Percentage of the element’s font size, for example:
line-height:150%;
Negative values are not allowed.
Possible values number, length, percentage,
normalInitial value: normal for the font
Applies to: block-level elements
Inherited: yes
Percentage values: refers to the font size of the element itself
Text Decoration
CSS syntax name:
text-decorationJavaScript syntax name:
textDecorationThis property describes decorations that are added to the text of an element. If
the element has no text (for example, the
<IMG> element in HTML) or is anempty element (for example, "
<EM></EM>"), this property has no effect.This property is not inherited, but children elements will match their parent. For
example, if an element is underlined, the line should span the child elements.
The color of the underlining will remain the same even if child elements have
different
color values.For example:
BLOCKQUOTE {text-decoration: underline;}
The text decoration options do not include color options, since the color of text
is derived from the
color property value..
Text Transform
CSS syntax name:
text-transformJavaScript syntax name:
textTransformPossible values:
none, underline, line-through, blinkInitial value:
noneApplies to: all elements
Inherited: no, but see clarification below
Percentage values: N/A
Javascript Accessible Style Sheets 59
Style Sheet Properties
This property indicates text case.
capitalize
Display the first character of each word in uppercase.
uppercase
Display all letters of the element in uppercase.
lowercase
Display all letters of the element in lowercase.
none
Neutralizes inherited value.
For example:
CSS Syntax Example
<STYLE TYPE="text/css">
H1 {text-transform:capitalize;}
H1.CAPH1 {text-transform: uppercase;}
</STYLE>
JavaScript Syntax Example
<STYLE>
tags.H1.textTransform = "capitalize";
classes.CAPH1.H1.textTransform = "uppercase";
</STYLE>
Possible values:,
capitalize, uppercase, lowercase, noneInitial value:
noneApplies to: all elements
Inherited: yes
Percentage values: N/A
Style Sheet Use
<H1>This is a regular level-one heading</H1>
<H1 CLASS=CAPH1>important heading</H1>
Text Alignment
CSS syntax name:
text-alignJavaScript syntax name:
textAlignThis property describes how text is aligned within the element.
Example:
tags.P.textAlign = "center"
CSS Syntax Example
<STYLE TYPE="text/css">
all.RIGHTHEAD {text-align:right; color:blue;}
P.LEFTP {text-align:left; color:red;}
</STYLE>
JavaScript Syntax
<STYLE TYPE="text/javascript">
classes.RIGHTHEAD.all.textAlign="right";
classes.LEFTP.P.textAlign="left";
classes.RIGHTHEAD.all.color="blue";
classes.JUSTP.P.color="red";
Possible values:
left, right, center, justifyInitial value:
leftApplies to: block-level elements
Inherited: yes
Percentage values: N/A
Javascript Accessible Style Sheets 61
Style Sheet Properties
</STYLE>
Style Sheet Use
<H3>A Normal Heading</H3>
<H3 CLASS=RIGHTHEAD>A Right-Aligned Heading</H3>
<P>This is a normal paragraph. This is what paragraphs usually look
like, when they are left to their own devices, and you do not use style
sheets to control their text alignment.</P>
<P CLASS = LEFTP>This paragraph is left-justified, which means it has a
ragged right edge. Whenever paragraphs contain excessively, perhaps
unnecessarily, long words, the raggedness of the justification becomes
more manifestly apparent than in the case where all the words in the
sentence are short.</P>
Text Indent
CSS syntax name:
text-indentJavaScript syntax name:
textIndentThe property specifies indentation that appears before the first formatted line.
The
text-indent value may be negative. An indent is not inserted in themiddle of an element that was broken by another element (such as
<BR> inHTML).
length
Length of the indent as a numerical value with units, for example:
P {text-indent:3em;}
percentage
Possible values: length, percentage
Initial value: 0
Applies to: block-level elements
Inherited: yes
Percentage values: refer to parent element's width
Length of the indent as a percentage of the parent element’s width, for
example:
P {text-indent:25%;}
CSS Syntax Example
<STYLE TYPE="text/css">
P.INDENTED {text-indent:25%;}
</STYLE>
JavaScript Syntax Example
<STYLE TYPE="text/css">
classes.INDENTED.P.textIndent="25%";
</STYLE>
Style Sheet Use
<P CLASS=INDENTED>
The first line is indented 25 percent of the width of the parent
element, which in this case happens to be the BODY tag, since this
element is not embedded in anything else.</P>
<BLOCKQUOTE>
<P CLASS=INDENTED>
This time the first line is indented 25 percent from the blockquote that
surrounds this element. A blockquote automatically indents its contents.
</P>
</BLOCKQUOTE>
Block-Level Formatting Properties
Style sheets treat each block-level element as if it is surrounded by a box.
Block-level elements start on a new line, for example,
<H1> and <P> areblock-level elements, but
<EM> is not.Each box can have padding, border, and margins.You can set values for top,
bottom, left and right paddings, border widths, and margins.
Javascript Accessible Style Sheets 63
Style Sheet Properties
For a more detailed overview discussion of block-level formatting, see
Part 4, "Format Properties for Block-Level Elements."
Margins
CSS syntax names:
margin-left, margin-right, margin-top, marginbottom,margin
JavaScript syntax names:
marginLeft, marginRight, marginTop,marginBottom
and margins()These properties set the margin of an element. The margins express the
minimal distance between the borders of two adjacent elements.
You can set each margin individually by specifying values for
margin-left/marginLeft
, margin-right/marginRight, margin-top/marginTopand
margin-bottom/marginBottom.In CSS syntax you can set all margins to the same value at one time by setting
the
margin property (note that the property name is singular). In JavaScriptsyntax you can use the
margins() method sets the margins for all four sidesat once. (Note that the function name is plural.)
The arguments to the
margin property and margins() method are top, right,bottom and left margins respectively. For example:
CSS Syntax
/* top=10pt, right=20pt, bottom=30pt, left=40pt */
P {margin:10pt 20pt 30pt 40pt;}
/* set all P margins to 40 pt */
Possible values length, percentage,
autoInitial value: 0
Applies to: all elements
Inherited: no
Percentage values: refer to parent element's width
P {margin:40pt;}
JavaScript Syntax
/* top=10pt, right=20pt, bottom=30pt, left=40pt */
tags.BODY.margins("10pt", "20pt", "30pt", "40pt");
/* set all P margins to 40 pt */
tags.P.margins("40pt");
Adjoining margins of adjacent elements are added together, unless one of the
elements has no content, in which case its margins are ignored. For example, if
an
<H1> element with a bottom margin of 40 points, is followed by a <P>element with a top margin of 30 points, then the separation between the two
elements is 70 points. However, if the
<H1> element has content, but the <P>element is empty, then the margin between them is 40 points.
When margin properties are applied to replaced elements (such as an
<IMG>tag), they express the minimal distance from the replaced element to any of the
content of the parent element.
The use of negative margins is not recommended because it may have unpredictable
results.
For a working example of setting margins, see the section
Block-levelFormatting Overview and Example
.Padding
CSS syntax names:
padding-top, padding-right, padding-bottom,padding-left
, paddingsJavaScript syntax names:
paddingTop, paddingRight, paddingBottom,paddingLeft
, and paddings()Possible values: length, percentage
Initial value: 0
Applies to: all elements
Inherited: no
Percentage values: refer to parent element's width
Javascript Accessible Style Sheets 65
Style Sheet Properties
These properties describe how much space to insert between the border of an
element and the content (such as text or image). You can set the padding on
each side individually by specifying values for
padding-top/paddingTop,padding-right
/paddingRight, padding-left/paddingLeft andpadding-bottom
/paddingBottom.In CSS syntax you can use the
padding property (note that it is paddingsingular) to set the padding for all four sides at once. In JavaScript syntax you
can use the
paddings() method to set the margins for all four sides at once.The arguments to the
padding property (CSS syntax) and the paddings()method (JavaScript syntax) are the top, right, bottom and left padding values
respectively.
CSS Syntax
/* top=10pt, right=20pt, bottom=30pt, left=40pt */
P {padding:10pt 20pt 30pt 40pt;}
/* set the padding on all sides of P to 40 pt */
P {padding:40pt;}
JavaScript Syntax
/* top=10pt, right=20pt, bottom=30pt, left=40pt */
tags.P.paddings("10pt", "20pt", "30pt", "40pt")
/* set the padding on all sides of P to 40 pt */
tags.P.paddings("40pt");
Padding values cannot be negative.
To specify the color or image that appears in the padding area, you can set the
background color or background image of the element. For information about
setting background color, see the section
Background Color. For informationabout setting a background image, see the section
Background Image.For a working example of setting paddings, see the section
Block-levelFormatting Overview and Example
.Border Widths
CSS syntax names:
border-top-width, border-bottom-width,border-left-width
, border-right-width, border-widthJavaScript syntax names:
borderTopWidth, borderBottomWidth,borderLeftWidth
, borderRightWidth, and borderWidths()These properties set the width of a border around an element.
You can set the width of the top border by specifying a value for
bordertop-width
/borderTopWidth. You can set the width of the right border byspecifying a value for
border-right-width/borderRightWidth. Youcan set the width of the bottom border by specifying a value for
borderbottom-width
/borderBottomWidth. You can set the width of the bottomborder by specifying a value for
border-left-width/ borderLeft-Width.
In CSS syntax, you can set all four borders at once by setting the
borderwidthproperty. In JavaScript syntax you can set all four borders at once by
using the
borderWidths() function.The arguments to the
border-width property (CSS syntax) and the border-Widths()
function (JavaScript syntax) are the top, right, bottom and leftborder widths respectively.
/* top=1pt, right=2pt, bottom=3pt, left=4pt */
P {border-width:1pt 2pt 3pt 4pt;} /* CSS */
tags.P.borderWidths("1pt", "2pt", "3pt", "4pt"); /* JavaScript syntax */
/* set the border width to 2 pt on all sides */
P {border-width:40pt;} /* CSS */
tags.P.borderWidths("40pt"); /* JavaScript syntax */
For a working example of setting border widths, see the section
Block-levelFormatting Overview and Example
.Possible values: length
Initial value: none
Applies to: all elements
Inherited: no
Percentage values: N/A
Javascript Accessible Style Sheets 67
Style Sheet Properties
Border Style
CSS syntax name:
border-styleJavaScript syntax name:
borderStyleThis property sets the style of a border around a block-level element.
For the border to be visible however, you must also specify the border width.
For details of setting the border width see the section
Setting Border Widths,Color, and Style
or the section Border Widths.For an example of each of the border values, see:
borders.htm
StyleSheetExampleBorder Color
CSS name:
border-colorJavaScript syntax name:
borderColorPossible values:,
none, solid, double, inset, outset,groove, ridge
Initial value: none
Applies to: all elements
Inherited: no
Percentage values: N/A
Possible values:
none, colorvalueInitial value: none
Applies to: all elements
Inherited: no
Percentage values: N/A
This property sets the color of the border. The color can either be a named color or
a 6-digit hexadecimal value indicating a color or an rgb color value.
For a list of the named colors, see the section
Color Units.For example:
CSS Syntax
P {border-color:blue;}
BLOCKQUOTE {border-color:#0000FF;}
H1 {border-color:rgb(0%, 0%, 100%);}
JavaScript Syntax
tags.P.borderColor="blue";
tags.BLOCKQUOTE.borderColor="#0000FF";
tags.H1.borderColor="rgb(0%, 0%, 100%);
For a working example of setting border color, see the section
Block-levelFormatting Overview and Example
.Width
CSS syntax name: width
JavaScript syntax name: width
This property determines the width of an element.
Possible values: length, percentage,
autoInitial value:
autoApplies to: block-level and replaced elements
Inherited: no
Percentage values: refer to parent element's width
Javascript Accessible Style Sheets 69
Style Sheet Properties
Note that if you set the left and right margins, and also the width of a property,
the margin settings take precedence over the width setting. For example, if the
left margin setting is 25%, the right margin setting is 10%, and the width setting
is 100%, the width setting is ignored. (The width will end up being 65% total.)
CSS Syntax Example
all.NARROW {width:50%;}
all.INDENTEDNARROW {margin-left:20%; width:60%;}
JavaScript Syntax Example
classes.NARROW.all.width = "50%";
classes.INDENTEDNARROW.all.width = "60%";
classes.INDENTEDNARROW.all.marginLeft = "20%";
For a working example of setting the width of an element, see the section
Block-level Formatting Overview and Example
.Alignment
CSS syntax name: float
JavaScript syntax name: align
The
float property (CSS syntax) and align property (JavaScript syntax)determine the alignment of an element within its parent. (Note that the
textalign/textAlign
property determines the alignment of the content of textelements.)
Possible values:
left, right, noneInitial values:
noneApplies to: all elements
Inherited: no
Percentage values: N/A
The term
float is a reserved word in JavaScript, which is why the JavaScriptsyntax uses the name
align instead of float for this property.Using the
float/align property, you can make an element float to the left orthe right and indicate how other content wraps around it.
If no value is specified, the default value is
none. If the value is none, theelement is displayed where it appears in the text.
If the value is
left or right, the element is displayed on the left or the right(after taking margin properties into account). Other content appears on the
right or left side of the floating element. If the value is
left or right, theelement is treated as a block-level element.
Using the
float/align property, you can declare elements to be outside thenormal flow of elements. For example, if the
float/align property of anelement is
left, the normal flow wraps around on the right side.If you set an element’s
float/align property set, do not also specify marginsfor it. If you do, the wrapping effect will not work properly. However, if you
want a floating element to have a left or right margin, you can put it inside
another element, such as a
<DIV> block, that has the desired margins.CSS Syntax Example
<STYLE TYPE="text/css">
H4 {
width:70%;
border-style:outset;
border-width:2pt;
border-color:green;
background-color:rgb(70%, 90%, 80%);
padding:5%;
font-weight:bold;
}
H4.TEXTRIGHT {text-align:right; margin-right:30%;}
H4.TEXTRIGHT_FLOATLEFT {text-align:right; float:left;}
H4.FLOATRIGHT {float:right;}
H4.FIXED_RIGHT_MARGIN {float:right; margin-right:30%;}
Javascript Accessible Style Sheets 71
Style Sheet Properties
</STYLE>
JavaScript Syntax Example
<STYLE TYPE="text/javascript">
with (tags.H4) {
width="70%";
borderStyle="outset";
borderWidth="2pt";
borderColor="green";
backgroundColor = "rgb(70%, 90%, 80%)";
paddings("5%");
fontWeight="bold";
}
classes.TEXTRIGHT.H4.textAlign="right";
classes.TEXTRIGHT.H4.marginRight="30%;"
classes.TEXTRIGHT_FLOATLEFT.H4.textAlign="right";
classes.TEXTRIGHT_FLOATLEFT.H4.align="left";}
classes.FLOATRIGHT.H4.align="right";
classes.FIXED_RIGHT_MARGIN.H4.align="right";
classes.FIXED_RIGHT_MARGIN.H4.marginRight="30%";
</STYLE>
Style Sheet Use
<BODY>
<H4>Level-Four Heading</H4>
<P>I am a plain paragraph, positioned below a non-floating level-four
heading.
</P>
<H4 CLASS=TEXTRIGHT>H4 - My Text On Right, No Float</H4>
<P>I am also a plain paragraph, positioned below a non-floating levelfour
heading. It just happens that the heading above me has its text
alignment set to right.
</P>
<H4 CLASS = FLOATRIGHT>H4 - Float = Right</H4>
<P>I am a regular paragraph. There’s not much more you can say about me.
I am positioned after a level-four heading that is floating to the
right, so I come out positioned to the left of it.</P>
<BR CLEAR>
<H4 CLASS=TEXTRIGHT_FLOATLEFT>H4 - My Text on Right, Float = Left </H4>
<P>I'm also just a plain old paragraph going wherever the flow takes me.
</P>
<BR CLEAR>
<H4 CLASS=FIXED_RIGHT_MARGIN>H4 - Float = Right, Fixed Right Margin</H4>
<P>Hello? Hello!! I am wrapping round an H4 that is floating to the right and
has a fixed right margin. When I try to satisfy all these requirements, you see
what happens! For best results, do not set the left and/or right margin when
you set the float (CSS syntax) or align (JavaScript syntax) property. Use an
enclosing element with margins instead.
</P>
<BR CLEAR>
<DIV STYLE="margin-left:30%;">
<H4 CLASS = FLOATRIGHT>H4 - Float = Right</H4>
<P>Notice how the heading next to me seems to have a right margin.
That’s because we are both inside a DIV block that has a right margin.</
P>
<BR CLEAR>
</DIV>
</BODY>
Clear
CSS syntax name:
clearJavaScript syntax name:
clearJavascript Accessible Style Sheets 73
Style Sheet Properties
This property specifies whether an element allows floating elements on its
sides. More specifically, the value of this property lists the sides where floating
elements are not accepted. With
clear set to left, an element will be movedbelow any floating element on the left side. With
clear set to none, floatingelements are allowed on all sides.
Example:
P {clear:left;}
tags.H1.clear = "left";
Color and Background Properties
Just as you can set color and background properties for a document as a whole,
you can set them for block-level elements too. These properties are applied to
the "box" that contains the element.
Color
CSS syntax name:
colorJavaScript syntax name:
colorPossible values:
none, left, right, bothInitial value:
noneApplies to: all elements
Inherited: no
Percentage values: N/A
This property describes the text color of an element, that is, the "foreground"
color.
See the section
Color Units for information about how to specify colors.The following examples illustrate the ways to set the color to red.
CSS Syntax Example
<STYLE TYPE="text/css">
EM {color:red;}
B {color:rgb(255, 0, 0);}
I {color:rgb(100%, 0%, 0%);}
CODE {color:#FF0000;}
</STYLE>
JavaScript Syntax Example
<STYLE TYPE="text/javascript">
tags.EM.color="red";
tags.B.color="rgb(255, 0, 0)";
tags.I.color="rgb(100%, 0%, 0%)";
tags.CODE.color="#FF0000";
</STYLE>
Background Image
CSS syntax name:
background-imagePossible values: color
Initial value: black
Applies to: all elements
Inherited: yes
Percentage values: N/A
Javascript Accessible Style Sheets 75
Style Sheet Properties
JavaScript syntax name:
backgroundImageThis property specifies the background image of an element.
Partial URLs are interpreted relative to the source of the style sheet, not relative
to the document.
CSS Syntax Example
<STYLE TYPE="text/css">
H1.SPECIAL {
background-image:url(images/glass2.gif);
padding:20pt;
color:yellow;
}
H2.SPECIAL {
padding:20pt;
background-color:#FFFF33;
border-style:solid;
border-width:1pt;
border-color:black;
}
P.SPECIAL B {background-image:url(images/tile1a.gif); }
P.SPECIAL I {background-color:cyan;}
</STYLE>
JavaScript Syntax Example
Possible values: url
Initial value: empty
Applies to: all elements
Inherited: no
Percentage values: N/A
<STYLE TYPE="text/javascript">
classes.SPECIAL.H1.backgroundImage = "images/glass2.gif";
classes.SPECIAL.H1.paddings("20pt");
classes.SPECIAL.H1.color="yellow";
classes.SPECIAL.H2.paddings("20pt");
classes.SPECIAL.H2.backgroundColor="FFFF33";
classes.SPECIAL.H2.borderStyle="solid";
classes.SPECIAL.H2.borderWidth="1pt";
classes.SPECIAL.H2.borderColor="black";
contextual(classes.SPECIAL.P, tags.B).backgroundImage=
"images/tile1a.gif";
contextual(classes.SPECIAL.P, tags.I).backgroundColor="cyan";
</STYLE>
Style Sheet Use
<H1 CLASS=SPECIAL>Heading One with Image Background</H1>
<P CLASS=SPECIAL>
Hello. Notice how the portion of this paragraph that has an <B>image
background</B> is promoted to being a block-level element on its own
line.</P>
<H2 CLASS=SPECIAL>Heading Two with Solid Color Background</H2>
<P CLASS=SPECIAL>Hello, here is some <I>very interesting</I>
information. Notice that each <I>colored portion</I> of this paragraph
just continues right along in its normal place.
</P>
Background Color
CSS syntax name:
background-colorJavaScript syntax name:
backgroundColorJavascript Accessible Style Sheets 77
Style Sheet Properties
This property specifies a solid background color for an element.
See the previous section,
Background Image, for a working example.Classification Properties
These properties classify elements into categories more than they set specific
visual parameters.
Display
CSS syntax name:
displayJavaScript syntax name:
displayPossible Values: color
Initial value: empty
Applies to: all elements
Inherited: no
Percentage values: N/A
Possible values:,
block, inline, list-itemnone
Initial value: according to HTML
Applies to: all elements
Inherited: no
Percentage values: N/A
This property indicates whether an element is inline (for example,
<EM> in HTML),block-level element (for example.
<H1> in HTML), or a block-level list item (forexample,
<LI> in HTML). For HTML documents, the initial value is taken from theHTML specification.
A value of
none turns off the display of the element, including children elements andthe surrounding box. (Thus if the value is set to
none, the element is not be displayed.)Note that block-level elements do not seem to respond to having their display property
set to
inline.CSS Syntax Example
EM.LISTEM {display:list-item;}
JavaScript Syntax Example
classes.LISTEM.EM.display="list-item";
List Style Type
CSS syntax name:
list-style-typeJavaScript syntax name:
listStyleTypeThis property describes how list items (that is, elements with a
display value oflist-item
) are formatted.This property can be set on any element, and its children will inherit the value.
However, the list style is only displayed on elements that have a
display value oflist-item
. In HTML this is typically the case for the <LI> element.Possible values:
disc, circle, square, decimal, lowerroman,upper-roman, lower-alpha, upperalpha,
none
Initial value:
discApplies to: elements with
display property value of list-itemInherited: yes
Percentage values: N/A
Javascript Accessible Style Sheets 79
Style Sheet Properties
CSS Syntax Example
<STYLE TYPE="text/css">
UL.BLUELIST {color:blue;}
UL.BLUELIST LI {color:aqua;list-style-type:square;}
OL.REDLIST {color:red;}
OL.REDLIST LI {color:magenta; list-style-type:upper-roman;}
</STYLE>
JavaScript Syntax Example
<STYLE TYPE="text/javascript">
classes.BLUELIST.UL.color="blue";
contextual(classes.BLUELIST.UL, tags.LI).color="aqua";
contextual(classes.BLUELIST.UL, tags.LI).listStyleType="square";
classes.REDLIST.OL.color="red";
contextual(classes.REDLIST.OL, tags.LI).color="magenta";
contextual(classes.REDLIST.OL, tags.LI).listStyleType="upper-roman";
</STYLE>
Style Sheet Use
<UL CLASS=BLUELIST> <!-- LI elements inherit from UL -->
<LI>Consulting
<LI>Development
<LI>Technology integration
</UL>
<OL CLASS=REDLIST> <!-- LI elements inherit from OL -->
<LI>Start the program.
<LI>Enter your user name and password.
<LI>From the File menu, choose the Magic command.
</OL>
White Space
CSS syntax name:
white-spaceJavaScript syntax name:
whiteSpaceThis property declares how white space inside the element should be handled.
The choices are:
•
normal (white space is collapsed),•
pre (behaves like the <PRE> element in HTML) .For example:
P.KEEPSPACES {white-space:pre;} /* CSS syntax */
classes.KEEPSPACES.P.whiteSpace = "pre"; /* JavaScript syntax */
Units
This section discusses units of measurement.
Length Units
The format of a length value is an optional sign character ('+' or '-', with '+'
being the default) immediately followed by a number followed by a unit of
measurement. For example,
12pt, 2em, 3mm.Possible values:
normal, preInitial value: according to HTML
Applies to: block-level elements
Inherited: yes
Percentage values: N/A
Javascript Accessible Style Sheets 81
Units
There are three types of length units: relative, pixel and absolute. Relative units
specify a length relative to another length property. Style sheets that use relative
units will scale more easily from one medium to another (for example, from a
computer display to a laser printer). Percentage units and keyword values (such
as
x-large) offer similar advantages.Child elements inherit the computed value, not the relative value, for example:
BODY {font-size:12pt; text-indent:3em;}
H1 {font-size:15pt;}
In the example above, the text indent value of
H1 elements will be 36pt, not45pt.
The following relative units are supported:
•
em -- the height of the element's font, typically the width or height of thecapital letter M
•
ex -- half the height of the element’s font, which is typically the height ofthe letter 'x'
•
px -- pixels, relative to rendering surfaceThe following absolute units are supported:
•
pt -- points•
pc -- picas•
px -- pixels•
in -- inches•
mm -- millimeters•
cm -- centimeters
Color Units
A color value is a either a color name or a numerical RGB specification.
The suggested list of color names is: aqua, black, blue, fuchsia, gray, green,
lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow. These 16
colors are taken from the Windows VGA palette and will also be used in HTML
3.2.
tags.BODY.color = "black";
tags.backgroundColor = "white";
tags.H1.color = "maroon";
tags.H2.color = "olive";
You can specify an RGB color by a six digit hexadecimal number where the
first two digits indicate the red value, the second two digits indicate the green
value, and the last two digits indicate the blue value. For example:
BODY {color: #FF0000}; /* red */
BODY {background-color:#333333";} /* gray */
You can also specify an RGB color by using the
rgb() function which takesthree arguments, for the red, green, and blue values. Each color value can
either be an integer from 0 to 255 inclusive, or a percentage, as in this example:
P {color: rgb(200, 20, 240);) /* bright purple */
BLOCKQUOTE {background-color: rgb(100%, 100%, 20%); /* bright yellow */