sass-site/source/index.html.haml

302 lines
12 KiB
Plaintext
Raw Normal View History

---
title: Sass | Syntactically Awesome Style Sheets
---
%h1 CSS with super powers
%p Sass makes writing CSS fun again. Think of Sass as an extension of CSS3 that adds a bunch of missing features like nesting, variables, mixins & extend.
2012-11-28 17:23:08 +01:00
%h2 There are a couple of ways to start using Sass:
2012-11-28 17:23:08 +01:00
%ol.quick-starts
2012-11-29 06:00:50 +01:00
%li.command-line
2012-11-29 18:33:08 +01:00
.quick-start-content
%h3 Command Line
%p.illustration
%span.circle= image_tag "illustrations/command-line.png", :width => "48", :height => "48", :alt => "CLI Illustration"
/ Description for Unix users
%p.description Using Sass on the command line is as simple as using a few commands. To install Sass just
type the following into the command line:
2012-11-29 18:33:08 +01:00
.call-to-action
/ We'll try and point the user to either
%p= link_to "Command Line Quickstart", '/quickstart/command-line#unix', :class => 'button'
2012-11-29 18:33:08 +01:00
%pre
%code
gem install sass
%p For Windows users, go check out the <a href="/quickstart/command-line#windows">Windows quickstart guide</a>.
2012-11-29 06:00:50 +01:00
%li.gui-application
2012-11-29 18:33:08 +01:00
.quick-start-content
%h3 GUI Application
%p.illustration
%span.circle= image_tag "illustrations/gui.png", :width => "48", :height => "48", :alt => "GUI Quickstart"
%p.description Get started quickly by installing a Sass for Linux, Mac or Windows.
.call-to-action
%p= link_to "GUI Quickstart", '/quickstart/gui', :class => 'button'
%p= link_to "Download Compass.app", '#', :class => 'button'
2012-11-28 12:59:42 +01:00
2013-02-09 04:10:33 +01:00
- content_for :section_2 do
.message.current-release
2012-11-28 17:23:08 +01:00
.container
2013-02-09 04:10:33 +01:00
%dl
%dt Current Release:
%dd Media Mark (3.2.1)
2012-11-28 17:23:08 +01:00
%ul
%li= link_to "Release Notes", "#"
%li= link_to "Fork Sass on Github", "#"
.container
2013-02-09 04:10:33 +01:00
.content
%h1 Sass Features
2012-11-28 17:23:08 +01:00
2013-02-09 04:10:33 +01:00
%nav.slide-navigation
2012-11-28 17:23:08 +01:00
%ul
2012-11-29 18:33:08 +01:00
%li= link_to "Preprocessing", '#1'
%li= link_to "Variables", '#2'
%li= link_to "Nesting", '#3'
%li= link_to "Mixins", '#4'
%li= link_to "Inheritance", '#5'
%li= link_to "Import", '#6'
%li= link_to "Extend", '#7'
%li= link_to "Operators", '#8'
2013-02-09 04:10:33 +01:00
%ul.slides
%li
%h2 Pre-processing
%p CSS on its own can be pretty darn nice, but stylesheets are getting larger, more complex, and harder to maintain. This is where a pre-processor can help. Sass lets you use features that don&rsquo;t exist in CSS yet like variables, nesting, mixins, inheritance and other nifty goodies that make writing CSS fun again.
%p Once you start tinkering with Sass, it will take your pre-processes Sass file and save it out as a normal CSS file that you can use in your web site.
2012-11-28 17:23:08 +01:00
2013-02-09 04:10:33 +01:00
%li
%h2 Variables
%p Think of variables as a way to store information that you want to reuse throughout your stylesheet. You can store things like colors, font stacks, or any CSS value you think you'll want to reuse. Sass uses the <code>$</code> symbol to make something a variable. Here's an example:
%pre.scss
2012-11-28 17:23:08 +01:00
%code
:preserve
$font-stack: Helvetica, sans-serif;
$primary-color: #333;
body {
color: $primary-color;
font: 100% $font-stack;
}
%p When the Sass is processed, it takes the variables we defines for the <code>$font-stack</code> and <code>$primary-color</code> and outputs normal CSS with our variable values placed in the CSS. This can be extremely powerful when working with brand colors and keeping them consistent throughout the site.
%pre.css
%code
:preserve
body {
color: #ccc;
font: 100% Helvetica, sans-serif
}
2012-11-28 17:23:08 +01:00
2013-02-09 04:10:33 +01:00
%li
%h2 Nesting
%p When you write HTML you've probably noticed that it has a fairly clear nested, visual hierarchy. CSS, on the other hand, isn't. Sass will let you nest your CSS selectors in a way that follows the same visual hierarchy of your HTML. Here's an example of some typical styles for a sites navigation:
%pre.scss
%code
:preserve
nav {
ul {
list-style: none;
margin: 0;
padding: 0;
}
li {
display: inline-block;
}
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}
%p You'll notice that the <code>ul</code>, <code>li</code>, and <code>a</code> selectors are nested inside the <code>nav</code> selector. This is a great way to organize your CSS and make it more readable. When you generate the CSS you'll get something like this:
%pre.sass
2012-11-28 17:23:08 +01:00
%code
:preserve
nav ul {
list-style: none;
margin: 0;
padding: 0;
}
nav li {
display: inline-block;
}
nav a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
%li
%h2 Partials
%p You can create partial Sass files that contain little snippets of CSS that you can include in other Sass files. This is a great way to modularize your CSS & help keep things easier to maintain. A partial is simply a Sass file named with a leading underscore. You might name it something like <code>_partial.scss</code>. The underscore lets Sass know that the file is only a partial file and that it should be generated into a CSS file. Sass partials are used with the <code>@import</code> directive.
2012-11-28 17:23:08 +01:00
2013-02-09 04:10:33 +01:00
%li
%h2 Import
%p CSS has an import option that lets you split your CSS in to smaller, more maintainable chunks. The only drawback is that each time you use <code>@import</code> in CSS it creates another HTTP request. Sass builds on top of the current CSS <code>@import</code> but instead of requiring an HTTP request, Sass will take the file that you want to import and combine it with the file your importing into so you can server a single CSS file to the web browser.
%p Lets say you have a couple of Sass files, <code>_reset.scss</code> and <code>base.scss</code>. We want to import <code>_reset.scss</code> into <code>base.scss</code>.
%pre.scss
2012-11-28 17:23:08 +01:00
%code
:preserve
/* _reset.scss */
2012-11-28 17:23:08 +01:00
html, body, ul, ol {
margin: 0;
padding: 0;
}
%pre.scss
2012-11-28 17:23:08 +01:00
%code
:preserve
/* base.scss */
@import 'reset';
body {
background-color: #efefef;
font-size: 100% Helvetica, sans-serif;
}
%p Notice we're using <code>@import 'reset';</code> in the <code>base.scss</code> file. When you import a file you don't need to include the file extension <code>.scss</code> Sass is pretty smart and will figure it out for you. When you generate the CSS you'll get:
%pre.css
%code
:preserve
html, body, ul, ol {
margin: 0;
padding: 0;
}
body {
background-color: #efefef;
font-size: 100% Helvetica, sans-serif;
}
2012-11-28 17:23:08 +01:00
2013-02-09 04:10:33 +01:00
%li
%h2 Mixins
%p Some things in CSS are a bit tedious to write, especially with CSS3 and the many vendor prefixes that exist. A mixin lets you take chunks of CSS declarations that you want to reuse throughout your site. You can even pass in values to make your mixin more flexible. A good use of a mixin is for vendor prefixes. Here's an example for <code>border-radius</code>.
%pre.scss
2012-11-28 17:23:08 +01:00
%code
:preserve
@mixin border-radius($radius) {
-webkit-border-radius: $radius;
-moz-border-radius: $radius;
-ms-border-radius: $radius;
-o-border-radius: $radius;
border-radius: $radius;
}
.box {
@include border-radius(10px);
}
2012-11-28 17:23:08 +01:00
%p To create a mixin you use the <code>@mixin</code> directive and giving it a name. We've named our mixin <code>border-radius</code>. We're also using the variable <code>$radius</code> inside the parenthesis so we can pass in a radius of whatever we want. After you create your mixin, you can then use it as a CSS declaration starting with <code>@include</code> followed by the name of the mixin. When your CSS is generated it'll look like this:
%pre.css
2012-11-28 17:23:08 +01:00
%code
:preserve
.box {
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
-ms-border-radius: 10px;
-o-border-radius: 10px;
border-radius: 10px;
}
2012-11-28 17:23:08 +01:00
%li
%h2 Extend/Inheritance
%p This is one of the most useful features of Sass. Using <code>@extend</code> lets you share a set of CSS properties from one selector to another. It helps keep your Sass very Dry. In our example we're going to create a simple series of messaging for errors, warnings and successes.
%pre.scss
%code
:preserve
.message {
border-color: 1px solid #ccc;
color: #333;
padding: 10px;
}
.success {
@extend .message;
border-color: green;
}
.error {
@extend .message;
border-color: red;
}
.warning {
@extend .message;
border-color: yellow;
}
%p What the above code does is allow you to take the CSS properties in <code>.message</code> and apply them to <code>.success</code>, <code>.error</code>, & <code>.warning</code>. The magic happens with the generated CSS. This is what it looks like:
%pre.css
%code
:preserve
.message, .success, .error, .warning {
border-color: 1px solid #cccccc;
color: #333;
padding: 10px;
}
.success {
border-color: green;
}
.error {
border-color: red;
}
.warning {
border-color: yellow;
}
2013-02-09 04:10:33 +01:00
%li
%h2 Operators
%p Doing math in your CSS can be pretty darn helpful. Sass has a handful of standard math operators like +, -, *, /, and %. In our example we're going to do some simple match to calculate widths for an aside & article.
%pre
%code
:preserve
#container {
width: 100%;
}
article[role="main"] {
float: left;
width: 600px / 960px * 100%;
}
aside[role="complimentary"] {
float: right;
width: 300px / 960px * 100%;
}
%p We've created a very simple fluid grid, based on 960px. Operations in Sass let us do something like take pixel values and convert them to percentages without a lot of hassle. The generated CSS will look like:
2012-11-28 17:23:08 +01:00
%pre
%code
:preserve
#container {
width: 100%;
}
article[role="main"] {
float: left;
width: 62.5%;
}
aside[role="complimentary"] {
float: right;
width: 31.25%;
}
2012-11-29 18:33:08 +01:00
2013-02-09 04:10:33 +01:00
%ul.pagination
2012-11-29 18:33:08 +01:00
%li= link_to "Prev", "#", :class => "prev"
2013-02-09 04:10:33 +01:00
%li= link_to "Next", "#", :class => "next"