Sass, un preprocesador CSS

Adolfo Sanz De Diego

Septiembre 2016

1 El autor

1.1 Adolfo Sanz De Diego

1.2 Algunos proyectos

1.3 ¿Donde encontrarme?

2 Introducción

2.1 ¿Qué es?

2.2 Ventajas

3 Instalación

3.1 Instalar Ruby

sudo apt-get install ruby-full

3.2 Instalar Sass

sudo gem install sass

3.3 Preprocesar

sass --watch input.scss:output.css
sass --watch input/dir:output/dir

3.4 Probar sin instalar

4 Características

4.1 Variables

$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}
body {
  font: 100% Helvetica, sans-serif;
  color: #333;
}

4.2 Valores por defecto

$content: "First content";
$content: "Second content?" !default;
$new_content: "First time reference" !default;

#main {
  content: $content;
  new-content: $new_content;
}

4.3 Valores por defecto

#main {
  content: "First content";
  new-content: "First time reference";
}

4.4 Scope

$var: red;

#page {
  $var: white;
  #header {
    color: $var; // white
  }
}

4.5 Reglas anidadas

nav {
  ul {
    margin: 0;
    padding: 0;
    list-style: none;
  }

  li { display: inline-block; }
}
nav ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

nav li {
  display: inline-block;
}

4.6 Parciales

_partial.scss

4.7 Imports

html, body, ul {
  margin: 0;
  padding: 0;
}
@import 'reset';

body {
  font: 100% Helvetica, sans-serif;
  background-color: #efefef;
}
html, body, ul, ol {
  margin: 0;
  padding: 0;
}

body {
  font: 100% Helvetica, sans-serif;
  background-color: #efefef;
}

4.8 Parent

a {
  font-weight: bold;
  text-decoration: none;
  &:hover { text-decoration: underline; }
  body.firefox & { font-weight: normal; }
}
a {
  font-weight: bold;
  text-decoration: none;
}
a:hover { text-decoration: underline; }
body.firefox a { font-weight: normal; }

4.9 Mixins

@mixin border-radius($radius) {
  -webkit-border-radius: $radius;
     -moz-border-radius: $radius;
      -ms-border-radius: $radius;
          border-radius: $radius;
}

.box {
  @include border-radius(10px);
}
.box {
  -webkit-border-radius: 10px;
  -moz-border-radius: 10px;
  -ms-border-radius: 10px;
  border-radius: 10px;
}

4.10 Argumentos variables

@mixin box-shadow($shadows...) {
  -moz-box-shadow: $shadows;
  -webkit-box-shadow: $shadows;
  box-shadow: $shadows;
}

.shadows {
  @include box-shadow(0px 4px 5px #666, 2px 6px 10px #999);
}
.shadows {
  -moz-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  -webkit-box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
  box-shadow: 0px 4px 5px #666, 2px 6px 10px #999;
}

4.11 Pasar contenido a mixin

@mixin apply-to-ie6-only {
  * html {
    @content;
  }
}
@include apply-to-ie6-only {
  #logo {
    background-image: url(/logo.gif);
  }
}
* html #logo {
  background-image: url(/logo.gif);
}

4.12 Interpolación

$position: left;
@mixin car($brand, $color) {
  .coche.#{$brand} {
    border-#{$position}: 2px;
    background-color: $color;
    background-image: url('img/#{$brand}-#{$color}.png');
  }
}
@include car('audi', 'green');
.coche.audi {
  border-left: 2px;
  background-color: green;
  background-image: url('img/audi-green.png');
}

4.13 Extend

.message {
  color: #333;
}
.success {
  @extend .message;
  border-color: green;
}
.error {
  @extend .message;
  border-color: red;
}
.message, .success, .error, .warning {
  color: #333;
}
.success {
  border-color: green;
}
.error {
  border-color: red;
}

4.14 Operadores

article[role="main"] {
  float: left;
  width: 600px / 960px * 100%;
}

aside[role="complementary"] {
  float: right;
  width: 300px / 960px * 100%;
}
article[role="main"] {
  float: left;
  width: 62.5%;
}

aside[role="complementary"] {
  float: right;
  width: 31.25%;
}

4.15 Funciones

$base: #f04615;
$list: 200, 500, 1200;

.class {
  width: nth($list, 3);
  color: darken($base, 5%);
  background-color:
    lighten($base, 25%);
}
.class {
  width: 1200;
  color: #f6430f;
  background-color: #f8a58d;
}

4.16 Definir funciones

$grid-width: 40px;
$gutter-width: 10px;

@function grid-width($n) {
  @return $n * $grid-width + ($n - 1) * $gutter-width;
}

#sidebar { width: grid-width(5); }
#sidebar {
  width: 240px;
}

4.17 IF

$animal: cat;
p {
  @if 1+1 == 2 { border: 2px solid black; }
  @if $animal == cat { color: white; }
}

4.18 IF

p {
  border: 2px solid black;
  color: white;
}

4.19 FOR

@for $i from 1 to 4 {
  .item-#{$i} { width: 2em * $i; }
}
.item-1 { width: 2em; }
.item-2 { width: 4em; }
.item-3 { width: 6em; }

4.20 EACH

@each $animal in puma, tiger, salamander {
  .#{$animal}-icon {
    background-image: url('/images/#{$animal}.png');
  }
}
.puma-icon {
  background-image: url('/images/puma.png'); }
.tiger-icon {
  background-image: url('/images/tiger.png'); }
.salamander-icon {
  background-image: url('/images/salamander.png'); }

4.21 WHILE

$i: 6;
@while $i > 0 {
  .item-#{$i} { width: 2em * $i; }
  $i: $i - 2;
}
.item-6 { width: 12em; }
.item-4 { width: 8em; }
.item-2 { width: 4em; }

4.22 logs

@debug 10em + 12em;
Line 1 DEBUG: 22em

5 Acerca de

5.1 Licencia

5.2 Fuentes

5.3 Bibliografía