CSSCompilers



CSSCompilers

0 0


CSSCompilers

Coding & Cocktails Kansas City Intro to CSS Compilers course

On Github KansasCityWomeninTechnology / CSSCompilers

Intro to CSS Compilers

Slides available at: bit.ly/CSSCmp

Join us on social media:

#LadyDevs

#KCWiT

#CodingAndCocktailsKC

  @codecocktailskc

  CodingAndCocktailsKC

Powered By:

Kansas City Developer Conference

June 22 - 24, 2016

KCWiT 10% off Discount Code: KCWITROCKS

June 24 - 26, 2016

Presentation

Our Hosts

The Nerdery

Mentors

Photo by JD Hancock cc

Last Month - Version Control

  • What Version Control is used for
  • Creating Repositories
  • Branching, Merging & Forking
  • Interaction Through the Command Line
  • Presentation & Worksheet Link

What we'll cover

  • Brief CSS Review
  • What CSS Compilation is and Why We Should Use It
  • Features of the Sass Preprocessor

CSS Review

Syntax

Image Credit:

LearnWebCode.com

Box Model

  • Element Selector:
    h1 { ... }
  • Class selector:
    .my-class { ... }
  • ID selector:
    #my-id { ... }
  • Descendant selector:
    div p { ... }
  • Properties:
    • margin
    • padding
    • border
    • font-family
    • font-size
    • list-style
  • Remember: The last rule applied wins!
  • Google Fonts
  • Color Pallette

What is a CSS Compiler?

Taking input (Sass in our case) and converting (or compiling) it into something else (plain old CSS in this case) so it can be understood by a third thing (your browser in this case)

Since your browser is considered to be the processor for your website, Sass is considered a preprocessor because it processes the code before your browser does.
  • Browsers only understand CSS
  • Helps reduce complexity
  • Reduce file size through minification
  • Warning: Once you start using a preprocessor, only edit pre-compiled files or you will overwrite your work!
CSS itself sometimes can make it hard to create clean/reusable code and the more styling you add the more complex your stylesheets become - compilers help with this! You can’t have some people editing .css and others editing .scss, .css edits will get overwritten with the next compile
Preprocessor vs. Postprocessor

No significant technical distinction

Preprocessor Postprocessor Sass, Less PostCSS, Autoprefixer Sometimes Own Syntax CSS Syntax Compiles into CSS Compiles into CSS Allows use of proposed CSS features or features not yet supported add browser prefixes is a common usage for postprocessors

Development Process

  • Create HTML/Style with Sass
  • Compile/Build
  • Deploy & View

Compiler Options

Sass

  • Syntactically Awesome Stylesheets
  • Ruby Sass: Written in Ruby and therefore has a Ruby Dependency
  • LibSass: Allows Sass to be used outside of Ruby
  • .scss (SASSy CSS) files vs. .sass files
Can get rid of Ruby dependency if using LibSass - this is what will be used with Grunt/Gulp/Webpack LibSass C/C++ port allows Sass to be used outside of Ruby .sass is the old version that just uses indenting without curly braces and semi-colons - hard to transfer b/t it & CSS leading to .scss - css syntax with the extra functionality

Less

  • Similar but with some slight differences in syntax and functionality
    • Example: Less uses @ in front of variables, Sass uses $
    • Example: Less uses mixin guards where Sass uses if statements
  • Originally written in Ruby but ported to JavaScript
Those are two most popular followed by Stylus but those are not the only options, there are others. We're going to focus on Sass. You'll find strong opinions on which is better among the development community but if you focus on learning one it will serve you best With Twitter's Bootstrap (a popular styling framework) moving to Sass it may gain a slight edge

Sass

  • Compilation options:
    • Command Line (Ruby Sass)
      sass inputfile.scss outputfile.css
                                          
      sass -watch inputfile.scss:outputfile.css
                                              
    • Task Runner (LibSass)
      • Gulp
      • Grunt
      • Webpack
    • GUI
When it gets old typing in the sass command all the time use watch May not want to store your CSS file when you're pushing it to version control Codekit is only on macs.

Why should we use them?

  • Cleaner & more maintainable code
  • Follow DRY (Don't Repeat Yourself) principles better
  • Increase Productivity
  • Selector nesting for better organization
Get more done in more code that is easier to read in less time

Sass Features

  • // Single-line comments that don't show in your CSS
  • /* Also supports multi-line comments that do show in compiled CSS */
  • Sourcemap for debugging help

Variables

  • $my-color: #fff;
  • Use these to help reduce repetition
  • Easily & quickly change things like colors or font stacks in one place
  • Don't replace everything, stick to common patterns
  • Common uses include colors, font-stacks, numbers, layout, font sizing
Example: 5px font size don't use the same variable for a 5px border-radius

Partials

  • Helpful for organization - keep all like things in one file so there is only one place to make changes
  • Allow you to import other scss files into each other
  • Typically start the filename with an _ (Example: _variables.scss) so it doesn't get compiled into it's own CSS file
  • Bring them into your main file using @import:
    @import "variables";
  • More advanced projects may have partials for typography, buttons, layout, forms, etc.

Nesting

  • Allows for better organization nesting css selectors inside each other
  • Avoid nesting more than 3 - 4 levels deep
  • Debugging Warning: Nesting can "create" selectors in your CSS that don't exist in your scss file making debugging difficult
.container {
    margin: 0;
    padding: 0; 

    .item {
        margin-bottom: 20px;
        border-top: 2px solid blue;
    }
}                           
.container {
    margin: 0;
    padding: 0;
}

.container .item {
    margin-bottom: 20px;
    border-top: 2px solid blue;
}                           
4 or more levels can create bloated, unnecessary CSS

Mixins

  • Reduce repetition
  • "Function" for your styling
  • Mixin libraries (Bourbon & Compass)
Define a function for those who may not know

Mixin Example

@mixin link-style ($link, $visit, $hover, $active) {
  a {
    color: $link;
    &:visited {
      color: $visit;
    }
    &:hover {
      color: $hover;   
    }
    &:active {
      color: $active;
    }
  }
}                       
a {
    @import link-style(blue, red, darkblue, green);
    text-decoration: none;
}

Operations

  • Allows math operations such as
    $my-var*2
  • Warning: Changes to my-var affect elements styled by $my-var*2 also
  • String operations
    $base_string: "Gin";
    $add-on-string: "Tonic";
    p:before {
        content: $base-string + " and " + $add-on-string; //Gin and Tonic
    }
    

Color Functions

  • Lighten: make the given color lighter
    lighten(#c0ffee, 20%)
  • Darken: make the given color darker
    darken(#decaff, 20%)
  • rgba: change the alpha (opacity) value of the color
    rgba(#f00d1e, 0.5)
Advanced: If Statements
$type: pina-colada;
p {
  @if $type == bloody-mary {
    color: red;
  } @else if $type == pina-colada {
    color: white;
  } @else {
    color: blue;
  }
}                       

Compiles to:

p {
    color: white;
}                       
Advanced: Loops

@for, @while, @each

@for $i from 1 through 3
    .style-#{$i}
    width: 10px * $i    

Compiles to:

.style-1 {
    width: 10px; 
}
.style-2 {
    width: 20px;
}
.style-3 {
    width: 30px; 
}                       

Extend

  • Reduce redundancy
  • Allows you to utilize styles already given to one selector in another selector
.btn {
    padding:5px;
    border: 1px solid #666;
    color:#000;
    text-decoration:none;
}

.btn-warning {
    @extend .btn
    background: red;
}                           

Debugging

  • Right click the element you're concerned with
    • Choose inspect
    • View the styles
    • The sourcemap will point you to the line in your scss file where the style in question exists.
  • Leave CSS expanded during development, minify for production

Converting CSS to Sass

Your browser does not support the video tag.

Organization

Despite Sass being awesome, organization is still important

stylesheets/
|
|-- modules/              # Common modules
|   |-- _all.scss         # Include to get all modules
|   |-- _utility.scss     # Module name
|   |-- _colors.scss      # Etc...
|-- partials/             # Partials
|   |-- _base.sass        # imports for all mixins + global project variables
|   |-- _buttons.scss     # buttons
|   |-- _figures.scss     # figures
|   |-- _grids.scss       # grids
|   |-- _typography.scss  # typography
|   |-- _reset.scss       # reset
|-- vendor/               # CSS or Sass from other projects
|   |-- _colorpicker.scss
|   |-- _jquery.ui.core.scss
|   ...
|
`-- main.scss            # primary Sass file
Sample Structure from The Sass Way

Review

  • Defined CSS Compilers
  • How CSS Compilation fits in the development process
  • Why we want to use CSS Compilers
  • Sass features

Tonight's Worksheet

Questions?

?

Thank You!

Keep in touch!

Join us again May 14th for an introduction to JavaScript!