Outils pour utilisateurs

Outils du site


issue88:command_conquer

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Prochaine révision
Révision précédente
issue88:command_conquer [2014/10/30 16:34] – créée andre_domenechissue88:command_conquer [2015/01/19 17:06] (Version actuelle) – [8] auntiee
Ligne 1: Ligne 1:
-Last month’s article was devoted to configuring grunt and node.js to allow for compiling of SASS. However, very little time was spent actually discussing SASS. This article should hopefully remedy that, as SASS can be extremely useful for any web programmer.+====== 1 ====== 
 + 
 +**Last month’s article was devoted to configuring grunt and node.js to allow for compiling of SASS. However, very little time was spent actually discussing SASS. This article should hopefully remedy that, as SASS can be extremely useful for any web programmer.
  
 What is SASS? What is SASS?
  
-SASS stands for Syntactically Awesome Style-Sheets, and is a pre-processor for CSS. It essentially adds features to CSS (such as nesting, functions, mixins, imports, custom units, mathematics, inheritance, and variables).+SASS stands for Syntactically Awesome Style-Sheets, and is a pre-processor for CSS. It essentially adds features to CSS (such as nesting, functions, mixins, imports, custom units, mathematics, inheritance, and variables).** 
 + 
 +L'article du mois dernier était consacré à la configuration de grunt et node.js pour permettre la compilation de SASS. Cependant, nous n'avons pas vraiment parlé de SASS. Cet article devrait, je l'espère, y remédier, car SASS peut être extrêmement utile à tout programmeur Web. 
 + 
 +Qu'est-ce que SASS ? 
 + 
 +SASS signifie « Syntactically Awesome Style Sheets » [Ndt : feuilles de style syntaxiquement impressionnantes], et c'est un pré-processeur pour CSS. Il ajoute des fonctionnalités à CSS (comme l'imbrication, les fonctions, les « mixins », les importations, les unités de mesure personnalisées, les mathématiques, l'héritage et les variables). 
 + 
 +====== 2 ======
  
-Nesting+**Nesting
  
 Imagine you have a two types of links on your website - a “default” style that should be blue and underlined (the standard values), but then you have links in your menu that you want to remove the decorations from, and adjust the :hover settings to darken the color of the button. In normal CSS, it could look something like this: Imagine you have a two types of links on your website - a “default” style that should be blue and underlined (the standard values), but then you have links in your menu that you want to remove the decorations from, and adjust the :hover settings to darken the color of the button. In normal CSS, it could look something like this:
Ligne 14: Ligne 24:
  
 In SASS, however, you can do the following: In SASS, however, you can do the following:
 +
 +$bg-color: #0000FF;
 +
 +ul.menu { li { a{ text-decoration: none; &:hover { background-color: darken($bg-color, 15%);}}}}**
 +
 +Imbrication
 +
 +Imaginez que vous avez deux types de liens sur votre site Web - un style « par défaut » qui devrait être bleu et souligné (les valeurs standard), mais vous avez aussi des liens dans votre menu pour lesquels vous souhaitez supprimer les décorations et ajuster le paramètre :hover pour assombrir la couleur du bouton. En CSS normal, cela pourrait ressembler à ceci :
 +
 +ul.menu li a { text-decoration: none; }
 +
 +ul.menu li a:hover { background-color: #000000; }
 +
 +Avec SASS, vous pouvez faire ce qui suit :
  
 $bg-color: #0000FF; $bg-color: #0000FF;
Ligne 19: Ligne 43:
 ul.menu { li { a{ text-decoration: none; &:hover { background-color: darken($bg-color, 15%);}}}} ul.menu { li { a{ text-decoration: none; &:hover { background-color: darken($bg-color, 15%);}}}}
  
-It looks a little less legible when written in a single line. However, format it nicely, and it looks something like that shown top right.+====== 3 ====== 
 + 
 +**It looks a little less legible when written in a single line. However, format it nicely, and it looks something like that shown top right.
  
 A few other things I’m doing in this example: A few other things I’m doing in this example:
 • Defining a variable (i.e. the company color, or a primary color in a template). This variable can be used anywhere in the SASS file. This way, any change to the color scheme requires only a single edit. • Defining a variable (i.e. the company color, or a primary color in a template). This variable can be used anywhere in the SASS file. This way, any change to the color scheme requires only a single edit.
 • Using a SASS function called darken, which calculates the value for a darker version of the supplied color. SASS offers many functions, such as the ability to create color values based on hue, saturation and lightness (hsl). • Using a SASS function called darken, which calculates the value for a darker version of the supplied color. SASS offers many functions, such as the ability to create color values based on hue, saturation and lightness (hsl).
-• &:hover - the ampersand essentially places the following text directly behind the parent when nesting. For example ul { .menu{}} would result in ul .menu in normal CSS (in other words, find an element with the class menu after a ul element), and ul { &.menu{}} would result in ul.menu. The second one means “find a ul element with class menu”. Ergo, this example means “find each tag in the menu unordered list, and darken the color when the user hovers over it”.+• &:hover - the ampersand essentially places the following text directly behind the parent when nesting. For example ul { .menu{}} would result in ul .menu in normal CSS (in other words, find an element with the class menu after a ul element), and ul { &.menu{}} would result in ul.menu. The second one means “find a ul element with class menu”. Ergo, this example means “find each tag in the menu unordered list, and darken the color when the user hovers over it”.**
  
-Mixins+Cela semble un peu moins lisible quand on l'écrit sur une seule ligne. Cependant, en le formatant correctement, cela ressemble à ce qui est en haut à droite. 
 + 
 +Quelques autres choses que je fais dans cet exemple : 
 +• Définir une variable (c'est-à-dire la couleur de l'entreprise, ou une couleur primaire dans un modèle). Cette variable peut être utilisée n'importe où dans le fichier SASS. Ainsi, toute modification de la palette de couleurs ne nécessite qu'un seul changement. 
 +• Utiliser une fonction SASS appelée darken, qui calcule la valeur pour une version plus sombre de la couleur fournie. SASS propose de nombreuses fonctions, offrant la possibilité de créer des valeurs de couleur en fonction de la teinte, la saturation et la luminosité (HSL). 
 +• &:hover - l'esperluette sert essentiellement à mettre le texte suivant directement derrière le parent lors de l'imbrication. Par exemple ul { .menu{}} donnera ul .menu en CSS normal (autrement dit, trouver un élément de la classe menu après un élément ul), et ul { &.menu{}} donnera ul.menu. Ce dernier signifie « trouver un élément ul de la classe menu ». Donc, cet exemple signifie « trouver chaque balise dans la liste non ordonnée menu et assombrir la couleur lorsque l'utilisateur la survole ». 
 + 
 +====== 4 ====== 
 + 
 +**Mixins
 Mixins are essentially macros, or functions, that do no calculation, but instead turn one line of SASS into multiple lines of CSS. A prime example is using CSS gradients. As this is a feature implemented differently by nearly every browser, it requires many lines of code to ensure the gradient appears in every browser that supports it. A SASS mixin I use for this purpose, looks like the example on the next page, at the top. Mixins are essentially macros, or functions, that do no calculation, but instead turn one line of SASS into multiple lines of CSS. A prime example is using CSS gradients. As this is a feature implemented differently by nearly every browser, it requires many lines of code to ensure the gradient appears in every browser that supports it. A SASS mixin I use for this purpose, looks like the example on the next page, at the top.
  
-Now, this does look a bit like gibberish. However, the second background: line can safely be ignored if you don’t want to use an SVG colored background in IE9. Everything else is commented to indicate which version of the browser it supports, and the colors supplied in the parameters of the mixin ensure that every gradient looks the same. This is the defined mixin - as such, you need to type it only once. I typically put variables, functions and mixins at the top of the SASS file, but it doesn’t really matter.+Now, this does look a bit like gibberish. However, the second background: line can safely be ignored if you don’t want to use an SVG colored background in IE9. Everything else is commented to indicate which version of the browser it supports, and the colors supplied in the parameters of the mixin ensure that every gradient looks the same. This is the defined mixin - as such, you need to type it only once. I typically put variables, functions and mixins at the top of the SASS file, but it doesn’t really matter.** 
 + 
 +Mixins 
 + 
 +Les mixins sont essentiellement des macros, ou des fonctions, qui ne font aucun calcul, mais transforment plutôt une ligne SASS en plusieurs lignes CSS. Un bel exemple est l'utilisation des dégradés CSS. Comme c'est une fonctionnalité mise en œuvre de façon différente par presque chaque navigateur, elle nécessite de nombreuses lignes de code pour assurer que le dégradé apparaît dans tous les navigateurs qui le supportent. Un mixin SASS que j'utilise à cette fin ressemble à l'exemple en haut de la page suivante. 
 + 
 +Oui, cela ressemble à du charabia. Cependant, le deuxième background: line peut être ignoré si vous ne voulez pas utiliser un fond de couleur SVG dans IE9. Tout le reste est commenté pour indiquer quelle version du navigateur cela supporte, et les couleurs fournies dans les paramètres du mixin assurent que chaque gradient se ressemble. C'est le mixin défini - ainsi, vous n'avez à le saisir qu'une seule fois. Je place généralement les variables, fonctions et mixins en haut du fichier SASS, mais  cela n'a pas vraiment d'importance. 
 + 
 +====== 5 ======
  
-To actually use the gradient mixin (to create a CSS gradient), you simply need to type: @include gradient($color1,$color2);+**To actually use the gradient mixin (to create a CSS gradient), you simply need to type: @include gradient($color1,$color2);
  
 Example: Example:
Ligne 44: Ligne 87:
 @function name($param1, $param2) { @return $result; } @function name($param1, $param2) { @return $result; }
  
-And to use it in your SASS: name($param1, $param2);+And to use it in your SASS: name($param1, $param2);**
  
-Mathematics+Pour effectivement utiliser le mixin de gradient (pour créer un gradient en CSS), il vous suffit de taper : @include gradient($couleur1,$couleur2); 
 + 
 +Exemple : 
 +@include gradient(#367864,#537a7c); 
 + 
 +donnera la CSS présentée à droite. 
 + 
 +Fonctions 
 + 
 +Les fonctions sont similaires aux mixins. Définissez votre fonction avec : 
 + 
 +@function nom($param1, $param2) { @return $resultat; } 
 + 
 +Et pour l'utiliser dans votre SASS : nom($param1, $param2); 
 + 
 +====== 6 ====== 
 + 
 +**Mathematics
  
 SASS also adds options to use mathematical operators (such as *, +, /, -). You must always define units to your numbers, or else use a function to ensure that SASS knows what the unit should be. Otherwise it will result in invalid CSS (i.e. width: 150, instead of width: 150px). SASS also adds options to use mathematical operators (such as *, +, /, -). You must always define units to your numbers, or else use a function to ensure that SASS knows what the unit should be. Otherwise it will result in invalid CSS (i.e. width: 150, instead of width: 150px).
Ligne 63: Ligne 123:
   @extend .message;   @extend .message;
   color: red;   color: red;
 +}**
 +
 +Mathématiques
 +
 +SASS ajoute également des options pour utiliser les opérateurs mathématiques (tels que *, +, /, -). Vous devez toujours définir les unités de vos nombres, ou bien utiliser une fonction pour vous assurer que SASS sait quelle unité utiliser. Sinon, cela se traduira par un CSS valide (par exemple width: 150, au lieu de width: 150px).
 +
 +Héritage
 +
 +Cela vous permet d'utiliser dans votre fichier un CSS défini ailleurs. Si, par exemple, vous avez des messages d'avertissement et que chacun doit avoir une police de couleur différente, mais autrement reste le même, vous pouvez le définir comme ceci :
 +
 +.message {
 +  border: 1px solid #ccc;
 +  padding: 10px;
 +  color: #333;
 } }
  
-Importing+.error { 
 +  @extend .message; 
 +  color: red; 
 +
 + 
 +====== 7 ====== 
 + 
 +**Importing
  
 Lastly, SASS also allows you to create separate files, and import them into a main file. Something like this: Lastly, SASS also allows you to create separate files, and import them into a main file. Something like this:
Ligne 73: Ligne 154:
 _mixins.scss - to contain all your mixins _mixins.scss - to contain all your mixins
 _variables.scss - to contain your variables _variables.scss - to contain your variables
-app.scss - the main file, that only contains imports for each of the other files listed.+app.scss - the main file, that only contains imports for each of the other files listed.**
  
-If anyone is looking for a SASS-enabled framework with various useful features (i.e. a grid, accordion, carousel, etc.), I have used the Zurb Foundation framework a few times. Otherwise, if you prefer to use your own CSS code, it might be time to convert it to SASS, to make further edits a little less time-consuming. And using the C&C articles from Issues 84-87, you can create a git repository with a configured grunt system to house your new SASS framework.+Importation 
 + 
 +Enfin, SASS vous permet également de créer des fichiers séparés et de les importer dans un fichier principal. Quelque chose comme ceci : 
 + 
 +_mobile.scss - pour les tailles d'écran inférieures à 720px ; 
 +_desktop.scss - pour des écrans plus grands ; 
 +_mixins.scss - pour contenir tous vos mixins ; 
 +_variables.scss - pour contenir vos variables ; 
 +app.scss - le fichier principal, qui ne contient que les importations de chacun des autres fichiers répertoriés. 
 + 
 +====== 8 ====== 
 + 
 +**If anyone is looking for a SASS-enabled framework with various useful features (i.e. a grid, accordion, carousel, etc.), I have used the Zurb Foundation framework a few times. Otherwise, if you prefer to use your own CSS code, it might be time to convert it to SASS, to make further edits a little less time-consuming. And using the C&C articles from Issues 84-87, you can create a git repository with a configured grunt system to house your new SASS framework.
  
 If anyone has any issues, questions, corrections, or requests, you are always welcome to send me a quick email at lswest34+fcm@gmail.com. If anyone has any issues, questions, corrections, or requests, you are always welcome to send me a quick email at lswest34+fcm@gmail.com.
Ligne 82: Ligne 175:
  
 http://sass-lang.com/guide - The official SASS guide. It covers some examples, and also houses the manuals (under Documentation). http://sass-lang.com/guide - The official SASS guide. It covers some examples, and also houses the manuals (under Documentation).
-@+@** 
 + 
 +Si vous recherchez un environnement SASS compatible avec diverses fonctionnalités utiles (par exemple une grille, un accordéon, un carrousel, etc.), j'ai utilisé l'environnement de la fondation Zurb à quelques reprises. Sinon, si vous préférez utiliser votre propre code CSS, le moment est peut-être venu de le convertir en SASS, pour apporter les futures modifications plus rapidement. Et en utilisant les articles de C&C des numéros 84 à 87, vous pouvez créer un dépôt git avec un système grunt configuré pour héberger votre nouvel environnement SASS. 
 + 
 +Si quelqu'un a des problèmes, des questions, des corrections ou des demandes, je vous encourage vivement à m'envoyer un courriel rapide à lswest34+fcm@gmail.com. 
 + 
 +Pour aller plus loin : 
 + 
 +http://sass-lang.com/guide - Le guide officiel de SASS. Il donne quelques exemples, et contient également les manuels (dans Documentation). 
issue88/command_conquer.1414683296.txt.gz · Dernière modification : 2014/10/30 16:34 de andre_domenech