Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sass for Fun and Profit

Sass for Fun and Profit

Presented at Chicago Code Camp 2018.

Abstract:
Ugh, CSS, it seems like it has a mind of its own and feels super complicated for anything more than a simple layout. If only there was some way to apply the best parts of programming to it, like variables or functions, and make things better. Enter: Sass, an extension of CSS that makes writing your stylesheets fun and easy. In this talk, I’ll introduce the basics of Sass: variables, nesting, mixins, and importing. We’ll discuss both of the available syntaxes for Sass and work through some ways to improve existing CSS using the magic of Sass. Bring your laptop to code along! This talk assumes basic knowledge of HTML and CSS, and is a great place to start for people just starting to feel the headaches of writing pure CSS.

Fen Slattery

June 09, 2018
Tweet

More Decks by Fen Slattery

Other Decks in Technology

Transcript

  1. 1. What's Sass? 2. Why would you use it? 3.

    What are some cool things it can do? @sublimemarch
  2. It's a language that takes input and processes it into

    some output, which is then used as the input to something else. @sublimemarch
  3. Your Sass file is processed and saved as a normal

    CSS file you can use on the web. @sublimemarch
  4. Compiling Sass! After you install Sass, you compile Sass to

    CSS using the sass command and telling Sass which file to build from and where to output CSS to. sass input.scss output.css @sublimemarch
  5. Sass lets you use features that don't exist yet in

    CSS3, or that aren't widely supported. @sublimemarch
  6. Newer syntax, SCSS: nav { color: red; background-color: black; a

    { text-decoration: none; color: white; } } @sublimemarch
  7. It's easy to read and write even if you aren't

    familiar with actual Sass features. @sublimemarch
  8. Variables are the way to store information you want to

    reuse throughout your styles. @sublimemarch
  9. Use $ to make something a variable. $purple: #5C528D; $big-padding:

    100px; $best-font: "Comic Sans"; @sublimemarch
  10. What do I use variables for? — colors — font

    families — padding values — border values — font sizes — themes @sublimemarch
  11. HTML has a clear nested, visual hierarchy, but CSS doesn't.

    Sass lets you nest your CSS selectors in a way that follows this same visual hierarchy. @sublimemarch
  12. <nav> <ul> <li> <a href="/home">Home</a> </li> <li> <a href="/about">About</a> </li>

    <li> <a href="/contact">Contact</a> </li> </ul> </nav> @sublimemarch
  13. nav { background-color: blue; ul { margin-right: 30px; li {

    background-color: red; a { text-decoration: none; } } } } @sublimemarch
  14. But this compiles into... nav { background-color: blue; } nav

    ul { margin-right: 30px; } nav ul li { background-color: red; } nav ul li a { text-decoration: none; } @sublimemarch
  15. You can also do this with properties that share the

    same prefix! section { font: { family: "Comic Sans"; size: 24px; weight: bold; } } @sublimemarch
  16. Use & to stick a selector onto its parent, instead

    of creating a nested selector. ul { background-color: blue; li { color: white; &:first-child { margin-left: 30px; } } } @sublimemarch
  17. which compiles into... ul { background-color: blue; } ul li

    { color: white; } ul li:first-child { margin-left: 30px; } @sublimemarch
  18. Mixins let you make groups of declarations to reuse across

    the site. @mixin input-styling { color: black; font-size: 18px; padding: 10px 5px; } input[type="text"] { @include input-styling; } @sublimemarch
  19. You can pass content to a mixin. @mixin phone-and-tablet {

    @media only screen and (max-width: 960px) { @content; } } @include phone-and-tablet { margin: 0 30px; } @sublimemarch
  20. You can pass values into a mixin. @mixin border-radius($radius) {

    -webkit-border-radius: $radius; -moz-border-radius: $radius; -ms-border-radius: $radius; border-radius: $radius; } input { @include border-radius(8px); } @sublimemarch
  21. CSS has imports that let you split your CSS into

    smaller, more maintainble portions. @sublimemarch
  22. But! Each time you use @import in CSS, it creates

    another HTTP request. @sublimemarch
  23. Sass uses @import from CSS, but it combines the files

    together, so there's just one HTTP request. @sublimemarch
  24. @extend lets you share a set of CSS properties from

    one selector to another. .button { font-family: "Comic Sans"; padding: 10px 14px; } .blue-button { @extend .button; background-color: blue; } @sublimemarch
  25. Which becomes: .button { font-family: "Comic Sans"; padding: 10px 14px;

    } .blue-button { font-family: "Comic Sans"; padding: 10px 14px; background-color: blue; } @sublimemarch
  26. You can also use placeholder classes that only show when

    extended. %button { font-family: "Comic Sans"; padding: 10px 14px; } .blue-button { @extend %button; background-color: blue; } @sublimemarch
  27. Use #{} to include text. @mixin border($side) { border-#{$side}: 3px

    solid blue; padding-#{$side}: 20px; } .box { @include border(left); } @sublimemarch
  28. And so much more! — control statements (if, for, each,

    while) — operators (+, -, *, /, %) — color operators (rgba) @sublimemarch