First, navigate via the command line to your /class2-exercises/practice/stylesheets directory.
Then type:
$ sass --watch scss:css
Create an extend in _utilities:
%yourExtendName {
property: value;
}
// example usage
p {
@extend %yourExtendName;
}
Create these mixins in _utilities:
@mixin yourMixinName($optionalArgument) {
// hard-code values
property: value;
// or use arguments
property: $optionalArgument;
}
p {
// example usage
@include yourMixinName($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:
Mixins are very useful for common design patterns. A great example is the CSS sprite design pattern.
Here's YouTube's icon sprite:
Sprite image: |
CSS for icon:
|
If you set up your Photoshop sprite to use grid lines, you can use Sass to easily position your sprite image without lots of trial and error.
@mixin sprite-position($x:0, $y:0) {
$gridSize: -32px;
$offsetX: $x * $gridSize;
$offsetY: $y * $gridSize;
background-position: $offsetX $offsetY;
}
@mixin sprite-image($file, $x:0, $y:0) {
background-image: url("../../images/icons/#{$file}");
@include sprite-position($x, $y);
}
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:
|
CSS output:
|
@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: