Cascading Stylesheets and You (2)

Objectives

Learn about the core components of CSS2.1 and how they can be used to present your content.

Sample CSS Rule

body { background: url(bgimage.gif); }

Anatomy of a CSS Rule

Anatomy of: body { background: url(bgimage.gif); }

Sample Document

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <title>Example Document</title>

  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  <link rel="stylesheet" href="sample.css" type="text/css" media="all" />
</head>
<body>
  <div>
  	<p>Getting ahead in the <em>fast</em> paced 
	world of tortoise racing.</p>

 </div>
</body>
</html>

Document Tree

Document Tree

Parents and Children

parent-child

Parents and Children

parent-child

Parents and Children

parent-child

Siblings

siblings

Descendant

Descendant

Ancestor

ancestor

Inheritance

Types of CSS Selectors

Type Selector

From the CSS2.1 Specification:

"A type selector matches the name of a document language element type. A type selector matches every instance of the element type in the document tree."

Example of a Type selector:

h1 { ... }

Class Selector

Custom selector that matches any element with a matching class attribute, or any specific element with a matching class attribute.

Examples of Class selectors:

.myClass { ... }

div.myClass { ... }

ID Selector

An ID is unique to one element in an HTML/XHTML document. ID selectors are used to match an element with a matching ID attribute. Can also match a specific element with a matching ID.

Examples of ID selectors:

#myID { ... }

div#myID { ... }

If an ID can only be used once, why would I ever need use the second selector?

Descendant Selector

A Descendant selector is used to match an element that is a descendant of a defined ancestor element.

Example of a Descendant selector:

p a { ... }

This would match any a element that is a descendant of a p element.

Used intelligently, descendant selectors can relieve your HTML documents of class-itis.

Universal Selector

The universal selector (represented by an asterisk "*") is used to match any element.

Example of the universal selector:

* { color: red; }

The above rule would make the foreground (text) color of every element in an HTML document red.

Combining Selector Types for Fun and Profit

Some examples of complex selectors which incorporate one or more selector types:

Advanced CSS 2.1 Selectors

Cascading: Putting the 'C' in CSS

Style rules can come from any or all of these sources:

Cascading: Putting the 'C' in CSS

Cascading Order

Weight is determined by importance (normal or important) and origin (author, user, or user agent):

(most important to least important)

  1. user important style sheets
  2. author important style sheets
  3. author normal style sheets
  4. user normal style sheets
  5. user agent style sheets

Cascading Order (Cont.)

Specificity

Calculating Specificity (Simple Version)

  1. Count each ID attribute in the selector
  2. Count each other attribute in the selector (i.e. classes)
  3. Count each element in the selector

Create a final value by concatenating A, B, and C

Example Calculations

#home div p a {}

A-B-C -> 1-0-3 = 103

Example Calculations

body#home .content p a {}

A-B-C -> 1-1-3 = 113

Shorthand Properties

There are many ways to say the same thing:

div {
     border-top-width: 1px;
     border-right-width: 1px;
     border-bottom-width: 1px;
     border-left-width: 1px;
     border-top-style: solid;
     border-right-style: solid;
     border-bottom-style: solid;
     border-left-style: solid;
     border-top-color: #000000;
     border-right-color: #000000;
     border-bottom-color: #000000;
     border-left-color: #000000;
}

Shorthand Properties

Could also be written as:

div {
     border: 1px solid #000;
}

For more information see Efficient CSS with Shorthand Properties by Roger Johansson.

...

Move on to the next set of slides