CSS3 and Sass: Mark Volkmann Object Computing, Inc. Strange Loop 2011
CSS3 and Sass: Mark Volkmann Object Computing, Inc. Strange Loop 2011
Mark Volkmann
Object Computing, Inc.
mark@ociweb.com
Strange Loop 2011
CSS
Separates formatting from content
Can specify formatting for
all elements with a given name
all elements with a given class
the element with a given id
elements with certain relationships
W3C documents
CSS1 Recommendation (1996)
CSS2 Recommendation (1998)
CSS3 - defined by many documents at various stages of approval; many are Working Drafts
2) style element
inside head element in HTML
discouraged
3) link element
inside head element in HTML
recommended
Example
#elephant, .title {
color: purple;
font-size: 24pt;
}
Comments
/* comment-text */
By class name
matches elements that have specified value for their class attribute
for example, <div class="title"> applies to all div tags that have a class of “title”
By id
matches the element that has specified value for its id attribute
values of id attributes must be
for example, <div id="education"> unique within the document
regardless of element name
id preceded by a pound sign - for example, #date, #tax
can also specify element name - for example, div#date
By context - children
a > b selects elements named “b” that are direct children of an element named “a”
for example, div.fy2010 > table.taxes > td td not inside a thead, tfoot or tbody
content border
.title {
color: purple;
font-size: 24pt;
}
.whippet {
background-color: yellow;
color: blue;
}
.whippet span {
vertical-align: top;
}
#first {
font-style: italic;
}
#second img {
border: solid red 5px;
}
a pseudo-class
button:hover {
color: red;
}
Current browsers
don’t support some CSS3 features
treat others as experimental
IE support is very limited
Firefox, Chrome and Safari support is better
Proposed Recommendations
CSS Namespaces - 11 August 2011
Candidate Recommendations
CSS Multi-column Layout - 12 April 2011
CSS Print Profile - 13 October 2006 CSS Transitions Level 3 - 01 December 2009
CSS3 Paged Media - 10 October 2006 CSS 3D Transforms Level 3 - 20 March 2009
CSS Text Level 3 - 1 September 2011 CSS Animations Level 3 - 20 March 2009
CSS Writing Modes Level 3 - 1 September 2011 Behavioral Extensions to CSS - 19 October 2007
CSS Conditional Rules Level 3 - 1 September 2011 CSS Grid Positioning Level 3 - 5 September 2007
CSSOM View - 4 August 2011 CSS Basic Box Model - 9 August 2007
CSS Image Values and Replaced Content Level 3 - 12 July 2011 CSS3 Values and Units - 19 September 2006
CSSOM (CSS Object Model) - 12 July 2011 CSS3 Cascading and Inheritance - 15 December 2005
CSS Regions - 09 June 2011 The CSS 'Reader' Media Type - 24 February 2004
CSS Lists and Counters Level 3 - 24 May 2011 CSS3 Presentation Levels - 13 August 2003
Flexible Box Layout - 22 March 2011 CSS3 Generated and Replaced Content - 14 May 2003
CSS Generated Content for Paged Media - 08 June 2010 CSS3 Border - 7 November 2002
red yellow
“allows property changes in CSS values to occur smoothly over a specified duration”
rather than immediately and all at once
“extends CSS Transforms to allow elements rendered by CSS to be transformed in three-dimensional space”
Main features
variables - avoids repeating property values; supports math
nesting - avoids repeating selectors
mixins - reuses lines of CSS; optionally parameterized
selector inheritance - a selector can inherit styles from another
Created by
Hampton Catlin, Nathan Weizenbaum, Chris Eppstein and others
http://sass-lang.com
see http://www.ruby-lang.org/en/downloads/
need version 1.8.7 or above
Output to stdout
sass {name}.scss by default, creates a
.sass-cache directory
useful for testing that stores compiled templates
to speed up subsequent runs
Output to a CSS file
sass {name}.scss:{name}.css
SCSS syntax
“Sassy CSS”
extension of CSS3 syntax
all CSS3 syntax is valid SCSS syntax
preferred
--style={value}
-t={value}
Nesting
Useful when multiple selectors specify the same ancestor(s)
avoids repeating ancestors in each selector #foo { SCSS
#foo td { CSS td {
border: solid red 2px; border: solid red 2px;
} }
#foo li { li {
font-style: italic; font-style: italic;
} }
}
<html> variables.html
<head>
<link rel="stylesheet" type="text/css" href="variables.css"/>
</head>
<body>
<h3>Sass Variables</h3>
<div id="d1">One</div>
<div id="d2">Two</div>
<div id="d3">Three</div>
</body>
</html>
Syntax is #{expression}
Example
$planet: Mars; SCSS .Mars4 { generated CSS
$number: 3; border-top-width: 6pt;
$side: top; }
.#{$planet}#{$number + 1} {
border-#{$side}-width: #{$number * 2}pt;
}
Number
can have CSS units like pt, px and in - ex. 24pt
arithmetic operations perform automatic conversions 2in - 4mm = 1.843in
String
in double-quotes, single-quotes or unquoted; use + to concatenate
Color
name, hex value or call to one of many functions that return a color
List
values separated by spaces or commas
some properties take a list value
examples include margin, padding and border-color
(order is top, right, bottom, left which is clockwise from 12 o’clock)
border-color: red orange yellow green;
Math
abs, ceil, floor, round, percentage (simply multiplies by 100)
Lists
join, length, nth
Introspection
comparable (takes two numbers with optional units and returns true if they can be compared, added and subtracted)
type-of, unit, unitless for example, an absolute length cannot
be compared to a relative one;
Other comparable(50px, 5ex) returns false
HSL
hsl, hsla, hue, saturation, lightness
adjust-hue, complement, grayscale, invert, lighten, darken
saturate, desaturate
Opacity
alpha (or opacity), rgba, opacify (or fade-in), transparentize (or fade-out)
Other color-related
changes one or more properties of a color;
adjust-color, scale-color, change-color see example on next slide
#d1 {
font: 24pt bold monospace;
color: change-color($color, $blue: 0, $green: 256);
}
$color is a positional argument;
$blue and $green are a keyword arguments;
results in yellow
“Partials”
Sass files only meant to be imported into others
starting filename with an underscore tells Sass
not to generate a .css files from it
@import "file-name";
don’t need to specify file extension or leading underscore for partials
looks in current directory by default
can specify other locations with --load-path command-line option
can import more than one file
with multiple @import statements or
listing multiple filenames separated by commas
@mixin
@mixin transition($property, $duration, $function: ease-in-out) {
defines content -moz-transition: $property, $duration, $function;
default
can @include -ms-transition: $property, $duration, $function;
value
other mixins -o-transition: $property, $duration, $function;
-webkit-transition: $property, $duration, $function;
@include
transition: $property, $duration, $function;
includes content }
can use positional
and/or div { This example demonstrates
keyword arguments border: solid red 2px; making it easier to use
CSS3 experimental properties
width: 600px;
that differ across current browsers.
@include transition(all, 3s);
}
Iteration
@for ${var} from {start} [to | through] {end} { ... }
end is exclusive with to and inclusive with through
$season: summer;
#d1 {
background-color: season-color($season);
height: 100px;
width: 100px;
}
ex. CSS3 experimental properties that support border radii, transitions and transforms