If you haven't already, create these mixins under Utilities:
@mixin example($optionalArgument) {
property: value;
property: $optionalArgument;
}
p {
@include example($optionalArgument);
}
@mixin gradient($color1, $color2) {
background-image: -webkit-linear-gradient($color1, $color2, $color1);
background-image: -moz-linear-gradient($color1, $color2, $color1);
background-image: linear-gradient($color1, $color2, $color1);
}
@mixin gradient($color1: #fff, $color2: #666) {
background-image: -webkit-linear-gradient($color1, $color2, $color1);
background-image: -moz-linear-gradient($color1, $color2, $color1);
background-image: linear-gradient($color1, $color2, $color1);
}
@mixin rotate($degree, $position) {
-webkit-transform: rotate(-5deg);
-moz-transform: rotate(-5deg);
transform: rotate(-5deg);
-webkit-transform-origin: $position;
-moz-transform-origin: $position;
transform-origin: $position;
}
@mixin rotate($degree, $position) {
-webkit-transform: rotate(#{$degree}deg);
-moz-transform: rotate(#{$degree}deg);
transform: rotate(#{$degree}deg);
-webkit-transform-origin: $position;
-moz-transform-origin: $position;
transform-origin: $position;
}
.headline {
font-size: 2em;
font-weight: bold;
}
.lead-story-headline {
@extend .headline;
text-decoration: underline;
text-transform: uppercase;
}
@mixin rotate($degree, $position) {
-webkit-transform: rotate(#{$degree}deg);
-moz-transform: rotate(#{$degree}deg);
transform: rotate(#{$degree}deg);
-webkit-transform-origin: $position;
-moz-transform-origin: $position;
transform-origin: $position;
}
@mixin gradient($color1: $bodyBackground, $color2: $accentBackground) {
background-image: -webkit-linear-gradient($color1, $color2, $color1);
background-image: -moz-linear-gradient($color1, $color2, $color1);
background-image: linear-gradient($color1, $color2, $color1);
}
@include gradient(#fff, #000);
Other CSS3 features that require browser prefixes to work in most popular browsers include:
Other common UI elements that Sass mixins make easy:
Stand up and stretch - we'll resume in 5 minutes
Sass has logic statements that you can use to create conditionals. They are @if, @else if, and @else
@mixin opacity($value: 0.5) {
@if $value == transparent {
opacity: 0;
} @else if $value == opaque {
opacity: 1;
} @else {
opacity: $value;
}
}
@include opacity(transparent);
Rewrite your existing mixins to use if/else if statements, so that they output different CSS depending on different arguments.
@mixin arrow($direction: right) {
@if $direction == right {
//right arrow styles
}
@else if $direction == left {
// left arrow styles
}
}
With @for loops, you can make Sass write your classes and styles for you.
Sass code:
|
CSS output:
|
@for $i from 1 through 3 {
.column-#{$i} { width: 2em * $i; }
}
With @each, you can loop through a list of items and create styles for each item in the list.
Sass code:
@each $icon in youtube, twitter, facebook {
.icon-$icon {
background-image: url('#{$icon}.png');
}
}
CSS output:
.icon-youtube {
background: url('youtube.png');
}
.icon-twitter {
background: url('twitter.png');
}
.icon-facebook {
background: url('facebook.png');
}
@each $woman in ada, grace, frances, barbara, anita, maria {
.#{$woman}-bg {
background-image: url('images/#{$woman}.png');
}
}
The following tools can help you write Sass even faster:
How Sass compiles into CSS
$ cd ../
Go to your home directory:
$ cd /
Go to a specific folder:
$ cd Users/cfarman/Sites/gdi-sass
List files in a directory:
$ ls
ruby -v
Now that Ruby is installed, we can install the Sass gem.
In Terminal or Git Bash:
gem install sass
You should have installed Sass on your computer prior to this workshop.
Check if Sass is installed:
gem list sass
You should see something like this:
# *** LOCAL GEMS ***
# sass (3.4.23)
Raise your hand if Sass is not installed.
We need to structure our stylesheets before we can compile them.
Check out your index.html file in a browser. Looks funky, yes?
We need to compile our .scss (Sass) files to make the CSS work in the browser.
First, navigate via the command line to your /stylesheets directory in the "practice" folder.
Then type:
$ sass --watch scss:css
Let's break this command down - you're going to use it *a lot*!
$ sass --watch scss:css
--watch tells Sass to look for changes to our .scss files, and to compile them to css if they have updates.
The scss bit is the folder where our .scss files live. We edit these files only.
The css part is the folder where our .css files will be. These files are the compiled output of our Sass, and are only created when we run the above command.
Edit some Sass in the .scss files and see your changes automatically compiled to CSS!