Академический Документы
Профессиональный Документы
Культура Документы
1. Что такое препроцессоры?
2. Возможности Less и Sass.
3. Автоматизация.
Препроцессоры
4
Препроцессоры
— Профессиональные инструменты.
— Расширяют стандартные возможности языка.
— Бывают для CSS, HTML, JavaScript.
5
— Подключение файлов.
— Переменные.
— Математические операции.
— Управление цветами.
— Вложенные селекторы.
7
Less
— Написан на Ruby в 2009 году, портирован на JS.
— Работает прямо в Node.js и в браузере.
— Текущая версия 3.0.0.
lesscss.org и lesstester.com
8
Sass
— Написан на Ruby в 2006 году, портирован на C++.
— Есть: Dart Sass, LibSass, Ruby Sass.
— Текущая версия 3.5, наверняка в работе 4.0.
sass-lang.com и sassmeister.com
9
Sass и SCSS
01. a 01. a {
02. font: 02. font-weight: bold;
03. weight: bold 03. font-family: serif;
04. family: serif 04. &:hover {
05. &:hover 05. background-color: #eee;
06. background-color: #eee 06. }
07. }
Подключение
11
Подключение в CSS
01. /* style.css */ 01. /* normalize.css */
02. @import "normalize.css"; 02. body {
03. 03. margin: 0;
04. body { 04. }
05. background-color: tomato;
06. }
Подключение в Less
01. /* style.less */ 01. /* normalize.less */
02. @import "normalize.less"; 02. body {
03. 03. margin: 0;
04. body { 04. }
05. background-color: tomato;
06. }
Подключение в Sass
01. /* style.scss */ 01. /* normalize.scss */
02. @import "normalize.scss"; 02. body {
03. 03. margin: 0;
04. body { 04. }
05. background-color: tomato;
06. }
Переменные в CSS
01. html {
02. --fancy-color: tomato;
03. }
04. body {
05. color: var(--fancy-color);
06. }
17
Видимость в CSS
01. p { 01. <p>
02. --fancy-color: tomato; 02. <em>Я томат</em>
03. } 03. </p>
04. em { 04.
05. color: var(--fancy-color); 05. <p></p>
06. } 06. <em>А я нет</em>
18
Переменные в Less
01. @fancy-color: tomato; 01. body {
02. 02. color: tomato;
03. body { 03. }
04. color: @fancy-color;
05. }
19
Видимость в Less
01. p { 01. p {
02. @fancy-color: tomato; 02. @fancy-color: tomato;
03. } 03. em {
04. em { 04. color: @fancy-color;
05. color: @fancy-color; 05. }
06. } 06. }
20
Переменные в Sass
01. $fancy-color: tomato; 01. body {
02. 02. color: tomato;
03. body { 03. }
04. color: $fancy-color;
05. }
21
Видимость в Sass
01. p { 01. p {
02. $fancy-color: tomato; 02. $fancy-color: tomato;
03. } 03. em {
04. em { 04. color: $fancy-color;
05. color: $fancy-color; 05. }
06. } 06. }
22
Интерполяция переменных
01. @where: bottom; 01. $where: bottom;
02. 02.
03. .no-@{where} { 03. .no-#{$where} {
04. border-@{where}: none; 04. border-#{$where}: none;
05. } 05. }
25
Результат интерполяции
01. .no-bottom {
02. border-bottom: none;
03. }
Математика
28
Псевдоклассы
01. .link { 01. .link {
02. color: tomato; 02. color: tomato;
03. &:hover, 03. }
04. &:focus { 04. .link:hover,
05. color: plum; 05. .link:focus {
06. } 06. color: plum;
07. } 07. }
41
Псевдоэлементы
01. .item { 01. .item {
02. color: tomato; 02. color: tomato;
03. &::before { 03. }
04. content: "—"; 04. .item::before {
05. } 05. content: "—";
06. } 06. }
42
Модификаторы
01. .button { 01. .button {
02. width: 100px; 02. width: 100px;
03. &--wide { 03. }
04. width: 500px; 04. .button--wide {
05. } 05. width: 500px;
06. } 06. }
43
❌ Элементы
01. .button { 01. .button {
02. color: tomato; 02. color: tomato;
03. &__icon { 03. }
04. width: 16px; 04. .button__icon {
05. } 05. width: 16px;
06. } 06. }
44
01. a { 01. a {
02. color: tomato; 02. color: tomato;
03. b { 03. }
04. color: plum; 04. a b {
05. c { 05. color: plum;
06. color: wheat; 06. }
07. } 07. a b c {
08. } 08. color: wheat;
09. } 09. }
45
Кроличья нора
01. .block { 01. .block__elem {
02. &__elem { 02. &--mod {
03. &--mod { 03. &::before { … }
04. &::before { … } 04. }
05. } 05. }
06. }
07. }
46
Примеси в Less
01. .heading() { 01. .clearfix() {
02. margin-bottom: 16px; 02. &::after {
03. font-size: 32px; 03. display: table;
04. } 04. clear: both;
05. content: "";
06. }
07. }
50
Примеси в Sass
01. @mixin heading { 01. @mixin clearfix {
02. margin-bottom: 16px; 02. &::after {
03. font-size: 32px; 03. display: table;
04. } 04. clear: both;
05. content: "";
06. }
07. }
51
Примеси в HTML
01. .heading { 01. <h2 class="
02. margin-bottom: 16px; 02. title
03. font-size: 32px; 03. heading
04. } 04. clearfix
05. .clearfix::after { 05. ">
06. clear: both;
07. }
54
Расширения в Less
01. .list-reset {
02. list-style: none;
03. }
04. .main-nav__items {
05. margin: 0;
06. &:extend(.list-reset);
07. }
59
Расширения в Sass
01. .list-reset {
02. list-style: none;
03. }
04. .main-nav__items {
05. margin: 0;
06. @extend .list-reset;
07. }
60
Рекомендации
— Для каждого блока — отдельный файл стилей.
— Не создавайте вложенность больше двух уровней.
— Не используйте цветовые функции для получения цветов из дизайна.
— Не используйте расширения, совсем.
— Не используйте сразу всё, пробуйте постепенно.
Автоматизация
img/ img/
fonts/ fonts/
less/ sass/
blocks/ blocks/
main-nav.less main-nav.scss
page-main.less page-main.scss
style.less style.scss
mixins.less mixins.scss
variables.less variables.scss
67
Неудобный CSS
01. .wait {
02. -ms-interpolation-mode: nearest-neighbor;
03. image-rendering: -webkit-optimize-contrast;
04. image-rendering: -moz-crisp-edges;
05. image-rendering: -o-pixelated;
06. image-rendering: pixelated;
07. }
68
Удобный CSS
01. .wait {
02. image-rendering: pixelated;
03. }
PostCSS
01. var postcss = require("gulp-postcss");
02. var autoprefixer = require("autoprefixer");
03. gulp.task("css", function () {
04. return gulp.src("*.css")
05. .pipe(postcss([autoprefixer()]))
06. .pipe(gulp.dest("build"));
07. });
70
Неудобный SVG
01. <body>
02. <svg viewBox="0 0 547.769 410.595" fill="#cd6799">
03. <path d="M471.418 235.99a112.63 112.63 0 0 0-49.627 11…
04. 9.729-4.561 16.32-29 66.16-36 81.6a467.21 467.21 0 0…
05. 13.92 44.64c8.013 0 16.075-10.391 19.664-15.688l.015…
06. </svg>
07. </body>
71
Удобный SVG
01. <body>
02. <include src="images/icon.svg"></include>
03. </body>
PostHTML
01. var posthtml = require("gulp-posthtml");
02. var include = require("posthtml-include");
03. gulp.task("html", function () {
04. return gulp.src("*.html")
05. .pipe(posthtml([include()]))
06. .pipe(gulp.dest("build"));
07. });
73
1. Less + Gulp
2. Less + Grunt
3. Sass + Gulp
4. Sass + Grunt
74
Задание на дом
1. Пройти дополнительные курсы.
2. Выбрать препроцессор и сборщик.
3. Получить обновления от Кекса.
4. Проверить проект с помощью линтеров.
5. Начать вёрстку с CSS-препроцессором.