Victor Costan
1997: | html | 2000: | css | 2005: | js | ||
css | css | js | |||||
js | css | js | |||||
html | html | css | |||||
css | js | css | |||||
js | html | css | |||||
html | js | html | |||||
css | html | html | |||||
js | js | html |
<!DOCTYPE html> <html> <head> <title>An HTML document.</title> </head> <body> <p>This is a paragraph.</p> <p>This is another paragraph with a link <a href="http://www.google.com">to Google</a> </p> </body> </html>
<p>
</p>
<img />
<p>This element is a paragraph of text</p>
Good | Bad |
---|---|
<p>This is a paragraph <em>with emphasized text</em></p> |
<p>This is a paragraph <em>with emphasized text</p></em> |
<a href="http://www.google.com">Google</a> <video src="funny.ogg" autoplay="autoplay" />
=
"
)<p>
in a paragraph?<p>This is a paragraph about <p;></p>
&
and end with ;
& | & |
© | " |
< | < |
ASCII 127 | F; |
> | > |
™ | ™ |
<!DOCTYPE html> <!-- The top-level element is always html --> <html> <head> <!-- The document's invisible contents (metadata) goes in the head. --> <title>This title is shown in the browser's title bar.</title> </head> <body> <!-- Everything that the user can see goes in the body. --> </body> </html>
<html>
wraps everything. <head>
always goes before <body>
.<!DOCTYPE html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<p>This is a paragraph of text. It covers an idea.</p> <p>This is another paragraph of text. It's a bit longer, so I had to break it up on three lines. That's OK, because HTML doesn't care about line breaks. </p>
Always wrap text in some element. <p>
is a good default if you don’t know where your text belongs.
<h1>New York Times</h1> <h2>Technology</h2> <h3>HTML 5 Standard Slated for 2012</h3> <h3>Firefox 3.6 Released Today</h3> <h2>Sports</h2>
Choose heading numbers to reflect semantics, not presentation.
Example: <h3>
should never follow <h1>
.
<p><em>Mary</em> had a little lamb.</p> <p>Mary <em>had</em> a little lamb.</p> <p>Mary had a <em>little</em> lamb.</p>
Semantic (Good) | Presentation (Bad) |
<em> |
<i> |
<strong> |
<b> |
<a href="http://www.twitter.com/victor_costan">Follow me on Twitter</a> <a href="/logout.php">Log out</a> <a href="chapter1.html">Chapter 1 of the story</a>
href
means hyper-referenceUnordered | Ordered |
---|---|
<p>This is my shopping list:</p> <ul> <li>eggs</li> <li>bread</li> <li>milk</li> </ul> | <p>Top 3 programming languages:</p> <ol> <li>Java</li> <li>C</li> <li>PHP</li> </ol> |
<ol>
, <ul>
: Ordered List, Unordered List<li>
: List Item<dl> <dt>HTML</dt> <dd>the Web's markup language</dd> <dt>JavaScript</dt> <dd>the Web's programming language</dd> </dl>
<dl>
: Definition List<dt>
: Definition Term<dd>
: Definition Description<img src="images/big_doll.png" alt="Short description for visitors who can't see images" />
<img>
tag should be empty.href
instead of src
.alt
attribute is mandatory.width
and height
attributes are deprecated. Use CSS instead.<video src="ads/annoying.ogg" autoplay="autoplay" controls="controls" loop="loop"> Display this text if the browser doesn't support video. </video> <audio src="boom_headshot.ogg" loop="loop" />
autoplay
with anything that makes sound.controls
is browser-specific. Consider building your own controls using JavaScript.<table> <tr> <td>Expenses</td><td>2008</td><td>2009</td> </tr> <tr> <td>T-shirts</td><td>$1,000</td><td>$2,000</td> </tr> <tr> <td>Pizza</td><td>$350</td><td>$450</td> </tr> <tr> <td>Total</td><td>$1,350</td><td>$2,450</td> </tr> </table>
Expenses | 2008 | 2009 |
T-shirts | $1,000 | $2,000 |
Pizza | $350 | $450 |
Total | $1,350 | $2,450 |
<table> <thead> <tr> <th>Expenses</th><th>2008</th><th>2009</th> </tr> </thead> <tbody> <tr><td>T-shirts</td><td>$1,000</td><td>$2,000</td></tr> <tr><td>Pizza</td><td>$350</td><td>$450</td></tr> </tbody> <tfoot> <tr> <th>Total</th><td>$1,350</td><td>$2,450</td> </tr> </tfoot> </table>
Expenses | 2008 | 2009 |
---|---|---|
T-shirts | $1,000 | $2,000 |
Pizza | $350 | $450 |
Total | $1,350 | $2,450 |
<table> <tr> <td colspan="2">Header</td> </tr> <tr> <td> Navigation </td> <td> Main </td> </tr> <tr> <td colspan="2">Footer</td> </tr> </table>
Don’t use tables for layout. HTML is for markup, CSS is for presentation.
<form action="http://www.google.com/search" method="POST"> <dl> <dt>Query</dt> <dd><input type="text" name="q" /></dd> <dt>Language</dt> <dd> <select name="lang"> <option value="en">English</option> <option value="fr">French</option> </select> </dd> </dl> <p><input type="submit" value="Search" /></p> </form>
POST /search HTTP/1.1 Content-Encoding: application/x-www-form-urlencoded Host: www.google.com q=unicorns&lang=en
Action | /search |
q |
unicorns |
lang |
en |
<form action="http://www.google.com/search" method="GET"> <dl> <dt><label for="goog_query">Query</label></dt> <dd><input type="text" name="q" id="goog_query" /></dd> </dl> <fieldset> <legend>Query Details</legend> <dl> <dt><label for="goog_language">Language</label></dt> <dd><input type="text" name="lang" value="en" id="goog_language" /></dd> <dt><label for="goog_encoding">Query encoding</label></dt> <dd><input type="text" name="ie" value="utf-8" id="goog_encoding" /></dd> </dl> </fieldset> <p><input type="submit" value="Search" /></p> </form>
Which tags are semantic (good), and which tags are purely for presentation (bad, forbidden, and deprecated).
<b> |
<big> |
<blockquote> |
<blink> |
<br> |
<center> |
<cite> |
<del> |
<ins> |
<i> |
<marquee> |
<pre> |
<q> |
<small> |
<sub> |
<sup> |
<tt> |
<u> |
Demo code available at:
http://bit.ly/6Gantb
http://github.com/costan/html_css_js_slides/ raw/master/exercises/html_start.html
Scaffold code available at:
http://bit.ly/813OjF
http://github.com/costan/html_css_js_slides/ raw/master/exercises/html_partial.html
Solution available at:
http://bit.ly/84UNOb
http://github.com/costan/html_css_js_slides/ raw/master/exercises/html_finish.html
Change color, size, font, and position
<p style="color: red;"/>This is a red paragraph.</a>
<style type="text/css"> p { color: red; } </style> <p>This is a red paragraph.</p>
<style>
tags are allowed in <head>
and <body>
.<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="code/css/red.css" /> </head> <body> <p>This paragraph will be red.</p> </body> </html>
p { color: red; }
<link>
tags are only allowed in <head>
.
<head>
<!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="code/css/red.css" /> </head> <body> <style type="text/css"> @include url("more_red.css"); </style> <p>This paragraph will be red.</p> </body> </html>
h1 { color: blue; } p em { font-size: 12pt; font-family: Arial, Helvetica, sans-serif; line-height: 1.2em; }
h1 { color: blue; } p em { font-size: 12pt; line-height: 1.2em; }
Selector | Declarations | |
Rule 1 | h1 |
color: red; |
Rule 2 | p em |
font-size: 12pt; line-height: 1.2em; |
The selector comes first, then the declarations are enclosed by brackets.
h1 { color: blue; } p em { font-size: 12pt; font-family: Arial, Helvetica, sans-serif; }
Property | Value |
color |
red |
font-size |
12pt |
line-height |
Arial, Helvetica, sans-serif |
Each declaration in a rule specifies a value for a property.
@include url("code/css/red.css"); @font-face { font-family: Myriad Pro; src: url("fonts/Myriad_Pro.otf"); }
@
.p
elements.p { color: red; }
id="summary"
.#summary { color: blue; }
class
attribute contains highlight
..highlight { color: red; }
.small { font-size: 10pt; } .highlight { color: red; }
<p class="highlight">This is highlighted.</p> <p class="highlight small">This is small and highlighted.</p> <p class="small">This is small.</p>
The class
attribute can contain multiple class names, separated by spaces.
/* <p> elements whose class contains highlight and small */ p.highlight.small { color: red; }
,
/* <h1> <h2> and <h3> elements */ h1, h2, h3 { color: red; }
/* <p> elements inside <form> elements */ form p { color: blue; }
>
/* <p> elements who are direct children of <form> elements */ form > p { color: blue; }
+
/* <p> elements coming right after <ul> elements */ ul + p { color: gray; }
~
/* <p> elements whose parents have <ul> children, and who come after the <ul> children in the document */ ul ~ p { color: gray; }
:hover |
The user’s mouse is over the element |
:focus |
The control has the input focus |
:active |
The user is about to activate the element (e.g. follow a link) |
:link |
The element is a link that the user has not visited |
:visited |
The element is a link that the user has visited |
a:link { color: blue; } a:visited { color: gray; } a:hover { color: red; } a:active { color: black; }
What does this do?
:first |
The first child (e.g. the first paragraph in the body) |
:last |
The last child |
:nth-child(even) |
All even children (2nd, 4th, …) |
:nth-child(2n) |
All even children (2nd, 4th, …) |
:nth-child(odd) |
All odd children |
:nth-child(2n+1) |
All odd children |
Very useful for improving readability by alternating font colors in lists and tables.
ul#master-index > li:nth-child(even).small p.highlighted.twisted.legal a:hover
A hovered link inside
a paragraph (<p>
) with classes highlighted
and twisted
inside
a list item (<li>
) with class small
which is an even child of
an unordered list (<ul>
) with id="master-index"
p { color: red; } p { color: blue; }
<p>What color is this paragraph?</p>
p.highlight { color: red; } p { color: blue; }
<p class="highlight">What color is this paragraph?</p>
Declarations have the following priority:
style
attributes always win.!important
, ordered by specificity.!important
, ordered by specificity.!important
, ordered by specificity.!important
, ordered by specificity.A declaration’s specifity is determined by its selector.
To compute a selector’s specificity:
p#header { color: red; }
p li.highlight { color: red; }
#header p.small a:hover { color: red; }
Specificities are ordered lexicographically:
Declarations cover these major areas
color
– the foreground color (text, list bullets)background-color
border-color
p { color: red; }
p { color: rgb(255, 0, 0); }
p { color: #FF0000; }
width
, height
, min-height
, max-height
, min-width
, max-width
– the size of an element’s boxfont-size
, text-indent
, line-height
– typographyborder-width
, padding
, margin
– layout stufftop
, right
, bottom
, left
– more layout stuffp { border-width: 2px; font-size: 12pt; width: 80%; min-height: 8em; margin: 1cm; }
px
– pixels; defined by the screen’s resolutioncm
, mm
, in
– physical units, size changes with screen resolutionpt
– points; usually used for fonts; 1 pt = 1/72 inThe absolute units don’t need a context to be interpreted.
%
– percentage of some other sizeem
– the font-size
of the parent elementFor %
, the size is usually the same size in the parent element.
width
, min-width
, max-width
refer to the width
of the parent element.font-size
refers to the font-size
of the parent element.
Generic families: serif, sans-serif, monospace, cursive, fantasy
Fonts available on most computers:
serif | sans-serif | monospace |
Garamond | Georgia | Courier New |
Times New Roman | Helvetica | |
Tahoma | ||
Verdana |
p { font-family: Myriad Pro, Verdana, Tahoma, sans-serif; }
@font-face { font-family: Myriad Pro; src: url("fonts/Myriad_Pro.otf") format("otf"); }
font-style
, font-variant
, text-decoration
font-style |
font-variant |
text-decoration |
italic | small-caps | underline |
oblique | normal | overline |
normal | line-through | |
none |
100 – lighter
400 – normal
700 – bold
800 – bolder
font-size
sets the size of the em box.1 em
is the size of the current font’s em box, not the size of capital M.line-height
, discussed next.p { font-size: 12pt; line-height: 2em; }
line-height
> font-size
: the difference is divided by two, added to the top and bottom of the em boxesline-height
< font-size
: line-height
is divided by two, the halves go above and below the font’s median lineh1 { padding-top: 20px; padding-right: 0; padding-bottom: 40px; padding-left: 10px; } /* Same as above, using shorthand. */ h2 { padding: 20px 0 40px 10px; } /* 5px top and bottom, 10px left and right. */ h3 { padding: 5px 10px; } /* 0 padding all around. */ p { padding: 0; }
top
, right
, bottom
, left
The following properties need to be set:
border-width
– the thickness of the border (a CSS size)border-style
– solid, dashed, dotted, inset, outset, grooveborder-color
is optional, defaults to the foreground color.
h1 { border-width: 2px; border-style: solid; border-color: black; } /* Same as above, using shorthand. */ p { border: 2px solid black; }
Each of the border’s sides can be set individually using both normal properties and short-hand.
h1 { border-left-width: 2px; border-left-style: solid; border-left-color: black; border-right: 2px solid black; }
Inline elements can share a line. Examples: <em>
, <strong>
, <input>
, <select>
, <img>
.
Block elements take the entire line for themselves. Examples: <p>
, <ul>
, <ol>
, <li>
, <h1>
– <h6>
.
A paragraph.
Another paragraph.
Unless specified otherwise, HTML elements are laid out according to the flow.
<div>
and <span>
have no intrinsic meaning. They exist solely as hooks for CSS and JavaScript.<div>
is a block element.<span>
is an inline element.<div>
or <span>
should have a class
or id
attribute.<div>
tags instead of tags with semantic meaning).<body> <div id="intro"> <p>The thesis of this essay is...</p> </div> <div id="contents"> <p>The first supporting point is...</p> <p>The second supporting point is...</p> <p>The third supporting point is...</p> </div> <div id="conclusion"> <p>In conclusion, ...</p> </div> </body>
The divs convey semantic information that is not reflected by the collection of paragraphs.
position
to something other than static
.left
, right
, top
and bottom
to change the default position. position |
In flow | Positioning |
static |
yes | according to the flow |
relative |
ghost | relative to the flow position |
absolute |
no | document coordinate space |
fixed |
no | screen coordinate space |
p:first { float: right; }
The text in this paragraph is floated to the right. The text in this paragraph is floated to the right.
This is a normal paragraph. It will flow around the float. This is a normal paragraph. It will flow around the float. This is a normal paragraph. It will flow around the float.
This is another normal paragraph. It will flow around the float. This is another normal paragraph. It will flow around the float. This is another normal paragraph. It will flow around the float.
p:first { float: right; } p:last { clear: both; }
The text in this paragraph is floated to the right. The text in this paragraph is floated to the right.
This is a normal paragraph. It will flow around the float. This is a normal paragraph. It will flow around the float. This is a normal paragraph. It will flow around the float.
This is a cleared paragraph. It will not flow around the float. This is a cleared paragraph. It will not flow around the float. This is a cleared paragraph. It will not flow around the float.
Demo code available at:
http://bit.ly/4AJXqz
http://github.com/costan/html_css_js_slides/ raw/master/exercises/css_demo.html
Scaffold code available at:
http://bit.ly/5ORxcN
http://github.com/costan/html_css_js_slides/ raw/master/exercises/css_start.html
Solution available at:
http://bit.ly/84UNOb
http://github.com/costan/html_css_js_slides/ raw/master/exercises/css_finish.html
<a href="javascript:alert('Lame script!');"/>Click me</a> <form action="#"> <input type="submit" onclick="alert('Lame script!');" value="Click me"> </form>
Forget you saw this.
<script type="text/javascript"> alert("I'm alive!"); </script>
<script>
tags can be added in <head>
or <body>
. There is a subtle difference that we’ll discuss later.
<html> <head> <title>Annoying page.</title> <script type="text/javascript" src="code/js/alert.js"></script> </head> <body> </body> </html>
alert("I'm alive!");
Demo code available at:
http://bit.ly/5XoTyi
http://github.com/costan/html_css_js_slides/ raw/master/exercises/js_start.html
Victor Costan
This Presentation