@mixin gradient($color1, $color2) {
background-image: linear-gradient($color1, $color2, $color1);
}
@mixin gradient($color1: #fff, $color2: #666) {
background-image: linear-gradient($color1, $color2, $color1);
}
@mixin rotate($degree, $position) {
transform: rotate(#{$degree}deg);
transform-origin: $position;
}
$type: 'large';
.button--#{$type} {
height: 100px;
}
$sidebar-width: 250px;
.main-content {
width: calc(100% - #{$sidebar-width});
}
%headline {
font-size: 2em;
font-weight: bold;
}
.lead-story-headline {
@extend %headline;
text-decoration: underline;
text-transform: uppercase;
}
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);
}
@media blocks do not need to be declared at the root level of the stylesheet, which helps keep them in sync with the elements they're modifying.
body {
article {
p {
font-size: 100%;
color: black;
padding: 10px;
@media (max-width: 768px) {
font-size: 150%; // use larger text for smaller screens
}
}
}
}
Compiled CSS:
body article p {
font-size: 100%;
color: black;
padding: 10px;
}
@media (max-width: 768px) {
body article p {
font-size: 150%;
}
}
Stand up and stretch - we'll resume in 5 minutes
@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);
Sass code:
@for $i from 1 through 3 {
.column-#{$i} { width: 2em * $i; }
}
CSS output:
.column-1 {
width: 2em;
}
.column-2 {
width: 4em;
}
.column-3 {
width: 6em;
}
@for $i from 1 through 3 {
.column-#{$i} { width: 2em * $i; }
}
Loops through a list of items and creates 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');
}
}
$elements: project-covers, user-tooltip, sorting-bar, modals, navigation;
.user-tooltip {
z-index: index($elements, user-tooltip);
}
This is an even more advanced example combining a custom function, lists, variables, and error handling.
@function z($list, $element) {
$z-index: index($list, $element);
@if $z-index {
@return $z-index;
}
@warn 'There is no item "#{$element}" in this list; choose one of: #{$list}';
@return null;
}