Stellan introduces you to SASS and LESS witch are two types of pre-processors for CSS. The use of them allow us to make CSS fun again using variables, nestling, mathematical operations and more when we write web design code. This makes it much easier, faster and more fun to create and update our CSS-code. Stellan also brings up some dos and don'ts using CSS/SASS/LESS and some other nice tools like Sourcemaps, Autoprefixer, CSSNano together with GULP Task Runner.
5. Problems with CSS
• ”The color problem”
• Explains many different problems that occurs when we don’t have any variables, not only for colors.
• When we don’t have variables we have to repeat ourselves, often many times.
• ”The duplication problem”
• When we have to write browser specific values we repeat ourselves, making our code look messy
• ”The cascade problem”
• To be specific in CSS we have to write a many elements on one row to follow the DOM-structure
• If we do changes in the HTML-code, even smaller once, we have to change a lot in the CSS.
• ”The calculation problem”
• There aren't possible to do any mathematical calculations in CSS.
• ”The importing problem”
• The support of the import function in CSS are browser specific making it unreliable.
• When we import we want to include the whole file into our CSS, working just like INCLUDE in PHP.
7. How do we solve these problems
with LESS or SASS?
• Variables
• Mixins (similar to a function – should always be used to include
functionality)
• Nestled code does it easy to follow the DOM-structure
• Inherited code enables multiple elements to have the same
properties/values
• Functions (both own and language specific – should always return a value)
• Mathematical operations directly
• Smarter import makes it easy to split up large files
10. Nestled code in LESS / SASS
LESS
body {
a {
color: #FFF;
&:hover {
color: #F00;
}
}
}
Outputs:
body a {
color: #FFF;
}
body a:hover {
color: #F00;
}
SASS (exactly the same)
body {
a {
color: #FFF;
&:hover {
color: #F00;
}
}
}
Outputs:
body a {
color: #FFF;
}
body a:hover {
color: #F00;
}
14. Import in LESS / SASS
LESS
@import "foo.css"; //@import url(foo.css);
@import "foo.less"; //Content renders in file
@import (less) "http://foo.com/bar"; //@import
"http://foo.com/bar";
as a LESS-file
@import "foo"; //foo.less renders in file
@import
SASS
@import "foo.css"; //@import url(foo.css);
@import "foo.scss"; //Content renders in file
@import "http://foo.com/bar"; //@import "http://foo.com/bar";
@import "foo"; //_foo.scss renders in file
@import "foo", "foo2"; //Content renders in file
$family: unquote("Droid+Sans");
@import url("http://fonts.googleapis.com/css?family=#{$family}");
// Outputs:
// @import
url("http://fonts.googleapis.com/css?family=Droid+Sans");
#main {
@import "foo"; //Content renders in file
}
15. Dos and Don’ts with LESS / SASS
• Most common CSS guidelines also applies when using SASS/Less.
• Avoid inline-styling at all cost. Avoid ID and tags, use classes.
• Never use important.
• Don’t use long selectors (in SASS/LESS don’t nestle your code to far). Makes your CSS file bigger than necessary.
• Be consistent with units. Use only a few. Choose one static (px, pt, mm, cm, in) and one, maybe two, relative (%, vh, em, ).
• Don’t forget to support all browsers. Use Autoprefixer (https://css-tricks.com/autoprefixer/).
• Specific
• Mixins.
• Use DRY-principle (Do not Repeat Yourself).
• Keep your mixins simple, use KISS-principle (Keep It Simple Stupid).
• Use arguments, but only when needed. If you have no arguments, don’t use parentheses.
• Variables
• Naming – Be specific! Preferred lower-case-with-dashes.
• Use as often as possible. Easier when you change your mind.
• Use sourcemaps! But only in dev environment.
• Organize your files. Use including and preferable subfolders. Depending on project size.
17. Structure in my demo project
• Uses SASS.
• Based on GULP task runner.
• Uses Sourcemaps and Autoprefixer.
• Uses live-reload for SASS, JavaScript and HTML.
• Also contains GULP-structure for JavaScript with TDD (Karma/Jasmine).
• Saves both the minified and the full version of the CSS-file.
http://tinyurl.com/swetugg-sass
Aweria
Tech-startup inom akutsjukvård.
10 sjukhus som kunder och fler på gång.
Selfie med publiken
LESSFunktion för att automatiskt välja bakgrundsfärg utifrån given textfärg. Om färgen är mer ljus så kommer svart bakgrund att användas annars vit.
SASS
Funktion för att generera ett RGBA värde med ett procentuellt värde för alfa.