April 21, 2008
David P. Heitmeyer
david_heitmeyer@harvard.edu
Harvard University
Division of Continuing Education
Extension School
Markup and Presentation IntertwinedSymptoms
Consequences
|
Separated
|
|
|
|
|
|
|
|
|
|
With CSS disabled:
|
Heading elements (h1,h2,etc.) combined with CSS are very powerful. Headings can remain headings in markup and CSS can style them as desired.
Lists combined with CSS are very powerful. Lists can remain lists in markup (navigation, content items, etc.) and CSS can style them as desired.
Department of African and African American Studies
With CSS disabled:
css Zen Garden: The Beauty in CSS Design. A demonstration of what can be accomplished visually through CSS-based desgin.
|
|
|
|
|
|
|
|
And here is the stylesheet (simple-style.css):
www.alistapart.com |
www.simplebits.com |
meyerweb.com/eric/css/ |
www.westciv.com/style_master/house |
Three ways to bind CSS rules to XHTML/HTML markup:
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 type="text/css">)
within
head element:<style type="text/css">)
within
head element:
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.
<style type="text/css">)
within
head element:<style type="text/css">)
within
head element:<style type="text/css">)
within
head element:ul li
p.abstract
.warn
#main
#head
body > p
ul > li
input[type=text]
input[type=radio]
<style type="text/css">)
within
head element:
Note: Firebug is a very useful Firefox extension for analyzing CSS properties and values.
Suggested reading: CSS 2.1: Assigning property values, Cascading, and Inheritance
<style type="text/css">)
within
head element:|
CSS Level 1 lists 53 properties.
|
CSS Level 2.1 lists 98 properties .
|
You can specify font-size by:
Praesent tincidunt, elit porta elementum pharetra, magna odio consequat nisi, eu iaculis mauris massa eu eros.
By Name:
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:
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:
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:
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.
As a general guideline with CSS, relative measurements are better than absolute measurements.
Relative size are relative to parent. Nested div elements with font-size: 85%
and margin-left: 50px have properties of:
div:
div:
div:
div:
<style type="text/css">)
within
head element:[font-style | font-variant | font-weight ]? font-size[/line-height]? font-family
<style type="text/css">)
within
head element: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.
The following are all equivalent ways of defining a shade of crimson:
|
|
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Image from Cascading Style Sheets: The Definitive Guide, 3rd ed by Eric Meyer, published by O'Reilly
Firebug visualization of block model (Layout):
Stay out of "TRBL" (top right bottom left) for padding and margin shorthand.
border is a shorthand notation that allows you to set
border-width, border-style,
border-color in one statement.
<style type="text/css">)
within
head element:
<style type="text/css">)
within
head element:A background image with CSS:
<style type="text/css">)
within
head element:Repeat on y-axis only:
<style type="text/css">)
within
head element:Center, Don't Repeat and Fix position:
<style type="text/css">)
within
head element:<style type="text/css">)
within
head element: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.
<style type="text/css">)
within
head element:
The image (5px × 150px; 254 bytes):
"Normal" size:
"Increased +4":
<style type="text/css">)
within
head element: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:
Rendered in a browser:
<style type="text/css">)
within
head element:
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-letter pseudo element.
<style type="text/css">)
within
head element:
first-line pseudo element
<style type="text/css">)
within
head element:Opening paragraph with first-letter pseudo element.
<style type="text/css">)
within
head element:
<style type="text/css">)
within
head element: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 |
|---|---|
|
|
Choosing class and id names appropriately will help with:
#rightnav may very well become navigation positioned on the
left side or the top.
.redbold may very be changed to another color or background
entirely..dottedborder class, you may wish to change how your
thumbnail images are styled, but leave presentation of other markup that you've given
the same class to unchanged. See: Choosing Class and ID Names
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: right
|
<style type="text/css">)
within
head element:The "clear" property defines the sides of a block where floated blocks cannot occur.
float: right; clear: none
|
float: right; clear: right
|
<style type="text/css">)
within
head element:The "clear" property defines the sides of a block where floated blocks cannot occur.
clear: none
|
clear: right
|
<style type="text/css">)
within
head element:
<style type="text/css">)
within
head element:
<style type="text/css">)
within
head element:Use float: left and float: right for content to be on
opposite sides.
<style type="text/css">)
within
head element: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.
<style type="text/css">)
within
head element:We want to float one element left, the other right. We want content to be between the floated elements.
<style type="text/css">)
within
head element:Make the middle content not wrap around the floated contents (set margin for the middle content).
<style type="text/css">)
within
head element: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.
It is important to set height and width !
Without setting height and width:
Setting height and width:
<style type="text/css">)
within
head element:Can use CSS to turn off display. This will be especially useful to manipulate CSS properties with JavaScript.
Full list for the American League:
With ALE and ALW display set to "none":
<style type="text/css">)
within
head element:An ordered list:
<style type="text/css">)
within
head element:And now, let's add a basketball icon as a list bullet (image is at images/basketball.gif,
An unordered list controlled by CSS:
<style type="text/css">)
within
head element:How to accomplish this without CSS.
<style type="text/css">)
within
head element:<style type="text/css">)
within
head element:
Unordered List |
Inline List Items |
Inline List Items |
<style type="text/css">)
within
head element:Inspired by: CSS Cookbook by Christopher Schmitt
Default styling:
As navigation:
Inspired by: CSS Cookbook by Christopher Schmitt
Default styling of nested list:
Nested list styled as clamshell navigation:
Inspired by: CSS Cookbook by Christopher Schmitt
"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.
Strategy is similar to that of creating tabs. Use "display: inline" to make list items inline; use background image for "li" to show arrow.
CSS2 defines a media selector for different media types.
Incorporate as part of the "link" element:
Or, use "@media" selector in style rule:
Screen display (screen.css):
Print display (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!
The Yahoo! User Interface (YUI) Library is a set of utilities and controls, written in JavaScript, for building richly interactive web applications using techniques such as DOM scripting, DHTML and AJAX. The YUI Library also includes several core CSS resources. All components in the YUI Library have been released as open source under a BSD license and are free for all uses.
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.)
Tables exposed:
| Markup Type | HTML 4.01 Transitional
53 validation errors |
||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Markup Size | 26.1 k | ||||||||||||||||||||||||||||||||||||
| Markup (after 'tidy') | 380 lines | ||||||||||||||||||||||||||||||||||||
| Document dependencies |
|
||||||||||||||||||||||||||||||||||||
| Total page weight | 226 k | ||||||||||||||||||||||||||||||||||||
| Elements |
314 total
|
||||||||||||||||||||||||||||||||||||
| Attributes | 588 |
Harvard Homepage with Markup/Style Makeover
| Current | After Markup/Style Makeover | |
|---|---|---|
| Screenshot |
|
|
| 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 |
|
| Document dependencies |
|
|
| 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:
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.
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.
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.
Tab Plugin for jQuery allows the easy creation of tabs from simple markup.
Without JS and CSS:
With JS and CSS:
Markup:
JS:
jQuery has plugins that will provide an unobtrusive "calendar" widget for picking dates.
Markup:
JS:
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>:
Example: Gallery with Thickbox
Add
Thickbox
and give the "a" elemnts
a class="gallery":
Gallery
Viewing an individual image:
View example galleries implemented in Lightbox and Lightbox 2 (using Prototype/Scriptaculous) as well as Thickbox (using jQuery).