Craft > Cascading Style Sheet Tips

SwansonSoftware Home | Site Index | View With Frames

Final Version 1.11, updated January 30 2004

Contents

  1. Introduction
  2. Using CSS
  3. Inheritance
  4. The Cascade
  5. Units
  6. Selectors
  7. Examples
    1. browser-specific stylesheets - use javascript to determine which stylesheet to use for a particular browser, and to write the link to the stylesheet
    2. Paragraph element - sets margins for paragraph text
    3. Using @import
    4. Anchor Pseudo-classes - using the anchor pseudo-classes
    5. Anchor Pseudo-classes 2 - combining normal classes with pseudo-classes
  8. Links to More Information
  9. References

Introduction

Next | Top of page

The Cascading Style Sheets (CSS) standard has evolved from level 1 (CSS1) in 1996, through CSS2 in 1998, and the CSS Mobile Profile specification in 2001. Currently CSS3 is under development. Except where noted, when you see a reference to CSS on SwansonSoftware you can assume the reference is to CSS2.

CSS2 builds on CSS1 with addition of media-specific styles that allow authors to tailor documents for rendering with handheld and other devices, and in formats such as aural, braille, and print. CSS2 adds support for downloadable fonts, table layout, content positioning capabilities beyond those available with CSS1, internationalization, etc.

Features available with the new CSS and HTML standards allow authors to create Web pages that are accessible to an audience that includes people with impairments and people who browse the Web using a device other than a pc with full-size screen.

Standards information for Cascading Style Sheets (CSS) is available free from the World Wide Web Consortium (W3C).


Using CSS

Next | Previous | Top of page

Style sheets have the job of separating content from presentation. They are supposed to help maintain a consistent look across all pages on your site, but designing and implementing stylesheets in your pages can be time consuming and frustrating. The goal of this document is to save you time and frustration - the approach is to show you what you need to know to avoid the pitfalls.

Every CSS-capable browser has a default style it applies when no stylesheet is specified. When you view an html page that uses no stylesheet and sets no style properties, the browser uses its default styles to display the page. Note that all properties have a default value. For example, if you do not set the font color property in a <DIV> tag and the property is not set anywhere in the heirarchy above, the default value will be used.

Here is a checklist you can use when you have trouble with styles.


You Can Use More Than One Stylesheet

If you use lots of styles for a site and put every style in one file, it can become difficult to manage. You can increase modularity and reuse by splitting off commonly used styles into a separate style sheet. See the example on @import for ideas.

Some Things to Watch Out For

Annotated from Jakob Nielsen's
Alertbox, July 1997



Inheritance

Next | Previous | Top of page

Many style properties - particularly font properties - inherit. So not setting a font color property in a <DIV> tag will cause the text in the div to inherit that property from a tag higher up in the heirarchy.

Not all properties can be inherited. Font properties inherit (font-family, font-style, font-variant, font-weight, font-size, font), and most text properties inherit (letter-spacing, text-transform, text-align, text-indent, line-height).

For example, <DIV> and <P> tags will inherit font properties set in the <BODY> tag. If a font color is set in the <DIV> tag but not in the <BODY> tag, the <DIV> tag will inherit other font properties from the <BODY> tag but the font color will be as set in the <DIV> tag.

Note that <table> tags in IE don't inherit the font color set in a <div> tag. The <table> tag will inherit the browser's default font color property in this case.


The Cascade

Next | Previous | Top of page

The basic rule is: later styles supersede previous styles. For example, take the following:


/* sample css file, saved as styl.css */
div{
   font-size: 24pt;
   color: red;
}


<html>
<head>
   <link rel="stylesheet" type="text/css" href="styl.css">

   <style type="text/css">
      div{
         color:green;
         font-size:44pt;
      }
   <style>

</head>

<body>

<div>
Hello in green, 44pt
</div>

<div style="color:blue;">
Hello in blue, 44pt
</div>

</body>
</html>

In this example, the linked style sheet appears above the style rule in the html document, so the DIV style in the HEAD section supersedes the style in the .css file. If the .css file LINK element appeared after the STYLE element in the html document, the DIV style in the .css file would supersede the DIV style in the document. Also note that the inline style in the last DIV element sets the font color to blue, superseding both the .css file and the STYLE in the head element.


Units

Next | Previous | Top of page

Units are either absolute or relative. Always try to use relative units. If you use absolute units for font size, users will not be able to adjust text size using menu options in Internet Explorer, and it will adjust inconsistently in older versions of Netscape if you are using font tags and styles.

Relative Units


Absolute Units


Percentages

Percentage values are always relative to another value, and the property determines what they are relative to. Percentage values may be negative where the property allows this.

Integers

Sometimes an integer can be used:

   height="23"


CSS Selectors

Next | Previous | Top of page

Selectors are used to specify a style for an element. A CSS style rule contains the following:

selector{[property:value;]*}

There are several types of selectors, each with its own characteristic syntax.

Note on naming class selectors: Be careful how you name class selectors. Do not use underscores in the name, some browsers (e.g. Netscape 4.01) will not recognize names with underscores.

Type Description
Simple Either a universal or a type selector possibly followed by attribute or id selectors. Case is insensitive for HTML, sensitive for XML.
  Syntax element{attribute:property}
  Example div{font-size:12pt}
Universal Uses the wildcard selector * to match every element. You can make the selector apply to specific elements by applying an attribute.
  Syntax *{attribute:property}
  Example *{font-size:12pt}
  Syntax *[attribute]{attribute:property}
  Example *[lang=fr]{font-size:12pt; font-weight:bold}
Type Matches the element type.
  Syntax element{attribute:property}
  Example div{font-family:sans-serif}
Grouped Several elements that take the same property. Separate element names with a comma.
  Syntax element1,element2,element3{attribute:property}
  Example h1,h2,h3{font-family:sans-serif}
Descendant Matches the descendent element DE that has ancestor AE.
  Syntax AE DE{attribute:property}
  Example ol ol{margin:0.8em 0in .8em 0.3in;}
Child Matches the child element CE that has parent PE.
  Syntax PE>CE{attribute:property}
  Example li > ol{margin:0.8em 0in .8em 0.3in;}
Adjacent Matches an element E that immediately follows an element F.
  Syntax E+F{attribute:property}
  Example div+p{font-weight:bold}
Attribute There are four usage models
  Syntax 1 attribute [attribute]
  Example div [LANG] {font-size:14pt}
  Syntax 2 value [attribute=value]
  Example div [LANG=fr] {font-size:14pt}
  Syntax 3 hyphen-separated value [attribute|=value]
  Example div [LANG|=en] {font-size:14pt}
  Syntax 4 space-separated list of values [attribute~=value]
  Example div [rel~= "copyright"] {color:green}
Class Matches elements whose CLASS attribute has the class name.
  Syntax element.ClassName{attribute:property}
  Example h1.header{color:red}
ID Matches a unique element UE - this element should appear only once in the document.
  Syntax #UE{attribute:property}
  Example #h1MainHeader{font-size:160%}
Pseudo-element first-line Applies a style to the first line of text in any element E.
  Syntax E:first-line{attribute:property}
  Example p:first-line{font-weight:bold}
Pseudo-element first-letter Applies a style to the first letter of text in any element E.
  Syntax E:first-letter{attribute:property}
  Example p:first-letter{font-size:200%; font-family:"times new roman"}
Pseudo-class link This can take two values: A:link and A:visited
  Syntax a:link{attribute:property}
a:visited{attribute:property}
  Example a:link{color:blue}
a:visited{color:blue}
Pseudo-class dynamic This can take three values: :hover or :active or :focus. They are used for providing interactivity.
  Syntax a:hover{attribute:property}
a:active{attribute:property}
a:focus{attribute:property}
  Example a:hover{text-decoration:underline;}
a:active{text-decoration:none;}
a:focus{background:yellow;}
Pseudo-class language Matches element E if the element is in language C. The common example is to set correct quotation marks for the language.
  Syntax E:lang(c){attribute:property}
  Example html:lang(fr) {quotes:'< ' ' >'}
html:lang(de) {quotes:'>' '<' '\2039' '203A'}


Links to More Information

Next | Previous | Top of page

Reference

Name Rating Description
W3.org Best W3C - this should be the starting point for studying Internet technologies Besides having the CSS specification, there are tutorials and book listings.
MS Web Best * Microsoft Web Development Reference
Rich In Style Good Tables of browser support and bugs for CSS properties - they also have good tutorials.
blooberry.com Good Difficult to navigate but good reference

Tutorials

Name Rating Description
W3Schools Good Comprehensive list of tutorials on web development technologies.

* IE - specific


References


Cascading Style Sheets, level 2, CSS2 Specification: W3C REC-CSS2-19980512

W3.org - could rank as most improved Web site - the source for definitive references on internet technologies. When in doubt, this is the place to go for answers.

Boumphrey, Frank, 1998, Professional Style Sheets for HTML and XML: Wrox Press Ltd., 673 pages. ISBN 1-861001-65-7
Some of the information on this page is modified from this book.


Top of page | Previous