CSE 154 Extra Sessions

Lecture 1: Typography; Transitions; Responsive Web Design

Reading:

Except where otherwise noted, the contents of this document are Copyright 2012–2013 Morgan Doocy. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.

Valid HTML5 Valid CSS

1. Web typography

Line spacing (“leading”): line-height

p {
font-size: 18px;
line-height: 3em;
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut leo enim. Curabitur eleifend interdum enim, mattis dignissim lacus placerat gravida. Donec et eros et leo gravida condimentum et non lorem. Morbi velit quam, imperdiet a sodales in, laoreet in lectus. Vestibulum auctor massa sit amet metus ultricies nec vestibulum lacus adipiscing. Duis hendrerit malesuada diam ac porta.

More CSS properties for text (3.1.6)

property description
vertical-align vertical offset from the line of text
line-height distance between lines of text
word-spacing distance between words
letter-spacing distance between letters
text-indent indents the first line only of a paragraph
text-transform alters the capitalization of text
font-variant switches to (or simulates) small caps variant
white-space controls how whitespace is handled within an element
Complete list of text properties

Anatomy of type

Anatomy of type: ascent, descent, baseline, median line, x-height, cap height

The vertical-align property

Sphinx Sphinxþ

Interactive Example vertical-align:
property description
vertical-align specifies inline element’s vertical position on the line of text; can be top, middle, bottom, baseline (default), sub, super, text-top, text-bottom, or a length value or %

Letter & word spacing

h1 { font-size: 5em; word-spacing: -.05em; letter-spacing: -.05em; }
h2 { letter-spacing: .03em; word-spacing: -.075em; }
abbr { font-size: 80%; letter-spacing: .075em; }

Avant Garde

wharves and warfingers

The W3C standards HTML, for content, and CSS for presentation.

Hanging indentation: The text-indent property

p {
text-indent: 2em;
margin: 0;
}
p:first-child {
text-indent: 0;
}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ut leo enim. Curabitur eleifend interdum enim, mattis dignissim lacus placerat gravida. Donec et eros et leo gravida condimentum et non lorem.

Morbi velit quam, imperdiet a sodales in, laoreet in lectus. Vestibulum auctor massa sit amet metus ultricies nec vestibulum lacus adipiscing. Duis hendrerit malesuada diam ac porta.

Capitalization: The text-transform property

dt.charactername {
text-transform: uppercase;
font-size: .8em;
letter-spacing: .075em;
}
Thomasina:
Septimus, what is carnal embrace?
Septimus:
Carnal embrace is the practice of throwing one’s arms around a side of beef.

“Core” web fonts

Font “stacks”: similar, consistent, cross-platform

Stack: A list of similar, cross-platform, fallback typefaces in order of preference

body {
font-family: Garamond, "Hoefler Text", "Times New Roman", Times, serif;
}
h2 {
font-family: Garamond, "Hoefler Text", Palatino, "Palatino Linotype", serif;
}

CSS3 web fonts

@font-face {
font-family: 'Josefin Sans'; font-style: italic; font-weight: 100;
src: local('Josefin Sans Thin Italic'), local('JosefinSans-ThinItalic'),
    url('http://themes.googleusercontent.com/static/fonts/josefinsans/v1/s7-P1gqRNRNn-YWdOYnAOUU4uDXlcIT8_0bdRGwoYww.woff')
    format('woff');
}
h2 {
font-family: 'Josefin Sans', sans-serif;
font-weight: 100;
font-style: italic;
}

Sphinx Letterpress

CSS3 type effects

h1 { color: #484848; /* embossed look */
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.75); }
h2 { text-shadow: rgba(0, 0, 0, .5) .25em .25em .25em; /* drop shadow */
  -webkit-text-stroke: 1px red; /* outline */
  opacity: .5; /* transparency */ }

Built for the Web

Sphinx Letterpress

More precise font-weights

strong {
font-family: 'Open Sans';
font-weight: 100;
}
Open Sans 100 (*)
Open Sans 200 (*)
Open Sans 300
Open Sans 400
Open Sans 500 (*)
Open Sans 600
Open Sans 700
Open Sans 800
Open Sans 900 (*)

(*) Not available; rounded to nearest available weight

Font sizes: How ems work (or, “Em Is For Math”)

.before, .after {
margin: 0;
background-color: lightblue;
}
p {
font-size: 1em;      /* default */
margin-top: 1em;     /* default */
margin-bottom: 1em;  /* default */
}
p, h2 {
line-height: 1em;
background-color: fuchsia;
}
h2 {
font-size: 1.5em;       /* default */
margin-top: 1em;
margin-bottom: 1em;
}
before

Sphinx

after
before

Letterpress

after

How ems work (cont’d)

.before, .after {
margin: 0;
background-color: lightblue;
}
p {
font-size: 1em;      /* default */
margin-top: 1em;     /* default */
margin-bottom: 1em;  /* default */
}
p, h2 {
line-height: 1em;
background-color: fuchsia;
}
h2 {
font-size: 1.5em;       /* default */
margin-top: 0.333em;    /* = .5em/1.5em */
margin-bottom: 0.666em; /* = 1em/1.5em */
}
before

Sphinx

after
before

Letterpress

after

Base font sizes & scales

body {
font-size: 62.5%;
}
h2 {
font-size: 2em;
}

Decide on a sequence (“scale”) of sizes:

Web typography articles

2. Transitions & Transforms

CSS3 Special Effects

Upcoming CSS3 standard includes features for affine transformations and animation.

CSS3 Transforms: The transform property

selector {
-webkit-transform: function1, function2, ...;
-moz-transform: function1, function2, ...;
-ms-transform: function1, function2, ...;
-o-transform: function1, function2, ...;
transform: function1, function2, ...;
}

Translation: The translate function

selector {
-prefix-transform: translate(x-dimension, y-dimension);
}
#original, #transformed { position: absolute; left: 0; }
#transformed {
/* (browser-prefixed properties omitted) */
transform: translate(50px, 25%);
}
Original
Transformed

Scaling: The scale function

selector {
-prefix-transform: scale(x-factor, y-factor);
}
#transformed {
/* (browser-prefixed properties omitted) */
transform: scale(.5, 1.25);
}
Original
Transformed

Rotation: The rotate function

selector {
-prefix-transform: rotate(angle);
}
#transformed {
/* (browser-prefixed properties omitted) */
transform: rotate(30deg);
}
Original
Transformed

Skew/shear: The skew function

selector {
-prefix-transform: skew(x-angle, y-angle);
}
#transformed {
/* (browser-prefixed properties omitted) */
transform: skew(-45deg, 10deg);
}
Original
Transformed

Moving the origin: transform-origin

selector {
-prefix-transform-origin: x-dimension y-dimension;
}
#transformed {
/* (browser-prefixed properties omitted) */
transform-origin: left bottom;
transform: rotate(-75deg);
}
Original
Transformed
origin: left bottom
Transformed
origin: default
(center center)

3D Transforms

In recent & upcoming browsers, can specify 3-dimensional transforms:

Interactive examples:

3D Rotation: X and Y axes

selector {
-prefix-transform: rotateX(angle);
-prefix-transform: rotateY(angle);
}
#transformedX {
/* (browser-prefixed properties omitted) */
transform: rotateX(30deg);
}
#transformedY {
/* (browser-prefixed properties omitted) */
transform: rotateY(-45deg);
}
Original
Transformed X
(X-axis)
Transformed Y
(Y-axis)

3D Translation & Scale

selector {
-prefix-transform: translateZ(dimension);
-prefix-transform: scaleZ(factor);
}
#translatedZ {
/* (browser-prefixed properties omitted) */
transform: translateZ(-5em);
}
#scaledZ {
/* (browser-prefixed properties omitted) */
transform: rotateY(-45deg)
          scaleZ(.1) translateZ(100em);
}
Original
Translated Z
Scaled Z

3D Perspective Distortion

selector {
-prefix-perspective: pixels;
}
/* (browser-prefixed properties omitted) */
#container {
border: solid 2px blue;
background-color: gray;
perspective: ;
}
#transformed {
transform: rotateY(-45deg) translateZ(-5em);
}
Original
Transformed

CSS3 Transitions: the transition property

selector {
-prefix-transition: property duration timing function delay;
}
/* (browser-prefixed properties omitted) */
#transformed {
transform: rotate(-45deg);
transition: transform 2s ease-in-out,
           background-color .5s ease 2s;
}
#transformed:hover {
font-size: 200%;
background-color: blue;
transform: rotate(10deg) scale(1.5);
}
Original
Transformed
(hover over me!)

Transition Timing Functions

The timing function specifies how the transition will speed up and slow down across its lifecycle.

timing function description
ease (default) quick start, gradual finish
ease-in slow start, steady build to max speed
ease-out start at max speed, steady slow to finish
ease-in-out slow start, increase to max speed, slow to finish
linear constant speed for entire duration
cubic-bezier(n, n, n, n) specify your own custom Bézier curve function

Where to specify transitions?

Click me!
#foo {
width: 5em; height: 5em;
transition: width 1s ease, height 1s ease,
            font-size 1s ease;
}
.enlarged {
width: 7.5em; height: 7.5em;
font-size: 1.5em;
}
$(document).ready(function() {
$('#foo').click(function() {
$(this).addClass('enlarged');
});
});

Other transition options

I remain visible from behind
I’m rotated 60° from this element — change transform-style
I disappear past 90° (back not visible)
#foo, #bar {
transition: transform 1s ease;
transform-style: ;
}
#foo:hover, #bar:hover {
transform: rotateY(-135deg);
}
#bar {
backface-visibility: hidden;
}

Proper browser-prefixing

#foo {
-webkit-transition: -webkit-transform 1s ease;
-moz-transition: -moz-transform 1s ease;
-ms-transition: -ms-transform 1s ease;
-o-transition: -o-transform 1s ease;
transition: transform 1s ease;
}

#foo.altered {
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
-ms-transform: rotate(-45deg);
-o-transform: rotate(-45deg);
transform: rotate(-45deg);
}
$('#foo').parent().css({
'-webkit-perspective': '600px',
'-moz-perspective': '600px',
'-ms-perspective': '600px',
'-o-perspective': '600px',
'perspective': '600px'
});

3. Responsive Web Design (RWD)

Mobile device web traffic

Mobile devide web traffic

Mobifying layouts: “Responsive” web design

Layouts that adjust themselves (“respond”) to optimize for screen size

Boston Globe responsive website

Fluid columns example

/* Three-column fluid layout with gutters */
/* 32% + 2% + 32% + 2% + 32% = 100% */
.col { float: left; width: 32%; margin-right: 2% }
/* remove extra 2% at right of every 3rd column */
.col:nth-child(3n) { margin-right: 0; }
  • Small batch bushwick scenester, banksy salvia mlkshk williamsburg post-ironic.
  • Freegan gentrify aesthetic jean shorts raw denim pour-over +1
  • Wes anderson ethnic godard brooklyn mixtape carles cred.
  • Retro twee fap, cardigan etsy pitchfork farm-to-table next level vice.
  • Brunch pickled bespoke biodiesel, lomo swag street art four loko jean shorts forage.
  • Umami fixie shoreditch, single-origin coffee cred quinoa ethnic brunch lomo aesthetic

Adapting with CSS3 Media Queries

<head>
...
<link rel="stylesheet" href="default.css" type="text/css" media="screen">
<link rel="stylesheet" href="mobile.css" type="text/css" media="screen and (max-width: 800px)">
<link rel="stylesheet" href="large.css" type="text/css" media="screen and (min-width: 1200px)">
...
</head>
/* default.css - 3-column */
.col { float: left; width: 32%; margin: 0 2% 1em 0; }
.col:nth-child(3n) { margin-right: 0 }
/* mobile.css - switch to 2-column */
.col { width: 48%; margin-right: 4%; } /* 48% + 4% + 48% = 100% */
.col:nth-child(3n) { margin-right: 4% } /* "undo" the 3n rule from default.css */
.col:nth-child(2n) { margin-right: 0 }
  • Small batch bushwick scenester, banksy salvia mlkshk williamsburg post-ironic.
  • Freegan gentrify aesthetic jean shorts raw denim pour-over +1
  • Wes anderson ethnic godard brooklyn mixtape carles cred.
  • Retro twee fap, cardigan etsy pitchfork farm-to-table next level vice.
  • Brunch pickled bespoke biodiesel, lomo swag street art four loko jean shorts forage.
  • Umami fixie shoreditch, single-origin coffee cred quinoa ethnic brunch lomo aesthetic

Responsive Techniques

Use case: Dropdown navigation

<nav>
<ul>
   <li><a href="/">Home</a></li>
   <li><a href="/blog/">Blog</a></li>
   <li><a href="/about/">About</a></li>
</ul>
<select>
   <option value="/">Home</option>
   <option value="/blog/">Blog</option>
   <option value="/about/">About</option>
</select>
</nav>
/* default.css */
nav ul     { display: block; }
nav select { display: none; }
/* mobile.css */
nav ul     { display: none; }
nav select { display: block; }
// scripts.js
// handle dropdown nav using JS
$('nav select').change(function() {
window.location = this.value;
});