CSCI E-75: Cascading Style Sheets (CSS)

April 21, 2008

David P. Heitmeyer
david_heitmeyer@harvard.edu

Harvard University
Division of Continuing Education
Extension School

Cascading Style Sheets (CSS)

Markup and Presentation Intertwined

Symptoms

  • proprietary HTML extensions
  • making text into images
  • proliferate use of "spacer" 1px by 1px GIF images
  • deprecated HTML elements and/or attributes
  • using tables for layout

Consequences

  • Changing design can be a major undertaking
  • HTML/XHTML very complex; can easily lead to errors
  • Unnecessary content constraints

Separated

  • Allows structure and presentation to be separate.
  • Allows greater control over presentation than XHTML/HTML alone.
  • Improves accessibility.
  • Simplifies XHTML/HTML markup, both in term of structure and size

The Power of Stylesheets

W3C Core Styles applied to the US Constitution.
constitution constitution
constitution constitution
constitution constitution
constitution constitution

Harvard College Admissions

Harvard College Admissions

admissions site

With CSS disabled:

admissions site no css

Headings

Heading elements (h1,h2,etc.) combined with CSS are very powerful. Headings can remain headings in markup and CSS can style them as desired.

Harvard College Admissions page with headings highlighted

Lists

Lists combined with CSS are very powerful. Lists can remain lists in markup (navigation, content items, etc.) and CSS can style them as desired.

Harvard College Admissions page with lists highlighted

Department of African and African American Studies

Department of African and African American Studies

AAAS site  AAAS site

With CSS disabled:

AAAS site no css

Designing with Stylesheets: CSS Zen Garden

css Zen Garden: The Beauty in CSS Design. A demonstration of what can be accomplished visually through CSS-based desgin.

Zen Garden No CSS Zen Garden Source
Zen Garden CSS Zen Garden CSS
Zen Garden CSS Zen Garden CSS
Zen Garden CSS Zen Garden CSS

XHTML: a simple example with CSS

simple XHTML simple XHTML with CSS

And here is the stylesheet (simple-style.css):

CSS Recommendations from the W3C

web standards timeline

CSS Validator

W3C CSS Validation Service
http://jigsaw.w3.org/css-validator/

Valid CSS!

CSS Resources

A List Apart
www.alistapart.com
Simplebits
www.simplebits.com
Eric Meyer CSS
meyerweb.com/eric/css/
WestCiv
www.westciv.com/style_master/house

Books

book book book book book book book

Specifications / Resources

Binding Styles to Markup

Three ways to bind CSS rules to XHTML/HTML markup:

style attribute

Example 10.1
Example 10.1 Source:
Example 10.1 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis.


style element

Example 10.2
Example 10.2 Source:
In style element (<style type="text/css">) within head element:
Example 10.2 Rendered:

link element

The link element can reference an external stylesheet.
Example 10.3
Example 10.3 Source:
In example3.css
In head element:
Example 10.3 Rendered:

Anatomy of a CSS Rule

Example 10.4
Example 10.4 Source:
In style element (<style type="text/css">) within head element:
Example 10.4 Rendered:

css rule

CSS Rule

css rule

Selector and Declarations

css selector and declarations

Properties and Values

css property and value

Combining Rules

Rules can be combined. The following two sets of style rules would produce identical results. Rules can be listed separately:

Or, rules can be grouped. Property:Value pairs need to be separated by a semicolon.

Combining Selectors

Selectors can be combined into comma-separated groups.
We combine the selectors so that a single declaration applies to multiple selectors.

class selectors

The class and id attributes of XHTML elements can be used in conjunction with styles.
Example 10.5
Example 10.5 Source:
In style element (<style type="text/css">) within head element:
Example 10.5 Rendered:

id selectors

Example 10.6
Example 10.6 Source:
In style element (<style type="text/css">) within head element:
Example 10.6 Rendered:

Contextual Selectors

Example 10.7
Example 10.7 Source:
In style element (<style type="text/css">) within head element:
Example 10.7 Rendered:

Selectors

Inheritance

Properties are typically inherited by children elements.
Example 10.8
Example 10.8 Source:
In style element (<style type="text/css">) within head element:
Example 10.8 Rendered:

inheritance tree

inheritance

inheritance p

inheritance em

Note: Firebug is a very useful Firefox extension for analyzing CSS properties and values.

The Cascade

Suggested reading: CSS 2.1: Assigning property values, Cascading, and Inheritance

Stylesheet Origin

Specificity of Selector

Order

The Cascade in Action

cascade

Example 10.9
Example 10.9 Source:
In style element (<style type="text/css">) within head element:
Example 10.9 Rendered:

Sample "UA" default stylesheets for HTML 2.0 and HTML 4.0

Take a look at the sample default stylesheets for HTML 2.0 and HTML 4.0 listed in the Appendices of the CSS1 and CSS2 Recommendations.

HTML 4 Sample default CSS

HTML 2 Sample default CSS

CSS Properties

CSS Level 1 lists 53 properties.

  • Font Properites
    • font-family
    • font-style
    • font-variant
    • font-weight
    • font-size
    • font
  • Color and Background Properties
    • color
    • background-color
    • background-image
    • background-repeat
    • background-attachment
    • background-position
    • background
  • Text Properties
    • word-spacing
    • letter-spacing
    • text-decoration
    • vertical-align
    • text-transform
    • text-align
    • text-indent
    • line-height
  • Box Properties
    • margin-top
    • margin-right
    • margin-bottom
    • margin-left
    • margin
    • padding-top
    • padding-right
    • padding-bottom
    • padding-left
    • padding
    • border-top-width
    • border-right-width
    • border-bottom-width
    • border-left-width
    • border-width
    • border-color
    • border-style
    • border-top
    • border-right
    • border-bottom
    • border-left
    • border
    • width
    • height
    • float
    • clear
    • display
  • Classification Properties
    • white-space
    • list-style-type
    • list-style-image
    • list-style-position
    • list-style

CSS Level 2.1 lists 98 properties .

  • azimuth
  • background-attachment
  • background-color
  • background-image
  • background-position
  • background-repeat
  • background
  • border-collapse
  • border-color
  • border-spacing
  • border-style
  • border-top
  • border-top-color
  • border-top-style
  • border-top-width
  • border-width
  • border
  • bottom
  • caption-side
  • clear
  • clip
  • color
  • content
  • counter-increment
  • counter-reset
  • cue-after
  • cue-before
  • cue
  • cursor
  • direction
  • display
  • elevation
  • empty-cells
  • float
  • font-family
  • font-size
  • font-style
  • font-variant
  • font-weight
  • font
  • height
  • left
  • letter-spacing
  • line-height
  • list-style-image
  • list-style-position
  • list-style-type
  • list-style
  • margin-right
  • margin-top
  • margin
  • max-height
  • max-width
  • min-height
  • min-width
  • orphans
  • outline-color
  • outline-style
  • outline-width
  • outline
  • overflow
  • padding-top
  • padding
  • page-break-after
  • page-break-before
  • page-break-inside
  • pause-after
  • pause-before
  • pause
  • pitch-range
  • pitch
  • play-during
  • position
  • quotes
  • richness
  • right
  • speak-header
  • speak-numeral
  • speak-punctuation
  • speak
  • speech-rate
  • stress
  • table-layout
  • text-align
  • text-decoration
  • text-indent
  • text-transform
  • top
  • unicode-bidi
  • vertical-align
  • visibility
  • voice-family
  • volume
  • white-space
  • widows
  • width
  • word-spacing
  • z-index

font properties


font-family

Example 10.10
Example 10.10 Source:
Example 10.10 Rendered:
Garamond, Times, or serif (generic family)
Arial, Helvetica or sans-serif (generic family)
Courier or monospace (generic family)
Fantasy (generic family)
Cursive (generic family)

font-style

Example 10.11
Example 10.11 Source:
Example 10.11 Rendered:
Normal font-style
Italic font-style
Oblique font-style

font-variant

Example 10.12
Example 10.12 Source:
Example 10.12 Rendered:
This should be rendered in small-caps.
Here we revert to "normal".

font-weight

values: normal | bold | bolder | lighter | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900
Example 10.13
Example 10.13 Source:
Example 10.13 Rendered:
font-weight can be used to make things bold.

font-size

You can specify font-size by:

Praesent tincidunt, elit porta elementum pharetra, magna odio consequat nisi, eu iaculis mauris massa eu eros.

By Name:

Example 10.14
Example 10.14 Source:
Example 10.14 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.


Relative Names:

Example 10.15
Example 10.15 Source:
Example 10.15 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.


Relative Units:

Example 10.16
Example 10.16 Source:
Example 10.16 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.


Absolute Units:

Example 10.17
Example 10.17 Source:
Example 10.17 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.

Lorem ipsum dolor sit amet.


Relative vs. Absolute

As a general guideline with CSS, relative measurements are better than absolute measurements.

Units for Font Sizes

Relative Sizes and Inheritance

Relative size are relative to parent. Nested div elements with font-size: 85% and margin-left: 50px have properties of:

relative sizes and inheritance

Example 10.18
Example 10.18 Source:
In style element (<style type="text/css">) within head element:
Example 10.18 Rendered:

font "shorthand" property

In CSS, there are various shorthand properties; these allow you to define several properties in a single place.
[font-style | font-variant | font-weight ]? font-size[/line-height]? font-family
Example 10.19
Example 10.19 Source:
In style element (<style type="text/css">) within head element:
Example 10.19 Rendered:

text properties

Example 10.20
Example 10.20 Source:
Example 10.20 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis.


CSS Units

Color Units

Wikipedia Web Colors

Name

As defined in HTML: black, silver, gray, white, maroon, red, purple, fuchsia, green, lime, olive, yellow, navy, blue, teal, aqua

RGB Color Space

256 colors in each channel (Red, Green, Blue). Values can be

The following are all equivalent ways of defining a shade of crimson:

  • #99000
  • #900
  • rgb(153,0,0)
  • rgb(60%,0%,0%)
 
Example 10.21
Example 10.21 Source:
Example 10.21 Rendered:
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Web Palette / Web Safe Colors

Visibone Color Laboratory
visibone colorlab

Percent
Red 0% 20% 40% 60% 80% 100%
Green 0% 20% 40% 60% 80% 100%
Blue 0% 20% 40% 60% 80% 100%
Hexadecimal
Red 00 33 66 99 cc ff
Green 00 33 66 99 cc ff
Blue 00 33 66 99 cc ff
Decimal
Red 0 51 102 153 204 255
Green 0 51 102 153 204 255
Blue 0 51 102 153 204 255

color cube axes

Web Color Cube
web safe color palette

Block Model

Box Model Illustration from CSS:  The Definitive Guide by Eric Meyer

Image from Cascading Style Sheets: The Definitive Guide, 3rd ed by Eric Meyer, published by O'Reilly


Firebug visualization of block model (Layout):
Firebug Block Model

margin, padding, border

TRBL for padding and margin shorthand

Stay out of "TRBL" (top right bottom left) for padding and margin shorthand.

border-style

border is a shorthand notation that allows you to set border-width, border-style, border-color in one statement.

Example 10.22
Example 10.22 Source:
In style element (<style type="text/css">) within head element:
Example 10.22 Rendered:

Margin, Padding, Border Example

declaration of independence

Example 10.23
Example 10.23 Source:
In style element (<style type="text/css">) within head element:
Example 10.23 Rendered:

Backgrounds

Background Image with CSS

A background image with CSS:
background

Example 10.24
Example 10.24 Source:
In style element (<style type="text/css">) within head element:
Example 10.24 Rendered:

Repeat

Repeat on y-axis only:
background

Example 10.25
Example 10.25 Source:
In style element (<style type="text/css">) within head element:
Example 10.25 Rendered:

Center and Fix

Center, Don't Repeat and Fix position:
background

Example 10.26
Example 10.26 Source:
In style element (<style type="text/css">) within head element:
Example 10.26 Rendered:

Backgrounds aren't just for the "body"

Here we use the following images as backgrounds in div elements.

wood.jpg
wood
marble.jpg
marble
water.jpg
water

backgrounds

Example 10.27
Example 10.27 Source:
In style element (<style type="text/css">) within head element:
Example 10.27 Rendered:

Backgrounds aren't just for the "body"

Instead of having an img element within the XHTML, we can include design images in the CSS. This example displays the flag.png with the h1 of the markup.

background image

Example 10.28
Example 10.28 Source:
In style element (<style type="text/css">) within head element:
Example 10.28 Rendered:

Backgrounds: use tiling and image size

The image (5px × 150px; 254 bytes):
blue gradient

"Normal" size:
background image

"Increased +4":
background image


Example 10.29
Example 10.29 Source:
In style element (<style type="text/css">) within head element:
Example 10.29 Rendered:

Minuteman

Image as a fixed background for the "body". Alternate versions of the image for the "header" and "main" content areas of the page.

The three images:

minuteman dark minuteman dark minuteman dark

Rendered in a browser:

Minuteman Screenshot

Example 10.30
Example 10.30 Source:
In style element (<style type="text/css">) within head element:
Example 10.30 Rendered:

Pseudo-Classes and Elements

CSS 2.1 defines these pseudo-classes and pseudo-elements. Examples are p:first-line, ul:first-child, and input:focus

Pseudo-Classes Pseudo-Elements
  • first-child
  • link
  • visited
  • hover
  • active
  • focus
  • lang
  • first-line
  • first-letter
  • before
  • after

Pseudo Elements

first-letter pseudo element.

first letter pseudo element

Example 10.31
Example 10.31 Source:
In style element (<style type="text/css">) within head element:
Example 10.31 Rendered:

first-line pseudo element

first line pseudo element

Example 10.32
Example 10.32 Source:
In style element (<style type="text/css">) within head element:
Example 10.32 Rendered:

Opening paragraph with first-letter pseudo element.

Example 10.33
Example 10.33 Source:
In style element (<style type="text/css">) within head element:
Example 10.33 Rendered:

Pseudo Classes

default  a pseudo class

Example 10.34
Example 10.34 Source:
In style element (<style type="text/css">) within head element:
Example 10.34 Rendered:

Class Names

You can choose class names when authoring your CSS and XHTML. A good rule is to create "logical" class names and not "descriptive" ones. Remember, you've gone to the trouble of separating markup and presentation -- keep this separation when creating class names.

If you can guess how the class is styled based upon the name, this should cause you to consider changing the name.

Good Class/ID Names Poor Class Names
  • globalnav
  • sitenav
  • gallery
  • imgcaption
  • aside
  • warning
  • thinborder
  • rightcolumn
  • rightnav
  • redbold
  • center

Choosing class and id names appropriately will help with:


See: Choosing Class and ID Names

Float

Float takes the block out of the flow of the containing block and moves it (right or left) within the containing block.

float: none
float none
float: right
float right
Example 10.35
Example 10.35 Source:
In style element (<style type="text/css">) within head element:
Example 10.35 Rendered:

Float and Clear

The "clear" property defines the sides of a block where floated blocks cannot occur.

float: right; clear: none
float
float: right; clear: right
clear
Example 10.36
Example 10.36 Source:
In style element (<style type="text/css">) within head element:
Example 10.36 Rendered:

Float and Clear

The "clear" property defines the sides of a block where floated blocks cannot occur.

clear: none
float
clear: right
clear
Example 10.37
Example 10.37 Source:
In style element (<style type="text/css">) within head element:
Example 10.37 Rendered:

Float

Float: right

css float

Example 10.38
Example 10.38 Source:
In style element (<style type="text/css">) within head element:
Example 10.38 Rendered:

Float: left

css float

Example 10.39
Example 10.39 Source:
In style element (<style type="text/css">) within head element:
Example 10.39 Rendered:

Float Opposite

Use float: left and float: right for content to be on opposite sides.

float opposite

Example 10.40
Example 10.40 Source:
In style element (<style type="text/css">) within head element:
Example 10.40 Rendered:

The background color of the parent div did not display. The content of this div has been "floated," therefore it has been removed from the calculation of its box model. The parent div size is null. Solution: float the parent div. Recall that a float is always with respect to the containing box.

float opposite

Example 10.41
Example 10.41 Source:
In style element (<style type="text/css">) within head element:
Example 10.41 Rendered:

Float and Margins

We want to float one element left, the other right. We want content to be between the floated elements.

float margin

Example 10.42
Example 10.42 Source:
In style element (<style type="text/css">) within head element:
Example 10.42 Rendered:

Make the middle content not wrap around the floated contents (set margin for the middle content).

float margin

Example 10.43
Example 10.43 Source:
In style element (<style type="text/css">) within head element:
Example 10.43 Rendered:

Float and a Flexible "Grid"

Here each "program" is wrapped in a div, which contains a thumbnail and a caption. The div elements are sized (height and width set) and floated. The result is a table-like layout that is flexible for varying widths of the browser window.

Harvard at Home Programs

float float

Congress

float float

Float and a Flexible "Grid"

It is important to set height and width !

Without setting height and width:
float

Setting height and width:
float


Example 10.44
Example 10.44 Source:
In style element (<style type="text/css">) within head element:
Example 10.44 Rendered:

Display: none

Can use CSS to turn off display. This will be especially useful to manipulate CSS properties with JavaScript.

Full list for the American League:
display

With ALE and ALW display set to "none":
display


Example 10.45
Example 10.45 Source:
In style element (<style type="text/css">) within head element:
Example 10.45 Rendered:

Lists

Ordered List

An ordered list:

Example 10.46
Example 10.46 Source:
In style element (<style type="text/css">) within head element:
Example 10.46 Rendered:

Lists with Image

And now, let's add a basketball icon as a list bullet (image is at images/basketball.gif, basketball

list

An unordered list controlled by CSS:

Example 10.47
Example 10.47 Source:
In style element (<style type="text/css">) within head element:
Example 10.47 Rendered:

Doing this without CSS

How to accomplish this without CSS.

Example 10.48
Example 10.48 Source:
In style element (<style type="text/css">) within head element:
Example 10.48 Rendered:

For the complete list: NCAA Men's Basketball Statistics

Lists without list-style

Example 10.49
Example 10.49 Source:
Example 10.49 Rendered:
  • Big XII
  • Ivy League
  • Pac 10

Example 10.50
Example 10.50 Source:
In style element (<style type="text/css">) within head element:
Example 10.50 Rendered:

Display List Items "Inline"

list
Unordered List
list
Inline List Items
list
Inline List Items
Example 10.51
Example 10.51 Source:
In style element (<style type="text/css">) within head element:
Example 10.51 Rendered:

Example 10.52
Example 10.52 Source:
In example52.css
In head element:
Example 10.52 Rendered:

Inspired by: CSS Cookbook by Christopher Schmitt

Lists as Navigation

Default styling:
navigation lists

As navigation:
navigation lists


Example 10.53
Example 10.53 Source:
In example53.css
In head element:
Example 10.53 Rendered:

Inspired by: CSS Cookbook by Christopher Schmitt

Lists as Navigation

Default styling of nested list:
lists as navigation

Nested list styled as clamshell navigation:
lists as navigation


Example 10.54
Example 10.54 Source:
In example54.css
In head element:
Example 10.54 Rendered:

Inspired by: CSS Cookbook by Christopher Schmitt

Breadcrumb Navigation

"Just wait, Gretel, until the moon rises, and then we shall see the crumbs of bread which I have strewn about, they will show us our way home again.", Hansel in Hansel and Gretel

Markup "breadcrumb" navigation using nested lists so that markup reflects the parent/child or hierarchy relationship.

screenshot

screenshot

Strategy is similar to that of creating tabs. Use "display: inline" to make list items inline; use background image for "li" to show arrow.

Example 10.55
Example 10.55 Source:
In example55.css
In head element:
Example 10.55 Rendered:

media selector

CSS2 defines a media selector for different media types.

Incorporate as part of the "link" element:

Or, use "@media" selector in style rule:

all
Suitable for all devices.
braille
Intended for braille tactile feedback devices.
embossed
Intended for paged braille printers.
handheld
Intended for handheld devices (typically small screen, limited bandwidth).
print
Intended for paged material and for documents viewed on screen in print preview mode.
projection
Intended for projected presentations, for example projectors.
screen
Intended primarily for color computer screens.
speech
Intended for speech synthesizers. Note: CSS2 had a similar media type called 'aural' for this purpose.
tty
Intended for media using a fixed-pitch character grid (such as teletypes, terminals, or portable devices with limited display capabilities).
tv
Intended for television-type devices (low resolution, color, limited-scrollability screens, sound available).

Harvard College Funding Database

Screen display (screen.css):

funding database

Print display (print.css):

funding print

Print CSS

CSS Print Profile deals with paged media.

The book Cascading Style Sheets : Designing for the Web (3rd Edition) by Hakon Wium Lie and Bert Bos was printed using CSS.

See A List Apart: Articles: Printing a Book with CSS: Boom!

Yahoo! UI (YUI) Library

YUI Grid Example

Grid: 4 areas:

Note the use of "reset-fonts-grids.css", which gives you a blank slate for styles (overrides UA stylsheet for font sizes, margins, padding, etc.)

yui grid e

yui grid e

Harvard Homepage Markup/Style Makeover

Harvard Home Page
HU Home

Tables exposed:
HU Home


Markup and Page Stats

Markup Type HTML 4.01 Transitional
53 validation errors
Markup Size 26.1 k
Markup (after 'tidy') 380 lines
Document dependencies
  • 1 HTML
  • 48 images
Total page weight 226 k
Elements

314 total

a 60
b 15
body 1
br 9
center 2
div 1
font 10
head 1
hr 6
html 1
img 61
meta 3
p 9
script 1
table 16
td 85
title 1
tr 32
Attributes 588

Homepage Sections

Homepage

hu

As Markup

hu

Stage 1: YUI Grid

Stage 2: Styles for Main sections

Stage 3: Styles for Navigation Lists

Stage 4: Styles for Content

Results of Markup/Style Makeover

Harvard Homepage with Markup/Style Makeover

  Current After Markup/Style Makeover
Screenshot hu no css hu css beginning
Markup Type HTML 4.01 Transitional
53 validation errors
XHTML 1.0 Strict
Markup Size 26.1 k 6.9 k
Markup (after 'tidy') 380 lines 134 lines
Style (CSS) n/a
  • Custom CSS
    7.6k
    241 lines
  • YUI CSS
    3.3k
Document dependencies
  • 1 HTML
  • 48 images
  • 1 XHTML
  • 11 images
  • 2 CSS
Total page weight 226 k 83 k
Elements
elements:  314
        a       60
        b       15
        body    1
        br      9
        center  2
        div     1
        font    10
        head    1
        hr      6
        html    1
        img     61
        meta    3
        p       9
        script  1
        table   16
        td      85
        title   1
        tr      32
attributes: 588
elements:  148
        a       50
        body    1
        br      1
        div     21
        h1      1
        h2      2
        h3      1
        head    1
        html    1
        img     4
        li      43
        link    1
        meta    1
        p       9
        title   1
        ul      10
attributes:  106

With CSS disabled:

hu nocss


Assets

Results on Many Browsers

Services

Do it yourself


Results for Harvard Makeover

browsershots

The above uses "Thickbox", which is a UI widget written in JavaScript that is built on jQuery and uses AJAX. Uses: jquery.js, thickbox.js, thickbox.css.

JavaScript: Manipulate the Page

JS can be used to manipulate CSS properties.

Note the use of event attributes (onclick, onmouseover, onmouseout) as well as a javascript: protocol in an href value.

js manipulate

Example 10.56
Example 10.56 Source:
In example56.js
In head element:
Example 10.56 Demonstrated

Exposing Additional Form Elements

Additional relevant form elements are exposed based upon user input. Here, this is achieved by having the entire form in the markup, with a certain section hidden via CSS (display: none;). If "Yes" is chosen, the display property is changed to "block" through JS.

ice cream options ice cream options

Example 10.57
Example 10.57 Source:
In script element (within head element):
Example 10.57 Demonstrated

Tabs with jQuery

Tab Plugin for jQuery allows the easy creation of tabs from simple markup.


Without JS and CSS:
tabs

With JS and CSS:
tabs


Markup:

JS:

A Calendar Widget

jQuery has plugins that will provide an unobtrusive "calendar" widget for picking dates.
datepicker

Example: date_picker.html

Markup:

JS:

Slideshow: Thickbox

ThickBox is a webpage UI dialog widget written in JavaScript on top of the jQuery library. Its function is to show a single image, multiple images, inline content, iframed content, or content served through AJAX in a hybrid modal.

A gallery with no JavaScript -- thumbnail images are linked to the full-sized image with a simple
<a href="full/a.png"><img src="thumb/a.png"/></a>:

unobtrusive

Thickbox

Example: Gallery with Thickbox

Add Thickbox and give the "a" elemnts a class="gallery":

Gallery
thickbox

Viewing an individual image:
thickbox


Example Galleries with Lightbox, Lightbox 2, and Thickbox

View example galleries implemented in Lightbox and Lightbox 2 (using Prototype/Scriptaculous) as well as Thickbox (using jQuery).