Enhanced Web Site Design: Stanford University Continuing Studies CS 22
Enhanced Web Site Design: Stanford University Continuing Studies CS 22
Mark Branom
markb@stanford.edu
http://www.stanford.edu/people/markb/
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 1 (of 54)
Cascading Style Sheets
• Unfinished business
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 2 (of 54)
What are Cascading
Style Sheets?
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 3 (of 54)
What are Cascading
Style Sheets?
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 4 (of 54)
What are Cascading
Style Sheets?
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 5 (of 54)
What are Cascading
Style Sheets? continued
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 6 (of 54)
What are Cascading
Style Sheets? continued
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 7 (of 54)
What are Cascading
Style Sheets? continued
• CSS Specifications:
– CSS 1: http://www.w3.org/TR/REC-CSS1-
961217.html
– CSS 2: http://www.w3.org/TR/CSS2/
– CSS 2.1: http://www.w3.org/TR/CSS21/
• For more detailed, technical discussions of the
differences between CSS 1, CSS 2, and CSS 2.1, go
to the following:
– Between CSS 1 and CSS 2:
http://www.w3.org/TR/CSS2/changes.html
– Between CSS 2 and CSS 2.1:
http://www.w3.org/TR/CSS21/changes.html
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 8 (of 54)
Pros and Cons of Using
CSS
• Pros
– Greater designer control of the appearance of the
page
– Easier management of site-wide changes
– Greater accessibility to web sites by non-graphical
browsers and web-page-reading software
• Cons
– Different browsers may interpret Style Sheets in
different ways
– Some styles may not be seen at all on some
browsers
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 9 (of 54)
CSS Examples
• The CSS Zen Garden shows some of the most advanced uses
of CSS:
http://www.csszengarden.com/
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 10 (of 54)
CSS Basics
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 11 (of 54)
CSS Rules
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 12 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 13 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 14 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 15 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 16 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 17 (of 54)
Grouping Styles and
Selectors
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 18 (of 54)
Where do you put the
styles?
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 19 (of 54)
Style Location: External
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 20 (of 54)
Style Location: External
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 21 (of 54)
External example
<head> <head>
</head> -->
</style>
</head>
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 22 (of 54)
Style Location: Internal
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 23 (of 54)
Style Location: Inline
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 24 (of 54)
Hierarchy of Styles
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 25 (of 54)
!important
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 26 (of 54)
Classes and IDs
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 27 (of 54)
Classes and IDs
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 28 (of 54)
Example: Class
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 29 (of 54)
Inline vs. Block Display
(HTML)
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 30 (of 54)
Inline vs. Block Display
(CSS)
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 31 (of 54)
Example – display:
block;
• Normally, <a> tags display inline.
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 32 (of 54)
Example – display:
inline;
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 33 (of 54)
Span and Div
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 34 (of 54)
Example: SPAN, DIV,
Class, and ID
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 35 (of 54)
Unit Measurements
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 36 (of 54)
Unit Measurements
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 36 (of 54)
Unit Measurements
• Examples:
body { font-size: 12px; }
h1, h2, h3 { line-height: 200%;}
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 36 (of 54)
Font and Text Styling
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 37 (of 54)
Modifying List Elements
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 38 (of 54)
The Box Model
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 39 (of 54)
The Box Model: IE vs.
CSS
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 40 (of 54)
Pseudo-elements and
Pseudo-classes
• Pseudo-elements and pseudo-classes are special pre-defined
groupings used in CSS to deal with special situations that do
not exist in standard HTML. For example, under standard
HTML, there is no way to automatically change the look and
feel of the first letter or line of a paragraph. But the pseudo-
element :first-letter allows you to specify a style that
affects the first letter and the pseudo-element :first-
line allows you specify a style that affects the first line.
h1:first-letter { font-size: 200%; color:red;}
p:first-line { color: pink;}
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 41 (of 54)
Positioning
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 42 (of 54)
Absolute, Relative, Fixed,
Inherit, and Static Positioning
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 43 (of 54)
Example:
position:absolute
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 44 (of 54)
Example:
position:relative
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 45 (of 54)
Example:
position:fixed (Code View)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML <div id="main">
1.0 Transitional//EN" <p>Lorem ipsum dolor sit amet, consectetuer
"http://www.w3.org/TR/xhtml1/DTD/xhtml adipiscing elit. Quisque ultrices, nibh
1-transitional.dtd"> ac rhoncus fermentum, orci sem dapibus
<html xmlns="http://www.w3.org/1999/xhtml"> nisi, sed tincidunt lectus lectus at
augue. In consectetuer vehicula enim. In
<head> hac habitasse platea dictumst. Donec a
<meta http-equiv="Content-Type" nisl vitae tortor tristique viverra. Sed
content="text/html; charset=iso-8859- at lorem a ante lobortis molestie.
1" /> Nulla ullamcorper urna accumsan diam.
Aliquam non eros. Pellentesque egestas
<title>Untitled Document</title> ultricies enim. Aenean lobortis. Nulla
<style type="text/css"> interdum commodo turpis. Sed ut mi id
<!-- elit vehicula sollicitudin. Sed lobortis,
ligula sit amet euismod egestas, mi
#links { ante iaculis nunc, ut rhoncus magna
position:fixed; lectus ac arcu. In hac habitasse platea
border:dotted; dictumst. Proin quis ligula vitae quam
pharetra adipiscing. Pellentesque
border-color:#000000; tincidunt suscipit nibh. Ut fermentum
width:20%; suscipit justo. </p>
height:100%; <p>Fusce purus lectus, ultricies nec, aliquam
at, facilisis id, arcu. Vestibulum quis
z-index:1; mi vel massa porta hendrerit. Nulla
left: 0px; ullamcorper ligula nec lectus. Quisque
top: 0px; tempor, augue in molestie gravida, eros
arcu luctus tortor, eu dignissim diam
background-color: #FFFFCC; urna sed urna. Ut dictum ultrices lacus.
} In hac habitasse platea dictumst.
#main { Suspendisse sed purus blandit metus
ultricies suscipit. Proin diam justo,
position:absolute; consequat id, rhoncus eget, facilisis ut,
left:25%; lacus. Vivamus dignissim dui in justo.
Suspendisse elit. Nam nulla tortor,
top:0px; fringilla sed, faucibus quis, ullamcorper
width:70%; a, leo. Fusce blandit condimentum
} turpis. Pellentesque vel odio et odio
suscipit egestas. Nullam ullamcorper
--> sagittis ipsum. Maecenas fringilla
</style> malesuada pede. Duis ut quam. </p>
</head> </div>
<body> <div id="links">
<p>This area is fixed and will never move.
It's good for things like navigation
bars.</p>
<ul>
<li><a href="page1.html">Page 1</a></li>
<li><a href="page2.html">Page 2</a></li>
<li><a href="page3.html">Page 3</a></li>
<li><a href="page4.html">Page 4</a></li>
<li><a href="page5.html">Page 5</a></li>
</ul>
</div></body></html>
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 46 (of 54)
Example:
position:fixed (in Firefox)
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 47 (of 54)
Layers and the
Bounding Box
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 48 (of 54)
Layering Example 1
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 49 (of 54)
Layering Example 2
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 50 (of 54)
Float
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 51 (of 54)
Float Example 1 – float:
right
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 52 (of 54)
Float Example 2 – float:
left
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 53 (of 54)
Resources
• A List Apart – articles on practical issues and suggestions for working with CSS correctly
http://www.alistapart.com/topics/code/css
• Example XHTML Pages, with and without the CSS Style Sheet:
http://techbriefings.stanford.edu/web_standards/example1.html
http://techbriefings.stanford.edu/web_standards/example2.html
http://techbriefings.stanford.edu/web_standards/example.css
• The CSS Zen Garden shows some of the most advanced uses of CSS:
http://www.csszengarden.com/
• CSS in the real world: ajc.com's 'News Break':
http://www.holovaty.com/blog/archive/2002/09/28/2340
• Microsoft's CSS Information:
http://msdn.microsoft.com/workshop/author/css/reference/
attributes.asp
• Microsoft's Style Sheet Demonstrations:
http://www.microsoft.com/typography/css/gallery/extract1.htm
http://www.microsoft.com/typography/css/gallery/slide1.htm
• W3C Style Examples
http://www.w3.org/Style/Examples/007
• W3C CSS 2.1 Specifications:
http://www.w3.org/TR/CSS21/
• W3Schools CSS Tutorial:
http://www.w3schools.com/css
• W3Schools CSS Reference:
http://www.w3schools.com/css/css_reference.asp
• Webmonkey’s Cascading Style Sheet Guide:
http://www.webmonkey.com/reference/stylesheet_guide/
• Brian Wilson’s Cascading Style Sheet Reference Guide:
http://www.blooberry.com/indexdot/css/index.html
CS 22: Enhanced Web Site Design - Cascading Style Sheets Slide 54 (of 54)