variables
Even something as simple as the ability to set variables allows for a number of possibilities in CSS:
// file -> my-first-variable.scss
$color-red: #993434; // hex color is a valid data type
$color-blue: #307aea;
$cdn-prefix: '/dr/ncaa/ncaa7/sites/default/files'; // so is string
body {
// interpolation syntax -
background-image: url('#{$cdn-prefix}/my-image.png');
background-color: $color-red;
color: $color-blue;
}
// compiles to -> my-first-variable.css
body {
background-image: url('/dr/ncaa/ncaa7/sites/default/files/my-image.png');
background-color: #993434;
color: #307aea; }
Bonus: SASS also provides the classic "// comment" style which I've used above. This style will be stripped from the compiled *.css. Regular "/* comment */" CSS comments will be left in place.
File includes (PARTIALS)
SASS provides the ability to include files much like other scripting languages. This is mostly helpful for organizing and re-using SASS. For example, you could:
- Split large stylesheets into smaller, possibly re-usable components (called partials in SASS) that are never directly compiled by themselves.
- Have a common variables partial that gets imported into other stylesheets as needed
A gotcha here is that the keyword for including a file is actually "@import" Semantically, this makes sense, but is extra confusing coming from PHP due to the existence of another keyword "@include" which is how mixins get used or "called" in SASS. More on those later.
Nesting
- Nesting is another feature of SASS that finally allows for organization of related style rules.
- It is important to understand the CSS selectors produced when using nesting
-
Recommended not to nest more than 3 levels deep although there is no technical limit
// file -> linescore.scss
.linescore {
background-color: purple;
h2 {
color: pink;
}
a {
text-decoration: none;
&:hover {
text-decoration: underline
}
}
}
// compiles to -> linescore.css
.linescore { background-color: purple; }
.linescore h2 { color: pink; }
.linescore a { text-decoration: none; }
.linescore a:hover { text-decoration: underline; }
MIXINS - DEFINING
Mixins are meant to be small, re-usable chunks of style rules. (But they don't have to be small.) They may also accept arguments so you can parameterize them as needed:
@mixin ncaa-nowrap($overflow: 'ellipsis') {
white-space: nowrap;
overflow: hidden;
text-overflow: $overflow;
}
Here we've defined a mixin to prevent copy from wrapping to 2-lines - a common scenario when working with an editorial staff.
The $overflow parameter allows the caller to specify how they want text overflow to be treated. By default, "ellipsis" value will be used (note a default value is not required on SASS mixins).
MIXINS - USING
Mixins are usable basically wherever you want. In fact, you can nest mixins into mixins if you're into that sort of thing. Let's use the mixin we crafted to control text wrapping within a <p>:
// file -> article.scss
// To use a mixin in this file, you must either @import another
// *.scss file which defines it OR define it here in this file.
// Just pretend the mixin resides in 'partials/_toolkit.scss'
@import 'partials/toolkit'
p.flow {
@include ncaa-nowrap();
&.clip { @include ncaa-nowrap('clip'); }
}
// compiles to -> article.css
p.flow {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis; }
p.flow.clip {
white-space: nowrap;
overflow: hidden;
text-overflow: clip; }
FUNCTIONS
- Functions in SASS are basically what you'd expect.
- They might seem kinda similar to mixins (which are more like macros) at first.
- The difference is that mixins provide one or more style rules and their values.
- Functions are used to compute just a value to be used in a single style rule.
- You can use functions in mixins (but probably not vice-versa AFAIK).
Here is a quick example that uses some of SASS' built-in color manipulation functions:
$color-heading: #4c821c;
.main-headline {
color: lighten($color-heading, 15%);
.sub {
color: darken($color-heading, 15%);
}
}
(The variable "$color-heading" will be automatically lightened or darkened by 15% from (from #4c821c -> #65ac25 or #335813, respectively) in the compiled output. This is very useful for setting hover-state colors, for example.)
COMPASS: FEATURES OVERVIEW
MIXINS
At its core, Compass is mostly just a collection of mixins. We primarily use their CSS3 mixin collection to support legacy browsers without having to manually type out all the vendor-specific rules all the time etc.
FUNCTIONS
In addition to mixins, Compass provides a number of useful functions that complement what SASS comes with. Compass generally refers to these as "helpers" within their documentation.
- sqrt()
- pi()
- sin()
- cos()
- tan()
- more advanced color manipulation funcs like shade() and tint()
- ... and more that struck me as too esoteric but I guess someone must use them
RESET
This is pretty self-explanatory. Like most CSS authoring frameworks, Compass includes a nice reset stylesheet. Paired with SASS file include (@import) ability, it is easy to use wherever you need it.
For example, we @import Compass' reset file into our theme's base stylesheet so that the rules apply to every page on the site.
AUTOMATED SPRITING
-
Compass includes several utilities that are used outside of the *.scss file
- By far the most useful/interesting is "automated spriting"
- No more creating (or worse: updating) sprite maps by hand!
- Never calculate another "background-position" [x,y] coord or sprite cut size
- Simply:
- Drop individual cuts into a folder within your module.
- Use the Compass sprite utility to automatically generate the sprite map image + a partial with mixins to provide CSS rules.
- Makes maintaining and altering existing sprite maps infinitely quicker.
- If there is time: live_data (REAL WORLD PROD example)