How to CSS in 2015?

Who needs CSS?

How to CSS in 2015?

  • Optimize existing CSS
  • Preprocessors to the rescue!
  • Responsive web: media queries and their friends

Optimize existing CSS

Deadly sins

  • cause more problems than they solve
  • symptoms of a more severe disease
  • avoid them, please

!important

Optimize existing CSS

Optimize existing CSS

inline CSS

Optimize existing CSS

mess with the display property

Optimize existing CSS

Selectors

Optimize existing CSS

Selectors

div ul li .special

Optimize existing CSS

Selectors

div ul li .special

1

Optimize existing CSS

Selectors

div ul li .special

1

2

Optimize existing CSS

Selectors

div ul li .special

1

2

3

Optimize existing CSS

Selectors

1

2

3

4

div ul li .special

Optimize existing CSS

Clean up unused CSS

Optimize existing CSS

Optimize images

Do we really need them?

Optimize existing CSS

Optimize images

  • image size is a secondary problem
  • the bottleneck is establishing the connection
  • the total file size is bigger, but it's still worth it
  • image sprites to the rescue!

Optimize existing CSS

Optimize images

  • background-position
  • what about the image tag?
  • semantic HTML + SEO
  • if you really need it: example #3

Optimize existing CSS

Optimize images

Data URI

Optimize existing CSS

Optimize images

  • not HTTP request at all
  • hard to maintain, bad for readability

Optimize existing CSS

Optimize images

Icon fonts

Optimize existing CSS

Optimize images

  • combine multiple images in a single request
  • small size
  • good scalability
  • deep browser support (IE8+)
  • limited number of images (64)
  • only one colour (stacking may be an option)

Optimize existing CSS

Optimize images

SVG images

Optimize existing CSS

Optimize images

  • it's the future, but it's not quite there yet
  • single colour
  • incomplete browser support

Optimize existing CSS

Reset CSS

Optimize existing CSS

Vendor specific rules

Optimize existing CSS

Best practices

  • naming: it should be based on use, not on properties
  • avoid redundancy
  • use short forms

Optimize existing CSS

Best practices

.foo {

  color: red;

  font-size: 24;

  padding: 10;

}

.bar {

  color: blue;

  font-size: 24;

  padding: 10;

}

.foo, .bar {

  font-size: 24;

  padding: 10;

}

.foo {

  color: red;

}

.bar {

  color: blue;

}

 

How to CSS in 2015

Preprocessors to the rescue!

  • CSS syntax is old and dumb
  • not DRY enough
  • hard to maintain
  • flat structure makes it hard to organize

Preprocessors to the rescue!

Which one?

  • Sass, SCSS
  • LESS
  • Turbine
  • Stylus
  • Switch CSS

Preprocessors to the rescue!

Which one?

Sass

  • older
  • the ruby implementation used to be slow
  • node.js implementation
  • invalid CSS (SCSS helps)

Less

  • backed by Bootstrap (Twitter)
  • code is similar to CSS
  • designer friendly documentation
  • class == mixin

Preprocessors to the rescue!

Variables

$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}

body {
  font: 100% Helvetica, sans-serif;
  color: #333;
}

Preprocessors to the rescue!

Variables

$font-size-xl: 40px;

h1 {
  font-size: $font-size-xl + 5;
}

 

h1 {
  font-size: 45px;
}

Preprocessors to the rescue!

Nesting

nav {
  color: #000;

 

  ul {

    list-style: none;

  }
}

nav {
  color: #000;

}

nav  ul {

  list-style: none;

}

Preprocessors to the rescue!

Nesting

nav {
  color: #000;

 

  ul {

    list-style: none;

  }
}

nav {
  color: #000;

}

nav  ul {

  list-style: none;

}

Preprocessors to the rescue!

Nesting

.slider {
  width: 100%;

 

  &-element {

    width: 25%;

  }
}

.slider {
  width: 100%;

}

.slider .slider-element {

  width: 25%;

}

Preprocessors to the rescue!

Nesting

.button {
  color: $color-primary;

 

  &:hover {

    color: $color-highlight;

  }
}

.button {
  color: #bada55;

}

.button:hover {

  color: #c0ffee;

}

Preprocessors to the rescue!

Nesting

  • don't go overboard: maximum 3 levels
  • use it for namespacing
  • watch out for long class names

Preprocessors to the rescue!

Partials, Imports

_partial.scss

@import 'partial'

 

nav {
  color: #000;

}

Preprocessors to the rescue!

Partials, Imports

  • not the same as CSS @import (won't cause FOUC)
  • create a main.scss to gather all the partials (table of contents)
  • modularization helps reusability, but don't go overboard

Preprocessors to the rescue!

Mixins

@mixin box() {

  display: inline-block;

  padding: 5px;

  box-sizing: border-box;

}

 

.button {

  @include box();

  background-color: #bada55;

}

.button {

  display: inline-block;

  padding: 5px;

  box-sizing: border-box;

  background-color: #bada55;

}

Preprocessors to the rescue!

Mixins

@mixin box($radius) {

  display: inline-block;

  padding: 5px;

  box-sizing: border-box;

  border-radius: $radius;

}

 

.button {

  @include box(10px);

  background-color: #bada55;

}

.button {

  display: inline-block;

  padding: 5px;

  box-sizing: border-box;

  border-radius: 10px;

  background-color: #bada55;

}

Preprocessors to the rescue!

Extend/Inheritance

.message {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;
}

.success {
  @extend .message;
  border-color: green;
}

.success {
  border: 1px solid #ccc;
  padding: 10px;
  color: #333;

  border-color: green;
}

Preprocessors to the rescue!

Functions

  • lighten($color, $amount)
  • transparentize($color, $amount)
  • length($list)
  • all the functions

Preprocessors to the rescue!

Control directives

  • almost like a real programming language
  • @if
  • @for
  • @each
  • @while

Preprocessors to the rescue!

Control directives

Preprocessors to the rescue!

How to compile it?

  • install node.js
  • install grunt
  • create grunt task
  • ideally no .css file should be committed

Preprocessors to the rescue!

Wrapping up

  • powerful tool
  • start small (variables, mixins, extensions)
  • you can do it individually (maybe it will convince the others)

How to CSS in 2015

Media queries and their friends

  • responsive web is the future (Google searches)
  • only hope for real multiplatform code base (Xamarin, Iconic, React native etc.)
  • easier to implement than you'd think
  • no need for JS at all
  • liligo
  • Where to go?

How to CSS in 2015

Media queries and their friends

.box {

  width: 200px;

}

@media(max-width: 480px) {

  .box {

    width: 100%;

  }

}

How to CSS in 2015

Media queries and their friends

@media(max-width: $screen-xs)

  and (min-width: $screen-lg) {

  .box {

    width: 100%;

  }

}

  • $screen-lg: 1200px
  • $screen-md: 992px
  • $screen-sm: 768px
  • $screen-ss: 550px
  • $screen-xs: 480px

How to CSS in 2015

Media queries and their friends

$desktop: "(min-width: 1024px)";

 

@media #{$desktop} {

  .box {

    width: 100%;

  }

}

How to CSS in 2015

Media queries and their friends

$desktop-width: 1024px;

@mixin desktop {
  @media (min-width: #{$desktop-width}) {
    @content;
  }
}

p {
  font-size: 16px;
  @include desktop {
    font-size: 20px;
  }
}

How to CSS in 2015

Wrapping up

  • the future is bright

  • flexbox

  • SVG animations

How to CSS in 2015

Wrapping up

How to CSS in 2015

Wrapping up

Questions?

Thank you for your attention!

Where  to learn CSS3?

How to CSS in 2015 - MSG Systems tech talk

By Benedek Gagyi

How to CSS in 2015 - MSG Systems tech talk

  • 988