Client-side
CSS

Parke Godfrey
26 October 2011
2 November 2011
CSE-2041

Credits

These slides are based in part on ones from the following sources.

How to style your page?

Style is format and layout.

Separate style and content

Markup is then cleaner, simpler, and easier to maintain.


Goals

What separating style and content buys us

Cascading Style Sheets (CSS)

The Web standards for style directives are style sheets, specifically Cascading Style Sheets (CSS).


Hooks into HTML source

CSS Syntax


See W3School's CSS2 Synatx for the syntax.

See W3 Org's CSS2's Selectors for a guide to more advanced selector syntax.

How are CSS style sheets attached to a document?


Style can also be marked up in the document itself (in the <body>).

What is cascading about them?

Rule Precedence
& The “Unified” Style Sheet

How does the browser decide which style rule to apply to an element

  • if more than one rule applies, and

  • they conflict?

The browser engine consolidates all attached style sheets — external (<link>) and in-document (<style> in <head>) — together into what I will call the unified style sheet for the document.

Precedence of Style Markup

Style directives take precedence as follows. (Top of the list is highest, bottom is lowest.)

Cascading the Style Sheets
into a “unified” style sheet

How are the attached style sheets cascaded together (into the unified style sheet)?

All external CSS sheets (<link>) are appended in the order called, then the in-document style sheets are appended in the order they appear.


Author / user & important / normal

Pieces of the consolidation — no matter where they came from — are re-arranged according to the user-author, important-normal precedence:

  • user important

  • author important

  • author normal

  • user normal

Rule Matching

Which CSS rule — from the unified spread sheet — applies to a given element? First,


more specific selector > more general selector

Then, of the equally best matching rules that apply to the element,


later > earlier

That is, lower in the unified style sheet wins.


Note that this is done separately for each style element!

E.g., font-family and color.

Style Inheritance

CSS Layout

CSS uses the box model.

Boxes can be nested.


Basic rules:


See BrainJar's CSS Positioning tutorial on box structure, positioning, and flow.