Section 16.1. Overview of CSS


16.1. Overview of CSS

Styles in CSS are specified as a semicolon-separated list of name/value attribute pairs, in which colons separate each name and value. For example, the following style specifies bold, blue, underlined text:

 font-weight: bold; color: blue; text-decoration: underline; 

The CSS standard defines quite a few style attributes. Table 16-1 lists most of them, omitting a few poorly supported ones. You are not expected to understand or be familiar with all these attributes, their values, or their meanings. As you become familiar with CSS and use it in your documents and scripts, however, you may find this table a convenient quick reference. For more complete documentation on CSS, consult Cascading Style Sheets: The Definitive Guide by Eric Meyer (O'Reilly) or Dynamic HTML: The Definitive Guide by Danny Goodman (O'Reilly). Or read the specification itself: you can find it at http://www.w3c.org/TR/CSS21/.

Table 16-1. CSS 2.1 style attributes and their values

Name

Values

background

[ background-color || background-image || background-repeat || background-attachment || background-position ]

background-attachment

scroll | fixed

background-color

color | TRansparent

background-image

url(url)| none

background-position

[ [ percentage | length ]{1,2} | [ [top | center | bottom ] || [ left | center | right ] ] ]

background-repeat

repeat | repeat-x | repeat-y | no-repeat

border

[ border-width || border-style || color ]

border-collapse

collapse | separate

border-color

color{1,4} | TRansparent

border-spacing

length length?

border-style

[ none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset ]{1,4}

border-top

[ border-top-width || border-style || [ color | transparent ] ]

border-right

border-bottom

border-left

border-top-color

color | TRansparent

border-right-color

border-bottom-color

border-left-color

border-top-style

none | hidden | dotted | dashed | solid | double | groove | ridge | inset |

border-right-style

outset

border-bottom-style

border-left-style

border-top-width

thin | medium | thick | length

border-right-width

border-bottom-width

border-left-width

border-width

[ thin | medium | thick | length ]{1,4}

bottom

length | percentage | auto

caption-side

top | bottom

clear

none | left | right | both

clip

[ rect( [ length | auto ]{4} )] | auto

color

color

content

[ string | url(url) | counter | attr(attribute-name) | open-quote | close-quote | no-open-quote | no-close-quote ]+ | normal

counter-increment

[ identifier integer? ]+ | none

counter-reset

[ identifier integer? ]+ | none

cursor

[ [ url(url) ,]* [ auto | crosshair | default | pointer | progress | move | e-resize | ne-resize | nw-resize | n-resize | se-resize | sw-resize | s-resize | w-resize | text | wait | help ] ]

direction

ltr | rtl

display

inline | block | inline-block | list-item | run-in | table | inline-table | table-row-group | table-header-group | table-footer-group | table-row | table-column-group | table-column | table-cell | table-caption | none

empty-cells

show | hide

float

left | right | none

font

[ [font-style||font-variant||font-weight]? font-size[/line-height]? font-family]|caption|icon|menu|message-box|small-caption|status-bar

font-family

[[ family-name | serif | sans-serif | monospace | cursive | fantasy ],]+

font-size

xx-small | x-small | small | medium | large | x-large | xx-large | smaller | larger | length | percentage

font-style

normal | italic | oblique

font-variant

normal | small-caps

font-weight

normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900

height

length | percentage | auto

left

length | percentage | auto

letter-spacing

normal | length

line-height

normal | number | length | percentage

list-style

[ list-style-type || list-style-position || list-style-image ]

list-style-image

url(url) | none

list-style-position

inside | outside

list-style-type

disc | circle | square | decimal | decimal-leading-zero | lower-roman | upper-roman | lower-greek | lower-alpha | lower-latin | upper-alpha | upper-latin | hebrew | armenian | georgian | cjk-ideographic | hiragana | katakana | hiragana-iroha | katakana-iroha | none

margin

[ length | percentage | auto ]{1,4}

margin-top

length | percentage | auto

margin-right

margin-bottom

margin-left

marker-offset

length | auto

max-height

length | percentage | none

max-width

length | percentage | none

min-height

length | percentage

min-width

length | percentage

outline

[ outline-color || outline-style || outline-width ]

outline-color

color | invert

outline-style

none | hidden | dotted | dashed | solid | double | groove | ridge | inset | outset

outline-width

thin | medium | thick | length

overflow

visible | hidden | scroll | auto

padding

[length | percentage]{1,4}

padding-top

length | percentage

padding-right

padding-bottom

padding-left

page-break-after

auto | always | avoid | left | right

page-break-before

auto | always | avoid | left | right

page-break-inside

avoid | auto

position

static | relative | absolute | fixed

quotes

[string string]+ | none

right

length | percentage | auto

table-layout

auto | fixed

text-align

left | right | center | justify

text-decoration

none | [ underline || overline || line-through || blink ]

text-indent

length | percentage

text-TRansform

capitalize | uppercase | lowercase | none

top

length | percentage | auto

unicode-bidi

normal | embed | bidi-override

vertical-align

baseline | sub | super | top | text-top | middle | bottom | text-bottom | percentage | length

visibility

visible | hidden | collapse

white-space

normal | pre | nowrap | pre-wrap | pre-line

width

length | percentage | auto

word-spacing

normal | length

z-index

auto | integer


The second column of Table 16-1 shows the allowed values for each style attribute. It uses the grammar used by the CSS specification. Items in fixed-width font are keywords and should appear exactly as shown. Items in italics specify a datatype such as a string or a length. Note that the length type is a number followed by a units specification, such as px (for pixels). See a CSS reference for details on the other types. Items that appear in italic fixed-width font represent the set of values allowed by some other CSS attribute. In addition to the values shown in the table, each style attribute may have the value "inherit" to specify that it should inherit the value from its parent.

Values separated by a | are alternatives; you must specify exactly one. Values separated by || are options; you must specify at least one, but you may specify more than one, and they can appear in any order. Square brackets [] group values. An asterisk (*) specifies that the previous value or group may appear zero or more times, a plus sign (+) specifies that the previous value or group may appear one or more times, and a question mark (?) specifies that the previous item is optional and may appear zero or one time. Numbers within curly braces specify a number of repetitions. For example, {2} specifies that the previous item must be repeated twice, and {1,4} specifies that the previous item must appear at least once and no more than four times. (This repetition syntax may seem familiar: it is the same one used by JavaScript regular expressions, discussed in Chapter 11.)

The CSS standard allows certain style attributes that are commonly used together to be combined using special shortcut attributes. For example, the font-family, font-size, font-style, and font-weight attributes can all be set at once using a single font attribute:

 font: bold italic 24pt helvetica; 

The margin and padding attributes are shortcuts for attributes that specify margins, padding, and borders for each of the individual sides of an element. Thus, instead of using the margin attribute, you can use margin-left, margin-right, margin-top, and margin-bottom, and the same goes for padding.

16.1.1. Applying Style Rules to Document Elements

You can apply style attributes to the elements of a document in a number of ways. One way is to use them in the style attribute of an HTML tag. For example, to set the margins of an individual paragraph, you can use a tag like this:

 <p style="margin-left: 1in; margin-right: 1in;"> 

One of the important goals of CSS is to separate document content and structure from document presentation. Specifying styles with the style attribute of individual HTML tags does not accomplish this (although it can be a useful technique for DHTML). To achieve the separation of structure from presentation, use stylesheets, which group all the style information into a single place. A CSS stylesheet consists of a set of style rules. Each rule begins with a selector that specifies the document element or elements to which it applies, followed by a set of style attributes and their values within curly braces. The simplest kind of rule defines styles for one or more specific tag names. For example, the following rule sets the margins and background color for the <body> tag:

 body { margin-left: 30px; margin-right: 15px; background-color: #ffffff } 

The following rule specifies that text within <h1> and <h2> headings should be centered:

 h1, h2 { text-align: center; } 

In the previous example, note how a comma is used to separate the tag names to which the styles are to apply. If the comma is omitted, the selector specifies a contextual rule that applies only when one tag is nested within another. For example, the following rules specify that the text inside <blockquote> tags is displayed in an italic font, but text inside an <i> tag inside a <blockquote> is displayed in plain, nonitalic text:

 blockquote { font-style: italic; } blockquote i { font-style: normal; } 

Another kind of stylesheet rule uses a different selector to specify a class of elements to which its styles should be applied. The class of an element is defined by the class attribute of the HTML tag. For example, the following rule specifies that any tag with the attribute should be displayed in bold:

 .attention { font-weight: bold; } 

Class selectors can be combined with tag name selectors. The following rule specifies that when a <p> tag has the attribute, it should be displayed in red, in addition to being displayed in a bold font (as specified by the previous rule):

 p.attention { color: red; } 

Finally, stylesheets can contain rules that apply only to individual elements that have a specified id attribute. The following rule specifies that the element with an id attribute equal to p1 should not be shown:

 #p1 { visibility: hidden; } 

You've seen the id attribute before: it is used with the document method getElementById() to return individual elements of a document. As you might imagine, this kind of element-specific stylesheet rule is useful when you want to manipulate the style of an individual element. Given the previous rule, for example, a script might switch the value of the visibility attribute from hidden to visible, causing the element to dynamically appear. You'll see how to do this later in this chapter.

The CSS standard defines a number of other selectors beyond the basic ones shown here, and some are well supported by modern browsers. Consult the CSS spec or a reference book for details.

16.1.2. Associating Stylesheets with Documents

You can incorporate a stylesheet into an HTML document by placing it between <style> and </style> tags within the <head> of the document. For example:

 <html> <head><title>Test Document</title> <style type="text/css"> body { margin-left: 30px; margin-right: 15px; background-color: #ffffff } p { font-size: 24px; } </style> </head> <body><p>Testing, testing</p> </html> 

When a stylesheet is to be used by more than one page on a web site, it is usually better to store it in its own file, without any enclosing HTML tags. This CSS file can then be included in the HTML page. Unlike the <script> tag, however, the <style> tag does not have a src attribute. To include a stylesheet into an HTML page, use the <link> tag instead:

 <html> <head><title>Test Document</title> <link rel="stylesheet" href="mystyles.css" type="text/css"> </head> <body><p>Testing, testing</p> </html> 

You can also use a <link> tag to specify an alternate stylesheet. Some browsers (such as Firefox) allow the user to select one of the alternatives you have provided (by choosing View

If your web page includes a page-specific stylesheet with a <style> tag, you can use the CSS @import directive to include a shared CSS file into the page-specific stylesheet:

 <html> <head><title>Test Document</title> <style type="text/css"> @import "mystyles.css"; /* import a common stylesheet */ p { font-size: 48px; }  /* override imported styles */ </style> </head> <body><p>Testing, testing</p> </html> 

16.1.3. The Cascade

Recall that the C in CSS stands for "cascading." This term indicates that the style rules that apply to any given element in a document can come from a cascade of different sources. Each web browser typically has its own default styles for all HTML elements and may allow the user to override these defaults with a user stylesheet. The author of a document can define stylesheets within <style> tags or in external files that are linked in or imported into other stylesheets. The author may also define inline styles for individual elements with the HTML style attribute.

The CSS specification includes a complete set of rules for determining which rules from the cascade take precedence over the other rules. Briefly, however, what you need to know is that the user stylesheet overrides the default browser stylesheet, author stylesheets override the user stylesheet, and inline styles override everything. The exception to this general rule is that user style attributes whose values include the !important modifier override author stylesheets. Within a stylesheet, if more than one rule applies to an element, styles defined by the most specific rule override conflicting styles defined by less specific rules. Rules that specify an element id are the most specific. Rules that specify a class are next. Rules that specify only tag names are the least specific, but rules that specify multiple nested tag names are more specific than rules that specify only a single tag name.

16.1.4. Versions of CSS

CSS is a relatively old standard. CSS1 was adopted in December 1996 and defines attributes for specifying colors, fonts, margins, borders, and other basic styles. Browsers as old as Netscape 4 and Internet Explorer 4 include substantial support for CSS1. The second edition of the standard, CSS2, was adopted in May 1998; it defines a number of more advanced features, most notably support for absolute positioning of elements. At the time of this writing, CSS2 features are reasonably well supported by current browsers. The crucial positioning features of CSS2 began the standardization process as part of a separate, earlier, CSS-Positioning (CSS-P) effort, and therefore these DHTML-enabling features are available in practically every browser deployed today. (These important positioning-related styles are discussed in detail later in this chapter.)

Work continues on CSS. At the time of this writing, the CSS 2.1 specification is almost complete. It clarifies the CSS 2 specification, fixes errors, and deletes some CSS 2 styles that were never actually implemented by browsers. For version 3, the CSS specification has been broken into various specialized modules that are going through the standardization process separately. Some CSS3 modules are nearing completion, and some browsers are beginning to implement selected CSS3 features, such as the opacity style. You can find the CSS specifications and working drafts at http://www.w3.org/Style/CSS/.

16.1.5. CSS Example

Example 16-1 is an HTML file that defines and uses a stylesheet. It demonstrates the previously described tag name, class, and ID-based style rules, and it also has an example of an inline style defined with the style attribute. Figure 16-1 shows how this example is rendered in a browser. Remember that this example is meant only as an overview of CSS syntax and capabilities. Full coverage of CSS is beyond the scope of this book.

Figure 16-1. A web page styled with CSS


Example 16-1. Defining and using Cascading Style Sheets

 <head> <style type="text/css"> /* Specify that headings display in blue italic text. */ h1, h2 { color: blue; font-style: italic } /*  * Any element of  displays in big bold text with large margins  * and a yellow background with a fat red border.  */ .WARNING {           font-weight: bold;           font-size: 150%;           margin: 0 1in 0 1in; /* top right bottom left */           background-color: yellow;           border: solid red 8px;           padding: 10px;       /* 10 pixels on all 4 sides */ } /*  * Text within an h1 or h2 heading within an element with   * should be centered, in addition to appearing in blue italics.  */ .WARNING h1, .WARNING h2 { text-align: center } /* The single element with  displays in centered uppercase. */ #P23 {       text-align: center;       text-transform: uppercase; } </style> </head> <body> <h1>Cascading Style Sheets Demo</h1> <div > <h2>Warning</h2> This is a warning! Notice how it grabs your attention with its bold text and bright colors. Also notice that the heading is centered and in blue italics. </div> <p > This paragraph is centered<br> and appears in uppercase letters.<br> <span style="text-transform: none"> Here we explicitly use an inline style to override the uppercase letters. </SPAN> </p> 




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net