cascading style sheets (css) - cscie12.dce.harvard.edu · cascading style sheets (css) 5 of 83...

42
Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html 1 of 83 2/20/2007 7:58 PM Table of Contents | All Slides | Link List | Examples | CSCI E-12 Cascading Style Sheets (CSS) February 20, 2007 Harvard University Division of Continuing Education Extension School Course Web Site: http://cscie12.dce.harvard.edu/ Copyright 1998-2007 David P. Heitmeyer Instructor email: [email protected] Course staff email: [email protected] Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html 2 of 83 2/20/2007 7:58 PM Quick Reference Cards From refcards.com: XHTML XHTML 1.0 Strict XHTML 1.0 Transitional CSS CSS Level 1 CSS Level 2

Upload: vuxuyen

Post on 25-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

1 of 83 2/20/2007 7:58 PM

Table of Contents | All Slides | Link List | Examples | CSCI E-12

Cascading Style Sheets (CSS)February 20, 2007

Harvard University Division of Continuing Education

Extension School

Course Web Site: http://cscie12.dce.harvard.edu/

Copyright 1998-2007 David P. Heitmeyer

Instructor email: [email protected] Course staff email: [email protected]

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

2 of 83 2/20/2007 7:58 PM

Quick Reference Cards

From refcards.com:

XHTMLXHTML 1.0 StrictXHTML 1.0 Transitional

CSSCSS Level 1CSS Level 2

Page 2: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

3 of 83 2/20/2007 7:58 PM

Server Side Includes (SSI) Recap

A simple way of dynamically including content in XHTML/HTML documents.

include other documents (use the virtual parameter)echo document and/or server information

SSI statements look like XHTML comments, except with a "#"

Apache processes files for SSI statements based upon file extension. Default behavior on minervais to parse files ending in .shtml

<!--#include virtual="footer.shtml" --> 1.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

4 of 83 2/20/2007 7:58 PM

Typical SSI Use Cases

Common pages header1.Frequently changing content2.Common navigation3.Common footer with document information4.

Footer:

Page for PRISE website with SSI:

Page 3: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

5 of 83 2/20/2007 7:58 PM

Footer

Contents of footer.shtml contains SSI commands:

Example 4.1

Example 4.1 Source:

Example 4.1 Demonstrated

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 2.<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 3.<head> 4. <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 5. <title>PRISE: About PRISE</title> 6. <link rel="stylesheet" href="prise.css" type="text/css" /> 7.</head> 8.<body id="about"> 9. <div id="container"> 10. <div id="logobanner"> 11. <!--#include virtual="banner.html" --> 12. </div> 13. <div id="content"> 14. <!--#include virtual="leftmenu.html" --> 15. <div id="main"> 16. <div id="notices"> 17. <!--#include virtual="notices.html" --> 18. </div> 19. <h2>About PRISE</h2><!--begin content--> 20. <p>The May 2005 Report ...</p> 21. <p>For more information ...</p><!--end content--> 22. <div class="footer"> 23. <!--#include virtual="footer.shtml" --> 24. </div> 25. </div> 26. </div> 27. </div> 28.</body> 29.</html> 30.

<div class="footer" > 1.<!--#config timefmt="%B %e, %Y"--> 2.Last updated: <!--#echo var="LAST_MODIFIED" --> 3.| Images: <a href="http://www.freeimages.co.uk" >www.freeimages.co.uk</a> 4.<br/> 5.Location: http://<!--#echo var="SERVER_NAME"--><!--#echo var="DOCUMENT_URI"--> 6.</div> 7.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

6 of 83 2/20/2007 7:58 PM

Iroquois Constitution

Iroquois Constitution

Iroquois Constitution Zip File containing XHTML, CSS, and "Include" files.

Page 4: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

7 of 83 2/20/2007 7:58 PM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 1. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 2.<html xmlns="http://www.w3.org/1999/xhtml"> 3.<head> 4.<title>The Great Binding Law, Gayanashagowa (Constitution of the Iroquois Nations)</titl5.<link rel="stylesheet" type="text/css" href="style.css" /> 6.</head> 7.<body> 8.<div id="header"> 9. <!--#include file="header.inc"--> 10.</div> 11.<div id="navigation"> 12. <!--#include file="nav.inc"--> 13.</div> 14.<div id="main"> 15. <!--#include file="1.inc"--> 16.</div> 17.</body> 18.</html> 19.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

8 of 83 2/20/2007 7:58 PM

Pros and Cons

Pros

Easy to use Widespread server support for SSIServer-side; not browser-dependentLess resource intensive than CGISimple, yet powerful.

Cons

Used appropriately, virtually none.Limiting (compared to other, more complex and full-featured mechanisms to produce dynamic content)

Apache SSI Documentation

Apache Tutorial: Introduction to Server Side IncludesApache module mod_include

Page 5: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

9 of 83 2/20/2007 7:58 PM

Cascading Style Sheets (CSS)

XHTML for Structure; CSS for Presentation

XHTML for MarkupCSS for style and presentation

Markup and Presentation Intertwined

Symptoms

proprietary HTML extensionsmaking text into imagesproliferate use of "spacer" 1px by 1px GIF imagesdeprecated HTML elements and/or attributesusing tables for layout

Consequences

Changing design can be a major hassleHTML/XHTML very complex; can easily lead to errorsUnnecessary 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 sizeImproves accessibility.Simple yet powerful.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

10 of 83 2/20/2007 7:58 PM

The Power of Stylesheets

United States Constitution (no external stylesheet)

W3C Core Styles applied to the US Constitution.

Chocolate StylesheetMidnight StylesheetModernist StylesheetOldstyle StylesheetSteely StylesheetSwiss StylesheetTraditional StylesheetUltramarine Stylesheet

Page 6: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

11 of 83 2/20/2007 7:58 PM

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

12 of 83 2/20/2007 7:58 PM

Harvard College Admissions

Harvard College Admissions With CSS disabled:

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.

Page 7: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

13 of 83 2/20/2007 7:58 PM

Lists

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

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

14 of 83 2/20/2007 7:58 PM

Department of African and African American Studies

Department of African and African American Studies

With CSS disabled:

Page 8: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

15 of 83 2/20/2007 7:58 PM

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.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

16 of 83 2/20/2007 7:58 PM

Page 9: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

17 of 83 2/20/2007 7:58 PM

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

18 of 83 2/20/2007 7:58 PM

CSS Recommendations from the W3C

CSS 1 (Dec 1996; Revised Jan 1999)CSS 2 (May 1998)CSS 2.1 (Februray 2004)CSS Current Work http://www.w3.org/Style/CSS/current-work (CSS Mobile Profile, Print Profile, TV Profile, CSS3)

CSS Validator

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

Page 10: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

19 of 83 2/20/2007 7:58 PM

style attribute

Example 4.2

Example 4.2 Source:

Example 4.2 Rendered:

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

<p style="color: black; background-color: teal; padding: 1em; font-family: helvetica, sa1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec 2. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli3. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 4. ante sit amet urna. Maecenas condimentum hendrerit turpis. </p> 5.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

20 of 83 2/20/2007 7:58 PM

style element

Example 4.3

Example 4.3 Source:

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

Example 4.3 Rendered:

With StylesWithout Styles

So the full page looks like:

<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci ne1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.

p { 1. color: black; 2. background-color: teal; 3. padding: 1em; 4. font-family: helvetica, sans-serif; 5. text-align: justify; 6. margin: 2em; 7. } 8.

view plain print ?

<html xmlns="http://www.w3.org/1999/xhtml"> 1. <head> 2. <title>CSCIE-12, Example: 4.3</title> 3. <style type="text/css"> 4. p { 5. color: black; 6. background-color: teal; 7. padding: 1em; 8. font-family: helvetica, sans-serif; 9. text-align: justify; 10. margin: 2em; 11. } 12. </style> 13. </head> 14. <body> 15. <p> 16. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci 17. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce 18. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla conval19. sit amet urna. Maecenas condimentum hendrerit turpis. 20. </p> 21. </body> 22.</html> 23.

Page 11: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

21 of 83 2/20/2007 7:58 PM

link element

The link element can reference an external stylesheet.

Example 4.4

Example 4.4 Source:

In example4.css

In head element:

Example 4.4 Rendered:

With StylesWithout Styles

The full source:

<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci ne1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.

p { 1. color: black; 2. background-color: teal; 3. padding: 1em; 4. font-family: helvetica, sans-serif; 5. text-align: justify; 6. margin: 2em; 7. } 8.

<link rel="stylesheet" type="text/css" href="example4.css"/> 1.

view plain print ?

<html xmlns="http://www.w3.org/1999/xhtml"> 1. <head> 2. <title>CSCIE-12, Example: 4.4</title> 3. <link href="example4.css" type="text/css" rel="stylesheet"/> 4. </head> 5. <body> 6. <p> 7. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci n8. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce v9. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convall10. sit amet urna. Maecenas condimentum hendrerit turpis. 11. </p> 12. </body> 13./html> 14.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

22 of 83 2/20/2007 7:58 PM

Anatomy of a CSS Rule

Example 4.5

Example 4.5 Source:

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

Example 4.5 Rendered:

With StylesWithout Styles

CSS Rule

Selector and Declarations

<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci ne1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.

p { 1. color: red; 2. background-color: blue; 3. } 4.

Page 12: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

23 of 83 2/20/2007 7:58 PM

Properties and Values

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

24 of 83 2/20/2007 7:58 PM

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.

p {color: black;} 1.p {background-color: teal;} 2.p {padding: 1em;} 3.p {margin: 1em;} 4.p {font-family: helvetica, sans-serif;} 5.p {text-align: justify;} 6.

p { 1. color: black; 2. background-color: teal; 3. padding: 1em; 4. margin: 1em; 5. font-family: helvetica, sans-serif; 6. text-align: justify; 7.} 8.

Page 13: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

25 of 83 2/20/2007 7:58 PM

Combining Selectors

Selectors can be combined into comma-separated groups.

We combine the selectors so that a single declaration applies to multiple selectors.

h1 { color: maroon; } 1.h2 { color: maroon; } 2.h3 { color: maroon; } 3.h4 { color: maroon; } 4.h5 { color: maroon; } 5.h6 { color: maroon; } 6.

h1, h2, h3, h4, h5, h6 { color: maroon; } 1.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

26 of 83 2/20/2007 7:58 PM

class selectors

The class and id attributes of XHTML elements can be used in conjunction with styles.

Class names are referenced in CSS as .classname, and may or may not have an element name preceding the period (.classname or element.classname.

Likewise, id names are referenced in CSS as #idref, and may or may not have an element name preceding the period (#idref or element#idref.

Example 4.6

Example 4.6 Source:

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

Example 4.6 Rendered:

<div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci 1. nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.</div> 2.<div class="withstyle" >Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, 3. commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum 4. hendrerit turpis.</div> 5.<div class="warn" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras 6. sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim 7. velit at orci.</div> 8.<div>Lorem ipsum dolor sit amet, 9. <span class="warn" >consectetuer adipiscing elit</span>. Cras sollicitudin, orci nec 10. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci.</div> 11.<div id="legalese" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras 12. sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim 13. velit at orci.</div> 14.

div 1.{ 2. background-color: white; 3. color: black; 4. font-family: times; 5. margin: 0.5em; 6. padding: 0.5em; 7.} 8.div.withstyle 9.{ 10. background-color: olive; 11. color: navy; 12. font-family: sans-serif; 13. margin: 0.5em; 14. padding: 0.5em; 15.} 16..warn 17.{ 18. background-color: yellow; 19. color: red; 20. font-weight: bold; 21.} 22.#legalese 23.{ 24. color: #cccccc; 25. font-size: 0.6em; 26.} 27. 28.

Page 14: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

27 of 83 2/20/2007 7:58 PM

With StylesWithout Styles

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

28 of 83 2/20/2007 7:58 PM

id selectors

Example 4.7

Example 4.7 Source:

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

Example 4.7 Rendered:

With StylesWithout Styles

<div id="header" > put in header information here </div> 1.<div id="main" > 2. <!-- main content --> 3. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras 4. sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim 5. velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo 6. vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit 7. turpis. </div> 8.<div id="footer" > put in footer information here </div> 9.

#main { 1. background-color: blue; 2. color: white; 3. border-color: green; 4. border-width: thick; 5. border-style: solid; 6.} 7.

Page 15: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

29 of 83 2/20/2007 7:58 PM

Contextual Selectors

Example 4.8

Example 4.8 Source:

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

Example 4.8 Rendered:

With StylesWithout Styles

<div> <em>Emphasized text</em> outside of <strong>li</strong> appear "normal". 1. <ul> 2. <li><em>Emphasized text</em> within <strong>li</strong> have a different 3. style.</li> 4. </ul> </div> 5.

li em { color: red; background-color: navy;} 1.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

30 of 83 2/20/2007 7:58 PM

Properties

CSS Level 1 lists 53 properties that let you style properties of:

fontcolortextboxes (border, padding, margins)classification (inline, block, list)

CSS Level 2.1 lists 105 properties.

Page 16: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

31 of 83 2/20/2007 7:58 PM

Inheritance

Properties are typically inherited by children elements.

Example 4.9

Example 4.9 Source:

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

Example 4.9 Rendered:

With StylesWithout Styles

<div> 1.Lorem ipsum dolor sit amet, <em>consectetuer adipiscing elit</em>. Cras sollicitudin, or2.<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec3.

body { color: navy; } 1.em { color: red; } 2.div { color: green; } 3.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

32 of 83 2/20/2007 7:58 PM

Recommendation and Implementations

CSS Browser Support

Page 17: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

33 of 83 2/20/2007 7:58 PM

Importing Stylesheets

Stylesheets can be imported. This is useful to reduce redundancy.

Example 4.10

Example 4.10 Source:

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

Example 4.10 Rendered:

With StylesWithout Styles

<div> 1.Now we have the best of both worlds. I have the styles from the main course site, and s2.</div> 3.

/* import main style sheet */ 1.@import url(http://cscie12.dce.harvard.edu/skin/basic.css) 2..elementcontent, .element { 3. margin-left: 1em; 4.} 5..bracket { 6. font-weight: bold; 7.} 8..ename { 9. color: blue; 10.} 11..attr { 12. color: red; 13.} 14..attrv { 15. color: green; 16.} 17..pcdata { 18. color: black; 19.} 20..source { 21. background-color: #eeeeee; 22. border-size: 1pt; 23. border-style: solid; 24. padding: 0.5em; 25.} 26.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

34 of 83 2/20/2007 7:58 PM

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.

Sample style sheet for HTML 2.0 [Local copy]Default style sheet for HTML 4.0 [Local copy]

HTML 4 Sample default CSS

Page 18: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

35 of 83 2/20/2007 7:58 PM

html, address, 1.blockquote, 2.body, dd, div, 3.dl, dt, fieldset, form, 4.frame, frameset, 5.h1, h2, h3, h4, 6.h5, h6, noframes, 7.ol, p, ul, center, 8.dir, hr, menu, pre { display: block } 9.li { display: list-item } 10.head { display: none } 11.table { display: table } 12.tr { display: table-row } 13.thead { display: table-header-group } 14.tbody { display: table-row-group } 15.tfoot { display: table-footer-group } 16.col { display: table-column } 17.colgroup { display: table-column-group } 18.td, th { display: table-cell; } 19.caption { display: table-caption } 20.th { font-weight: bolder; text-align: center } 21.caption { text-align: center } 22.body { margin: 8px; line-height: 1.12 } 23.h1 { font-size: 2em; margin: .67em 0 } 24.h2 { font-size: 1.5em; margin: .75em 0 } 25.h3 { font-size: 1.17em; margin: .83em 0 } 26.h4, p, 27.blockquote, ul, 28.fieldset, form, 29.ol, dl, dir, 30.menu { margin: 1.12em 0 } 31.h5 { font-size: .83em; margin: 1.5em 0 } 32.h6 { font-size: .75em; margin: 1.67em 0 } 33.h1, h2, h3, h4, 34.h5, h6, b, 35.strong { font-weight: bolder } 36.blockquote { margin-left: 40px; margin-right: 40px } 37.i, cite, em, 38.var, address { font-style: italic } 39.pre, tt, code, 40.kbd, samp { font-family: monospace } 41.pre { white-space: pre } 42.button, textarea, 43.input, object, 44.select { display:inline-block; } 45.big { font-size: 1.17em } 46.small, sub, sup { font-size: .83em } 47.sub { vertical-align: sub } 48.sup { vertical-align: super } 49.table { border-spacing: 2px; } 50.thead, tbody, 51.tfoot { vertical-align: middle } 52.td, th { vertical-align: inherit } 53.s, strike, del { text-decoration: line-through } 54.hr { border: 1px inset } 55.ol, ul, dir, 56.menu, dd { margin-left: 40px } 57.ol { list-style-type: decimal } 58.ol ul, ul ol, 59.ul ul, ol ol { margin-top: 0; margin-bottom: 0 } 60.u, ins { text-decoration: underline } 61.br:before { content: "\A" } 62.:before, :after { white-space: pre-line } 63.center { text-align: center } 64.abbr, acronym { font-variant: small-caps; letter-spacing: 0.1em } 65.:link, :visited { text-decoration: underline } 66.:focus { outline: thin dotted invert } 67. 68./* Begin bidirectionality settings (do not change) */ 69.BDO[DIR="ltr"] { direction: ltr; unicode-bidi: bidi-override } 70.BDO[DIR="rtl"] { direction: rtl; unicode-bidi: bidi-override } 71. 72.*[DIR="ltr"] { direction: ltr; unicode-bidi: embed } 73.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

36 of 83 2/20/2007 7:58 PM

HTML 2 Sample default CSS

BODY { 1. margin: 1em; 2. font-family: serif; 3. line-height: 1.1; 4. background: white; 5. color: black; 6.} 7. 8.H1, H2, H3, H4, H5, H6, P, UL, OL, DIR, MENU, DIV, 9.DT, DD, ADDRESS, BLOCKQUOTE, PRE, BR, HR, FORM, DL { 10. display: block } 11. 12.B, STRONG, I, EM, CITE, VAR, TT, CODE, KBD, SAMP, 13.IMG, SPAN { display: inline } 14. 15.LI { display: list-item } 16. 17.H1, H2, H3, H4 { margin-top: 1em; margin-bottom: 1em } 18.H5, H6 { margin-top: 1em } 19.H1 { text-align: center } 20.H1, H2, H4, H6 { font-weight: bold } 21.H3, H5 { font-style: italic } 22. 23.H1 { font-size: xx-large } 24.H2 { font-size: x-large } 25.H3 { font-size: large } 26. 27.B, STRONG { font-weight: bolder } /* relative to the parent */ 28.I, CITE, EM, VAR, ADDRESS, BLOCKQUOTE { font-style: italic } 29.PRE, TT, CODE, KBD, SAMP { font-family: monospace } 30. 31.PRE { white-space: pre } 32. 33.ADDRESS { margin-left: 3em } 34.BLOCKQUOTE { margin-left: 3em; margin-right: 3em } 35. 36.UL, DIR { list-style: disc } 37.OL { list-style: decimal } 38.MENU { margin: 0 } /* tight formatting */ 39.LI { margin-left: 3em } 40. 41.DT { margin-bottom: 0 } 42.DD { margin-top: 0; margin-left: 3em } 43. 44.HR { border-top: solid } /* 'border-bottom' could also have been used */ 45. 46.A:link { color: blue } /* unvisited link */ 47.A:visited { color: red } /* visited links */ 48.A:active { color: lime } /* active links */ 49. 50./* setting the anchor border around IMG elements 51. requires contextual selectors */ 52. 53.A:link IMG { border: 2px solid blue } 54.A:visited IMG { border: 2px solid red } 55.A:active IMG { border: 2px solid lime } 56. 57.

Page 19: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

37 of 83 2/20/2007 7:58 PM

The Cascade

Stylesheet Origin

author's stylesheetreader's stylesheetUA's stylesheet

Specificity of Selector

Concatenate the following:

count "id" attributes count "class" attributescount element names

Order

last occurence has higher specificity

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

38 of 83 2/20/2007 7:58 PM

font properties

font-familyfont-stylefont-variantfont-weightfont-sizefont

Page 20: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

39 of 83 2/20/2007 7:58 PM

font-family

Example 4.11

Example 4.11 Source:

Example 4.11 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)

<div style="font-family: garamond, times, serif;" >Garamond, Times, or serif (generic fa1.<div style="font-family: arial, helvetica, sans-serif;" >Arial, Helvetica or sans-serif 2.<div style="font-family: courier, monospace;" >Courier or monospace (generic family)</di3.<div style="font-family: fantasy;" >Fantasy (generic family)</div> 4.<div style="font-family: cursive;" >Cursive (generic family)</div> 5.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

40 of 83 2/20/2007 7:58 PM

font-style

Example 4.12

Example 4.12 Source:

Example 4.12 Rendered:

Normal font-styleItalic font-styleOblique font-style

<div style="font-style: normal;" >Normal font-style</div> 1.<div style="font-style: italic;" >Italic font-style</div> 2.<div style="font-style: oblique;" >Oblique font-style</div> 3.

Page 21: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

41 of 83 2/20/2007 7:58 PM

font-variant and font-weight

font-variant

Example 4.13

Example 4.13 Source:

Example 4.13 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 4.14

Example 4.14 Source:

Example 4.14 Rendered:

font-weight can be used to make things bold.

<div style="font-variant: small-caps;" >This should be rendered in small-caps. 1.<div style="font-variant: normal;" >Here we revert to "normal". 2.</div> 3.</div> 4.

<div> 1.font-weight can be used to make things <span style="font-weight: bold" >bold</span>. 2.</div> 3.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

42 of 83 2/20/2007 7:58 PM

font-size

Example 4.15

Example 4.15 Source:

Example 4.15 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec facili sis vehicula, neque urna porta risus, ut sagittis enim velitat orci.

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

<div style="font-size: 8pt;" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 1. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis en2. velit at orci.</div> 3.<div style="font-size: 120%;" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit.4. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis en5. velit at orci.</div> 6.<div style="font-size: 1.5em;" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit7. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis en8. velit at orci.</div> 9.

Page 22: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

43 of 83 2/20/2007 7:58 PM

Relative vs. Absolute

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

RelativeRelative Sizes (to UA settings):xx-small | x-small | small | medium | large | x-large |xx-largeRelative Sizes (to context): larger | smallerPercentage (%)"em" unitsLength

Absolute"pt" sizes (8pt, 10pt, 12pt)

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

44 of 83 2/20/2007 7:58 PM

font

In CSS, there are various shorthandproperties; these allow you to define several properties in a single place.[font-style | font-variant | font-weight ]? font-size[/line-height]? font-family

Example 4.16

Example 4.16 Source:

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

Example 4.16 Rendered:

With StylesWithout Styles

<div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci n1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </div> 4.

body { 1. font: normal normal normal 12pt/200% arial, helvetica, sans-serif; 2.} 3.

Page 23: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

45 of 83 2/20/2007 7:58 PM

text properties

word-spacingletter-spacingtext-decorationvertical-aligntext-transformtext-aligntext-indentline-height

Example 4.17

Example 4.17 Source:

Example 4.17 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Crassollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce velit. Integersapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nullaconvallis ante sit amet urna. Maecenas condimentum hendreritturpis.

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. Integersapien enim, rhoncus vitae, cursus

non, commodo vitae, felis. Nullaconvallis ante sit amet urna.

Maecenas condimentum hendrerit

<div style="margin-left: 30%; margin-right: 30%;" > 1. <p style="text-align: left" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit.2. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis 3. velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo 4. vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit 5. turpis. </p> 6. <p style="text-align: right" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit7. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis 8. velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo 9. vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit 10. turpis. </p> 11. <p style="text-align: justify" >Lorem ipsum dolor sit amet, consectetuer adipiscing 12. elit. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut 13. sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus14. commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas condimentum 15. hendrerit turpis. </p> 16.</div> 17.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

46 of 83 2/20/2007 7:58 PM

turpis.

Lorem ipsum dolor sit amet,consectetuer adipiscing elit. Crassollicitudin, orci nec facilisis vehicula,neque urna porta risus, ut sagittisenim velit at orci. Fusce velit. Integersapien enim, rhoncus vitae, cursusnon, commodo vitae, felis. Nullaconvallis ante sit amet urna.Maecenas condimentum hendreritturpis.

Page 24: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

47 of 83 2/20/2007 7:58 PM

Web Developer Extension for Firefox

Mozilla FirefoxWeb Developer Extension for Firefox

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

48 of 83 2/20/2007 7:58 PM

Firebug for Firefox

Firebug: Web Development Evolved

Page 25: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

49 of 83 2/20/2007 7:58 PM

Firebug

Simple HTML Example with CSS

Edit CSS in Firebug: Disable CSS Rules in Firebug:

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

50 of 83 2/20/2007 7:58 PM

Show computed styles in Firebug:

Page 26: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

51 of 83 2/20/2007 7:58 PM

Pseudo Elements

Example 4.18

Example 4.18 Source:

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

Example 4.18 Rendered:

With StylesWithout Styles

Example 4.19

Example 4.19 Source:

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

Example 4.19 Rendered:

With StylesWithout Styles

Example 4.20

Example 4.20 Source:

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

<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci ne1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.

p:first-letter { font-size: 400%; } 1.

<p> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci ne1. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli2. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 3. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.

p:first-line { font-variant: small-caps; } 1.

<p class="opener" > Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras 1. sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis enim ve2. at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, f3. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis. </p> 4.<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec5. facilisis vehicula, neque urna porta risus, ut sagittis enim velit at orci. Fusce veli6. Integer sapien enim, rhoncus vitae, cursus non, commodo vitae, felis. Nulla convallis 7. sit amet urna. Maecenas condimentum hendrerit turpis. </p> 8.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

52 of 83 2/20/2007 7:58 PM

Example 4.20 Rendered:

With StylesWithout Styles

p.opener:first-letter { font-size: 400%; float: left; } 1.

Page 27: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

53 of 83 2/20/2007 7:58 PM

Pseudo Classes

a:linka:visiteda:hovera:active

Example 4.21

Example 4.21 Source:

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

Example 4.21 Rendered:

With StylesWithout Styles

<div><a href="http://www.npr.org/" >National Public Radio</a></div> 1.<div><a href="http://www.cnn.com/" >Cable Network News</a></div> 2.<div><a href="http://www.foxnews.com/" >Fox News</a></div> 3.<div><a href="http://www.abcnews.com/" >ABC News</a></div> 4.<div><a href="http://www.economist.com/" >The Economist</a></div> 5.

div {margin: 0.5em; } 1.a {margin: 0.25em; padding: 0.25em;} 2.a:link { 3. text-decoration: none; 4. color: blue; 5. background-color: white; 6. } 7.a:visited { 8. text-decoration: none; 9. color: green; 10. background-color: silver; 11.} 12.a:hover { 13. text-decoration: none; 14. color: white; 15. background-color: blue; 16.} 17.a:active { 18. text-decoration: underline; 19. color: red; 20. background-color: yellow; 21.} 22.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

54 of 83 2/20/2007 7:58 PM

CSS Units

Length em, pt, ex, mm, cm, in, pxPercentageURL url(url goes here)Color name, rgb value

Page 28: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

55 of 83 2/20/2007 7:58 PM

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

decimal numbers (0 to 255)hexadecimal numbers (00 to ff)percentages (0 to 100%)

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

#99000#900rgb(153,0,0)rgb(60%,0%,0%)

Example 4.22

Example 4.22 Source:

Example 4.22 Rendered:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

<div style="background-color: #990000; color: white; padding: 0.5em; margin: 0.25em; " >1. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 2.</div> 3.<div style="background-color: #900; color: white; padding: 0.5em; ; margin: 0.25em;" > 4. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 5.</div> 6.<div style="background-color: rgb(153,0,0); color: white; padding: 0.5em; ; margin: 0.257. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 8.</div> 9.<div style="background-color: rgb(60%,0%,0%); color: white; padding: 0.5em; ; margin: 0.10. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. 11.</div> 12.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

56 of 83 2/20/2007 7:58 PM

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Lorem ipsum dolor sit amet, consectetuer adipiscing elit.

Page 29: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

57 of 83 2/20/2007 7:58 PM

Web Palette / Web Safe Colors

Web Color Cube

Hexadecimal R: 00, 33, 66, 99, cc, ffG: 00, 33, 66, 99, cc, ffB: 00, 33, 66, 99, cc, ff

Decimal R: 0, 51, 102, 153, 204, 255G: 0, 51, 102, 153, 204, 255B: 0, 51, 102, 153, 204, 255

Percentage R: 0%, 20%, 40%, 60%, 80%, 100%G: 0%, 20%, 40%, 60%, 80%, 100%B: 0%, 20%, 40%, 60%, 80%, 100%

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

58 of 83 2/20/2007 7:58 PM

Block Model

marginborderpaddingcontent

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

Page 30: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

59 of 83 2/20/2007 7:58 PM

margin, padding, border

marginmargin-topmargin-rightmargin-bottommargin-leftmargin (shorthand)

paddingpadding-toppadding-rightpadding-bottompadding-leftpadding (shorthand)

borderborder-top-widthborder-right-widthborder-bottom-widthborder-left-widthborder-widthborder-colorborder-styleborder-topborder-rightborder-bottomborder-leftborder

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

60 of 83 2/20/2007 7:58 PM

border-style

Example 4.23

Example 4.23 Source:

Page 31: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

61 of 83 2/20/2007 7:58 PM

<div> 1. <h4>Dotted</h4> 2. <p class="border1" >Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras 3. feugiat mauris facilisis libero. Etiam nisl. Cras est dolor, viverra ac, ultrices 4. volutpat, vestibulum et, odio. Nulla eget libero. Praesent eget tellus vel nibh nonu5. egestas. Donec a ligula. Aenean magna odio, nonummy eu, faucibus ac, aliquam non, es6. Integer gravida pede id justo. Maecenas elit nisi, blandit id, ornare eu, condimentu7. diam. Donec at felis mattis magna euismod suscipit. Curabitur pulvinar. Duis in dui.8. Integer mattis risus ac erat. Nullam sit amet dolor. Suspendisse non lacus. </p> 9. <h4>Dashed</h4> 10. <p class="border2" > Etiam eu arcu quis lectus semper sodales. Donec vitae risus. Inte11. sollicitudin imperdiet dolor. Donec vehicula. Aliquam ut sapien sed eros imperdiet 12. pharetra. Donec accumsan scelerisque leo. Sed eros nunc, pellentesque et, mollis non13. faucibus venenatis, tortor. Curabitur auctor rutrum turpis. Vestibulum id ipsum eu l14. venenatis cursus. Nulla at risus. Lorem ipsum dolor sit amet, consectetuer adipiscin15. elit. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac 16. turpis egestas. Curabitur eget odio. Phasellus consectetuer massa. Aenean ornare 17. congue purus. Aliquam orci ligula, tempus eget, blandit et, mattis a, lorem. Sed non18. magna pharetra sodales. </p> 19. <h4>Outset</h4> 20. <p class="border3" > Pellentesque a velit. Sed pharetra vestibulum mauris. Ut vel arcu21. Cras dolor ligula, eleifend et, ultrices nec, viverra in, ipsum. In convallis pharet22. lacus. Etiam tellus. Aliquam quam. Vivamus mattis purus nec quam. Suspendisse hendre23. dui ac massa. Morbi consectetuer malesuada lacus. Nulla eu pede. Quisque mattis. Eti24. vel nunc. </p> 25. <h4>Solid</h4> 26. <p class="border4" > Etiam rhoncus. Praesent id neque et odio dictum varius. Integer 27. imperdiet blandit orci. Donec nec nunc posuere augue egestas accumsan. Nunc nonummy 28. metus ut nunc. In id turpis vitae nisl eleifend bibendum. Curabitur cursus aliquam d29. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos 30. hymenaeos. Nulla facilisi. Integer vitae eros ac pede volutpat varius. Duis 31. pellentesque, lectus vel fringilla tincidunt, tortor erat gravida urna, vel interdum32. neque risus sit amet arcu. Phasellus sagittis sem vel eros. Nullam est sem, hendreri33. vitae, imperdiet id, fermentum nec, justo. Sed nisi risus, malesuada non, scelerisqu34. non, mattis ac, turpis. Mauris tempor commodo massa. Donec porttitor libero sed nequ35. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Cur36. Donec tortor. Sed quis dolor. </p> 37. <h4>Double</h4> 38. <p class="border5" > Duis id erat a tortor laoreet aliquet. Quisque consectetuer lobor39. mauris. Donec pede. Cras non turpis vel tortor iaculis nonummy. Ut facilisis viverra40. Morbi pretium iaculis ligula. Praesent lectus. Aenean vel ante. Nunc interdum semper41. nisl. Pellentesque tincidunt. Proin eget orci. Praesent tortor. Aenean lobortis orna42. lacus. Etiam congue. Ut egestas feugiat elit. </p> 43. <h4>Groove</h4> 44. <p class="border6" > Aliquam leo nunc, congue a, imperdiet eget, aliquet ac, tortor. S45. est. Vivamus nisi. Mauris in nisl. Sed ultricies nunc vel nunc. In dignissim consequ46. arcu. Sed in risus. Nulla facilisi. Integer purus urna, laoreet vitae, congue a, pos47. ut, ipsum. Nunc ac lacus sit amet nisi porttitor aliquam. Pellentesque ante. Etiam l48. nisl. Suspendisse a mauris vitae odio consequat pharetra. Nam egestas leo vel elit. 49. ornare risus at elit. Cras mi est, aliquam sed, dignissim quis, bibendum non, purus.50. Nullam dictum. Morbi nisl justo, dictum sit amet, lobortis non, auctor id, nunc. Cum51. sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. </52. <h4>Ridge</h4> 53. <p class="border7" > Vivamus dictum, sem in vulputate vestibulum, est tellus tempus do54. ut laoreet arcu metus eu orci. Sed enim augue, dignissim sed, porta sed, dapibus ac,55. Nunc mattis ipsum eu lectus. Nam pharetra mattis massa. Aliquam consectetuer, leo se56. pretium mollis, augue purus posuere augue, sed sagittis sapien odio in magna. Aliqua57. erat volutpat. Fusce odio. Integer euismod. Donec aliquam pede vel ipsum. Suspendiss58. potenti. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Sed quam. </p> 59. <h4>Inset</h4> 60. <p class="border8" > Maecenas consectetuer, lectus ac tempus iaculis, leo ipsum tincid61. erat, et aliquam libero nulla ac ipsum. Nam turpis leo, feugiat vel, nonummy id, orn62. arcu. Vestibulum porta, justo et ornare porta, neque eros vestibulum libero, semper 63. iaculis augue turpis eu neque. Quisque eu nisi sit amet mauris rutrum elementum. 64. Pellentesque ut nunc a tortor rutrum vulputate. Suspendisse lectus orci, luctus quis65. posuere non, pretium vel, diam. Etiam vulputate. Duis tempor posuere ipsum. Praesent66. eget risus ac turpis venenatis porttitor. Maecenas accumsan lacus sit amet urna fauc67. dapibus. </p> 68.</div> 69.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

62 of 83 2/20/2007 7:58 PM

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

Example 4.23 Rendered:

With StylesWithout Styles

body { 1. font-family: tahoma,arial,sans-serif; 2. font-size: small; 3.} 4.p { 5. margin: 1em; 6. padding: 1em; 7.} 8.p { 9. width: 30%; 10.} 11..border1 { 12. border: thin dotted #900; 13.} 14..border2 { 15. border: medium dashed #090; 16.} 17..border3 { 18. border: thick outset #009; 19.} 20..border4 { 21. border: 3px solid #999; 22.} 23..border5 { 24. border: 5px double #000; 25.} 26..border6 { 27. border: 10px groove black; 28.} 29..border7 { 30. border: 15px ridge black; 31.} 32..border8 { 33. border: 20px inset #900; 34.} 35.

Page 32: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

63 of 83 2/20/2007 7:58 PM

Margin, Padding, Border Example

Example 4.24

Example 4.24 Source:

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

Example 4.24 Rendered:

With StylesWithout Styles

<div> 1.<div class="about" > Drafted by Thomas Jefferson between June 11 and June 28, 1776, the 2. Declaration of Independence is at once the nation's most cherished symbol of liberty a3. Jefferson's most enduring monument.</div> 4.<div> We hold these truths to be self-evident, that all men are created equal, that they5. endowed by their Creator with certain unalienable Rights, that among these are Life, 6. Liberty, and the pursuit of Happiness. That to secure these rights, Governments are 7. instituted among Men, deriving their just powers from the consent of the governed. Tha8. whenever any Form of Government becomes destructive of these ends, it is the Right of 9. People to alter or to abolish it, and to institute new Government, laying its foundati10. such principles and organizing its powers in such form, as to them shall seem most lik11. effect their Safety and Happiness. </div> 12.</div> 13.

.about { 1. width: 33%; 2. text-align: left; 3. font-size: 0.75em; 4. color: #006600; 5. background: #fefecd; 6. padding: 0.5em; 7. margin: 0.75em; 8. border-width: thin; 9. border-style: dotted; 10. border-color: #900; 11. } 12.body { 13. font-size: large; 14. line-height: 1.5; 15. } 16.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

64 of 83 2/20/2007 7:58 PM

Backgrounds

background-colorbackground-imagebackground-repeatbackground-attachmentbackground-positionbackground (a shorthand property)

Example 4.25

Example 4.25 Source:

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

Example 4.25 Rendered:

With StylesWithout Styles

Example 4.26

Example 4.26 Source:

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

Example 4.26 Rendered:

With Styles

<div style="margin-left: 45%; margin-right: 45%;" > Lorem ipsum dolor sit amet, 1. consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque ur2. porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus3. vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas 4. condimentum hendrerit turpis. Lorem ipsum dolor sit amet, consectetuer adipiscing elit5. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis en6. velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vi7. felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis. </di8.

body { background-image: url(images/shield-background.gif);} 1.

<div style="margin-left: 45%; margin-right: 45%;" > Lorem ipsum dolor sit amet, 1. consectetuer adipiscing elit. Cras sollicitudin, orci nec facilisis vehicula, neque ur2. porta risus, ut sagittis enim velit at orci. Fusce velit. Integer sapien enim, rhoncus3. vitae, cursus non, commodo vitae, felis. Nulla convallis ante sit amet urna. Maecenas 4. condimentum hendrerit turpis. Lorem ipsum dolor sit amet, consectetuer adipiscing elit5. Cras sollicitudin, orci nec facilisis vehicula, neque urna porta risus, ut sagittis en6. velit at orci. Fusce velit. Integer sapien enim, rhoncus vitae, cursus non, commodo vi7. felis. Nulla convallis ante sit amet urna. Maecenas condimentum hendrerit turpis. </di8.

body { 1. background-image: url(images/shield-background.gif); 2. background-repeat: repeat-y; 3. } 4.

Page 33: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

65 of 83 2/20/2007 7:58 PM

Without Styles

Example 4.27

Example 4.27 Source:

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

Example 4.27 Rendered:

With StylesWithout Styles

<div style="margin-left: 45%; margin-right: 45%;" > 1.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec fa2.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec fa3.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec fa4.Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras sollicitudin, orci nec fa5.</div> 6.

body { 1. background-image: url(images/shield-background.gif); 2. background-repeat: no-repeat; 3. background-position: center; 4. background-attachment: fixed; 5. } 6.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

66 of 83 2/20/2007 7:58 PM

Backgrounds aren't just for the "body"

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

wood.jpgmarble.jpgwater.jpg

Example 4.28

Example 4.28 Source:

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

Example 4.28 Rendered:

With StylesWithout Styles

<div class="wood" > Wood 1. <div class="marble" > Marble 2. <div class="water" > Water </div> </div> </div> 3.

body { 1. font-size: 18pt; 2. font-weight: bold; 3. color: white;} 4.div.wood { 5. padding: 1em; 6. background-image: url(images/wood.jpg); 7.} 8.div.marble { 9. background-image: url(images/marble.jpg); 10. padding: 1em; 11.} 12.div.water { 13. background-image: url(images/water.jpg); 14. padding: 1em; 15.} 16.

Page 34: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

67 of 83 2/20/2007 7:58 PM

Backgrounds aren't just for the "body"

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

Example 4.29

Example 4.29 Source:

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

Example 4.29 Rendered:

With StylesWithout Styles

<div id="ustitle" > 1.<h1>United States Constitution</h1> 2.</div> 3.<div> Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In accumsan ipsum et lib4. Donec tincidunt. Duis non nulla a tortor sagittis bibendum. Nunc ante. Vestibulum 5. vulputate. Aliquam ultricies, est vitae mattis ornare, leo pede lacinia mi, vitae phar6. turpis enim sed turpis. Mauris lorem. Ut rhoncus. Fusce congue ultricies est. Nulla 7. dignissim sagittis ipsum. Vivamus eu lectus non enim dignissim imperdiet. Nulla facili8. Integer euismod cursus erat. </div> 9.

div#ustitle { 1. height: 80px; 2. background: #ddd url(images/flag-small.png) left center no-repeat; 3. margin-bottom: 0.5em; 4.} 5.#ustitle h1 { padding-top: 0.5em; text-align: center;} 6.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

68 of 83 2/20/2007 7:58 PM

Float

Example 4.30

Example 4.30 Source:

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

Example 4.30 Rendered:

With StylesWithout Styles

<div> 1. <div class="about" >Drafted by Thomas Jefferson between June 11 and June 28, 1776, the2. Declaration of Independence is at once the nation's most cherished symbol of liberty3. Jefferson's most enduring monument.</div> 4. <div> We hold these truths to be self-evident, that all men are created equal, that th5. endowed by their Creator with certain unalienable Rights, that among these are Life,6. Liberty, and the pursuit of Happiness. That to secure these rights, Governments are 7. instituted among Men, deriving their just powers from the consent of the governed. T8. whenever any Form of Government becomes destructive of these ends, it is the Right o9. People to alter or to abolish it, and to institute new Government, laying its founda10. such principles and organizing its powers in such form, as to them shall seem most l11. effect their Safety and Happiness. </div> 12.</div> 13.

.about { 1. float: rightright; 2. width: 33%; 3. text-align: left; 4. font-size: 0.75em; 5. color: #006600; 6. background: #fefecd; 7. padding: 0.5em; 8. margin: 0.75em; 9. border-width: thin; 10. border-style: dotted; 11. border-color: #900; 12. } 13.body { 14. font-size: large; 15. line-height: 1.5; 16. } 17.

Page 35: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

69 of 83 2/20/2007 7:58 PM

Lists

list-style-typedisc | circle | square | decimal | lower-roman | upper-roman | lower-alpha | upper-alpha | none

list-style-imagelist-style-positionlist-stylea shorthand property

Example 4.31

Example 4.31 Source:

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

Example 4.31 Rendered:

With StylesWithout Styles

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

Example 4.32

Example 4.32 Source:

<p>An ordered list:</p> 1.<strong>Winningest Active Coaches, Division I NCAA Men's Basketball</strong> 2.<ol class="basketball" > 3. <li>Mark Few</li> 4. <li>Roy Williams</li> 5. <li>Bruce Pearl</li> 6. <li>Bo Ryan</li> 7. <li>Thad Matta</li> 8. <li>Mike Krzyzewski</li> 9. <li>Jim Boeheim</li> 10. <li>Bob Huggins</li> 11. <li>Lute Olson</li> 12. <li>Tubby Smith</li> 13.</ol> 14.

ol.basketball { 1. list-style: upper-roman; 2.} 3.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

70 of 83 2/20/2007 7:58 PM

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

Example 4.32 Rendered:

With StylesWithout Styles

Example 4.33

Example 4.33 Source:

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

Example 4.33 Rendered:

With StylesWithout Styles

<p>An unordered list controlled by CSS:</p> 1.<div class="basketball" > 2.<strong>Winningest Active Coaches, Division I NCAA Men's Basketball</strong> 3.<ul class="basketball" > 4. <li>Mark Few</li> 5. <li>Roy Williams</li> 6. <li>Bruce Pearl</li> 7. <li>Bo Ryan</li> 8. <li>Thad Matta</li> 9. <li>Mike Krzyzewski</li> 10. <li>Jim Boeheim</li> 11. <li>Bob Huggins</li> 12. <li>Lute Olson</li> 13. <li>Tubby Smith</li> 14.</ul> 15.</div> 16.

ul.basketball { 1. list-style-image: url(images/basketball.gif); 2.} 3.

<p>Achieving the same effect using HTML and the IMG element (look at the source to see t1.<div> 2.<strong>Winningest Active Coaches, Division I NCAA Men's Basketball</strong><br/> 3.<img src="images/basketball.gif" alt="*" />Mark Few<br/> 4.<img src="images/basketball.gif" alt="*" />Roy Williams<br/> 5.<img src="images/basketball.gif" alt="*" />Bruce Pearl<br/> 6.<img src="images/basketball.gif" alt="*" />Bo Ryan<br/> 7.<img src="images/basketball.gif" alt="*" />Thad Matta<br/> 8.<img src="images/basketball.gif" alt="*" />Mike Krzyzewski<br/> 9.<img src="images/basketball.gif" alt="*" />Jim Boeheim<br/> 10.<img src="images/basketball.gif" alt="*" />Bob Huggins<br/> 11.<img src="images/basketball.gif" alt="*" />Lute Olson<br/> 12.<img src="images/basketball.gif" alt="*" />Tubby Smith<br/> 13.</div> 14.

/* no css rules */ 1.

Page 36: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

71 of 83 2/20/2007 7:58 PM

For the complete list: NCAA Men's Basketball Coaching Records (PDF)

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

72 of 83 2/20/2007 7:58 PM

Lists without list-style

Example 4.34

Example 4.34 Source:

Example 4.34 Rendered:

Big XIIIvy LeaguePac 10

Example 4.35

Example 4.35 Source:

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

Example 4.35 Rendered:

With StylesWithout Styles

<ul> 1. <li>Big XII</li> 2. <li>Ivy League</li> 3. <li>Pac 10</li> 4.</ul> 5.

<ul id="conferences" > 1. <li>Big XII</li> 2. <li>Ivy League</li> 3. <li>Pac 10</li> 4.</ul> 5.

ul#conferences { 1. list-style: none; 2.} 3.

Page 37: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

73 of 83 2/20/2007 7:58 PM

Display List Items "Inline"

Example 4.36

Example 4.36 Source:

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

Example 4.36 Rendered:

With StylesWithout Styles

Example 4.37

Example 4.37 Source:

In example37.css

<ul id="conferences" > 1. <li>Big XII</li> 2. <li>Ivy League</li> 3. <li>Pac 10</li> 4.</ul> 5.

ul#conferences { 1. list-style: none; 2.} 3.ul#conferences li { 4. display: inline; 5.} 6.

<ul id="conferencenav" > 1. <li><a href="http://big12sports.collegesports.com/" >Big XII</a></li> 2. <li><a href="http://www.ivyleaguesports.com/" >Ivy League</a></li> 3. <li><a href="http://www.pac-10.org/" >Pac 10</a></li> 4.</ul> 5.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

74 of 83 2/20/2007 7:58 PM

In head element:

Example 4.37 Rendered:

With StylesWithout Styles

Inspired by: CSS Cookbook by Christopher Schmitt

ul#conferencenav { 1. list-style: none; 2. padding-bottom: 0.5em; 3. border-bottom: 2px solid black; 4. font-weight: bold; 5. font-family: Arial, Verdana, Helvetica, sans-serif; 6. color: #990000; 7.} 8.ul#conferencenav li { 9. display: inline; 10. list-style: none; 11. margin: 0; 12.} 13.ul#conferencenav a{ 14. padding: 0.5em 1em; 15. margin-right: 0.25em; 16. background-color: #dddd00; 17. border-top: 2px solid black; 18. border-left: 2px solid black; 19. border-right: 2px solid black; 20. text-decoration: none; 21. } 22.ul#conferencenav a:link { 23. color: #990000; 24.} 25.ul#conferencenav a:visited { 26. color: #990000; 27.} 28.ul#conferencenav a:hover { 29. color: #dddd00; 30. background-color: #990000; 31.} 32.

<link rel="stylesheet" type="text/css" href="example37.css"/> 1.

Page 38: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

75 of 83 2/20/2007 7:58 PM

Lists as Navigation

Example 4.38

Example 4.38 Source:

In example38.css

<div id="navlhs" > 1.<ul> 2. <li><a href="http://www.baylor.edu/" >Baylor</a></li> 3. <li><a href="http://www.cu.edu/" >Colorado</a></li> 4. <li><a href="http://www.iastate.edu/" >Iowa State</a></li> 5. <li><a href="http://www.ku.edu/" >Kansas</a></li> 6. <li><a href="http://www.ksu.edu/" >Kansas State</a></li> 7. <li><a href="http://www.missouri.edu/" >Missouri</a></li> 8. <li><a href="http://www.unl.edu/" >Nebraska</a></li> 9. <li><a href="http://www.ou.edu/" >Oklahoma</a></li> 10. <li><a href="http://www.okstate.edu/" >Oklahoma State</a></li> 11. <li><a href="http://www.utexas.edu/" >Texas</a></li> 12. <li><a href="http://www.tamu.edu/" >Texas A&M</a></li> 13. <li><a href="http://www.ttu.edu/" >Texas Tech</a></li> 14.</ul> 15.</div> 16.

#navlhs { 1. font-family: Arial, Verdana, Helvetica, sans-serif; 2. font-weight: bold; 3. font-size: 0.7em; 4. width: 15em; 5. border-right: 1px solid #333; 6. margin-bottom: 1em; 7. background-color: #ddd; 8. color: #900; 9.} 10.#navlhs ul { 11. list-style: none; 12. margin: 0; 13. padding: 0; 14.} 15.#navlhs ul li { 16. margin: 0; 17. border-top: 1px solid #333; 18.} 19.#navlhs ul li a { 20. display: block; 21. background-color: #ccc; 22. border-top: 1px solid #333; 23. border-left: 5px solid #333; 24. text-decoration: none; 25. padding: 2px 2px 2px 0.5em; 26.} 27.#navlhs a:link { 28. color: #900; 29.} 30.#navlhs a:visited { 31. color: #900; 32.} 33.#navlhs a:hover { 34. color: #fff; 35. background-color: #900; 36.} 37.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

76 of 83 2/20/2007 7:58 PM

In head element:

Example 4.38 Rendered:

With StylesWithout Styles

Inspired by: CSS Cookbook by Christopher Schmitt

<link rel="stylesheet" type="text/css" href="example38.css"/> 1.

Page 39: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

77 of 83 2/20/2007 7:58 PM

Lists as Navigation

Example 4.39

Example 4.39 Source:

In example39.css

<div id="navlhs" > 1.<ul> 2. <li><a href="http://big12sports.collegesports.com/" >Big XII</a> 3. <ul> 4. <li><a href="http://www.baylor.edu/" >Baylor</a></li> 5. <li><a href="http://www.cu.edu/" >Colorado</a></li> 6. <li><a href="http://www.iastate.edu/" >Iowa State</a></li> 7. <li><a href="http://www.ku.edu/" >Kansas</a></li> 8. <li><a href="http://www.ksu.edu/" >Kansas State</a></li> 9. <li><a href="http://www.missouri.edu/" >Missouri</a></li> 10. <li><a href="http://www.unl.edu/" >Nebraska</a></li> 11. <li><a href="http://www.ou.edu/" >Oklahoma</a></li> 12. <li><a href="http://www.okstate.edu/" >Oklahoma State</a></li> 13. <li><a href="http://www.utexas.edu/" >Texas</a></li> 14. <li><a href="http://www.tamu.edu/" >Texas A&M</a></li> 15. <li><a href="http://www.ttu.edu/" >Texas Tech</a></li> 16. </ul> 17. </li> 18. <li><a href="http://www.ivyleaguesports.com/" >Ivy League</a> 19. <ul class="collapse" > 20. <li><a href="http://www.brown.edu/" >Brown</a></li> 21. <li><a href="http://www.columbia.edu/" >Columbia</a></li> 22. <li><a href="http://www.cornell.edu/" >Cornell</a></li> 23. <li><a href="http://www.dartmouth.edu/" >Dartmouth</a></li> 24. <li><a href="http://www.harvard.edu/" >Harvard</a></li> 25. <li><a href="http://www.upenn.edu/" >Penn</a></li> 26. <li><a href="http://www.princeton.edu/" >Princeton</a></li> 27. <li><a href="http://www.yale.edu/" >Yale</a></li> 28. </ul> 29. </li> 30. <li><a href="http://www.pac-10.org/" >Pac 10</a> 31. <ul class="collapse" > 32. <li><a href="http://www.arizona.edu/" >Arizona</a></li> 33. <li><a href="http://www.asu.edu/" >Arizona State</a></li> 34. <li><a href="http://www.berkeley.edu/" >California</a></li> 35. <li><a href="http://www.uoregon.edu/" >Oregon</a></li> 36. <li><a href="http://www.oregonstate.edu/" >Oregon State</a></li> 37. <li><a href="http://www.stanford.edu/" >Stanford</a></li> 38. <li><a href="http://www.ucla.edu/" >UCLA</a></li> 39. <li><a href="http://www.usc.edu/" >USC</a></li> 40. <li><a href="http://www.washington.edu/" >Washington</a></li> 41. <li><a href="http://www.wsu.edu/" >Washington State</a></li> 42. </ul> 43. </li> 44.</ul> 45.</div> 46.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

78 of 83 2/20/2007 7:58 PM

In head element:

Example 4.39 Rendered:

#navlhs { 1. font-family: Arial, Verdana, Helvetica, sans-serif; 2. font-weight: bold; 3. font-size: 0.7em; 4. width: 10em; 5. border-right: 1px solid #333; 6. margin-bottom: 1em; 7. background-color: #ddd; 8. color: #900; 9.} 10.#navlhs ul { 11. list-style: none; 12. margin: 0; 13. padding: 0; 14.} 15.#navlhs ul li { 16. margin: 0; 17. border-top: 1px none #333; 18.} 19.#navlhs ul li a { 20. display: block; 21. background-color: #ccc; 22. border-top: 1px none #333; 23. border-right: 1px solid #333; 24. border-bottom: thin dotted #333; 25. border-left: 5px solid #333; 26. text-decoration: none; 27. padding: 2px 2px 2px 0.5em; 28.} 29.#navlhs a:link { 30. color: #900; 31.} 32.#navlhs a:visited { 33. color: #900; 34.} 35.#navlhs a:hover { 36. color: #fff; 37. background-color: #900; 38. border-top: 1px outset #333; 39. border-right: 1px outset #333; 40. border-bottom: 1px outset #333; 41. border-left: 5px outset #333; 42.} 43.#navlhs a:active { 44. color: #fff; 45. background-color: #900; 46. border-top: 1px inset #333; 47. border-right: 1px inset #333; 48. border-bottom: 1px inset #333; 49. border-left: 5px inset #333; 50.} 51.#navlhs ul ul a { 52. list-style: none; 53. padding-left: 1.5em; 54. font-size: 80%; 55. font-weight: normal; 56.} 57.#navlhs ul.collapse { 58. display: none; 59.} 60.

<link rel="stylesheet" type="text/css" href="example39.css"/> 1.

Page 40: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

79 of 83 2/20/2007 7:58 PM

With StylesWithout Styles

Inspired by: CSS Cookbook by Christopher Schmitt

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

80 of 83 2/20/2007 7:58 PM

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:

allSuitable for all devices.

brailleIntended for braille tactile feedback devices.

embossedIntended for paged braille printers.

handheldIntended for handheld devices (typically small screen, limited bandwidth).

printIntended for paged material and for documents viewed on screen in print preview mode.

projectionIntended for projected presentations, for example projectors.

screenIntended primarily for color computer screens.

speechIntended for speech synthesizers. Note: CSS2 had a similar media type called 'aural' for thispurpose.

ttyIntended for media using a fixed-pitch character grid (such as teletypes, terminals, orportable devices with limited display capabilities).

tvIntended for television-type devices (low resolution, color, l imited-scrollability screens, soundavailable).

<link rel="stylesheet" type="text/css" media="all" href="screen.css" /> 1.<link rel="stylesheet" type="text/css" media="print" href="print.css" /> 2.

@media print { 1. body {font-size: 10pt;} 2.} 3.@media screen { 4. body {font-size: 12pt;} 5.} 6.@media print { 7. div.navigation {display: none;} 8. hr {page-break-after: always;} 9.} 10.

Page 41: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

81 of 83 2/20/2007 7:58 PM

Harvard College Funding Database

Screen display (screen.css):

Print display (print.css):

<link rel="stylesheet" type="text/css" media="all" href="screen.css" /> 1.<link rel="stylesheet" type="text/css" media="print" href="print.css" /> 2.

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

82 of 83 2/20/2007 7:58 PM

Page 42: Cascading Style Sheets (CSS) - cscie12.dce.harvard.edu · Cascading Style Sheets (CSS)  5 of 83 2/20/2007 7:58 PM Footer Contents of

Cascading Style Sheets (CSS) http://localhost:8080/cocoon/projects/cscie12/slides/20070220/handout.html

83 of 83 2/20/2007 7:58 PM

Resources

Dan Cederholm

Westciv: CSS and web standards tutorialsPublications from Dan Cederholm from SimpleBits

Eric Meyer

meyerweb.comEric Meyer CSS Workcss / edge

Books

CSS Cookbook by Christopher SchmittCascading Style Sheets : Designing for the Web (3rd Edition) by Hakon Wium Lie and Bert BosCascading Style Sheets: The Definitive Guide, 3rd Edition by Eric Meyer Eric Meyer on CSS and More Eric Meyer on CSSWeb Standards Solutions: The Markup and Style Handbook by Dan Cederholm

Specifications / Resources

W3C Cascading Style SheetsCascading Style Sheets, Level 1Cascading Style Sheets, Level 2Cascading Style Sheets 2.1W3C CSS Validation ServiceW3C Core Styles

Table of Contents | All Slides | Link List | Examples | CSCI E-12