Pré-processeur

Problèmes

  • Trop volumineux
  • Trop complexes
  • Trop difficile à maintenir

Solutions

  • Les variables
  • L'imbrication
  • Les mixins
  • L'héritage

Variables

style.scss


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

body {
  font-family: $font;
  color: $color;
}
							

style.css


body {
  font-family: Helvetica, sans-serif;
  color: #333;
}
							

L'imbrication

Le code

style.scss


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

  li { 
    display: flex;
    align-items: center;
    justify-content: center;
  }

  a {
    width: 100%;
    padding: 6px 12px;
    text-decoration: none;
  }
}
							

style.css


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

nav li {
  display: flex;
  align-items: center;
  justify-content: center;
}

nav a {
  width: 100%;
  padding: 6px 12px;
  text-decoration: none;
}
							

Les fichiers

_menu.scss


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

style.scss


@import 'reset';
@import 'menu';
							

Les fichiers préfixés par _ ne sont pas générés dans un fichier CSS.

Ils doivent être utilisés avec la directive @import

Les mixins

style.scss


@mixin button($bgcolor, $color) {
  background-color: $bgcolor;
  color: $color;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 6px 12px;
}

.btn { @include button(#26a69a, #ffffff); }
							

style.css


.btn {
  background-color: #26a69a;
  color: #ffffff;
  display: inline-flex;
  justify-content: center;
  align-items: center;
  padding: 6px 12px;
}
							

L'héritance

style.scss


// This CSS won't print because %equal-heights is never extended.
%equal-heights {
  display: flex;
  flex-wrap: wrap;
}

// This CSS will print because %message-common is extended.
%message-common {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.message {
  @extend %message-common;
}

.success {
  @extend %message-common;
  border-color: green;
}

.error {
  @extend %message-common;
  border-color: red;
}

.warning {
  @extend %message-common;
  border-color: yellow;
}
							

style.css


.message, .success, .error, .warning {
  border: 1px solid #cccccc;
  padding: 10px;
  color: #333;
}

.success {
  border-color: green;
}

.error {
  border-color: red;
}

.warning {
  border-color: yellow;
}
							

Les opérateurs

style.scss


$width: 100%;
$max-width: 1280px;

.container { 
  width: $width; 
  max-width: $max-width; 
  margin-left: auto;
  margin-right: auto;
}

article[role="main"] {
  float: left;
  width: 2 / 3 * $width;
}

aside[role="complementary"] {
  float: right;
  width: 1 / 3 * $width;
}
							

style.css


.container {
  width: 100%; 
  max-width: 1280px; 
  margin-left: auto;
  margin-right: auto;
}

article[role="main"] {
  float: left;
  width: 66.6666%;
}

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

Aller plus loin