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

Modular CSS Features with PostCSS

Modular CSS Features with PostCSS

Since the release of CSS 2.1 in 2011, the W3C has used a modular approach for the development of new features, allowing for more immediate and incremental improvements to CSS.

PostCSS allows us to take the same modular approach to adding and managing new CSS specs — and even non-spec "super powers" — without having to worry about preprocessor specific syntax.

In this talk, we'll discuss...
* CSS releases through the years
* modularized specification
* vendor prefixes, mixin libraries and CSS preprocessors
* PostCSS - what it is and how it works
* specific PostCSS plugins in combination with (or in place of) Sass
* getting started with PostCSS today

Jon Milner

June 01, 2016
Tweet

Other Decks in Technology

Transcript

  1. 1998 2000 2002 2004 2006 2008 2010 CSS 1.0 1996

    released 1997 1999 2001 2003 2005 2007 2009 2011 1.0
  2. 1998 2000 2002 2004 2006 2008 2010 CSS 2.0 1996

    released 1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0
  3. 1998 2000 2002 2004 2006 2008 2010 CSS 3 1996

    work began 1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0
  4. 1998 2000 2002 2004 2006 2008 2010 CSS 2.1 1996

    candidate recommendation > working draft > release 1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0
  5. 1998 2000 2002 2004 2006 2008 2010 CSS 2.1 1996

    1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0 candidate recommendation > working draft > release
  6. 1998 2000 2002 2004 2006 2008 2010 CSS 2.1 1996

    1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0 candidate recommendation > working draft > release
  7. 1998 2000 2002 2004 2006 2008 2010 CSS 2.1 1996

    1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0 2.1 candidate recommendation > working draft > release
  8. 1998 2000 2002 2004 2006 2008 2010 1996 13 years

    since last major release 1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0 2.1
  9. 1998 2000 2002 2004 2006 2008 2010 1996 10 years

    since work began on CSS 3 1997 1999 2001 2003 2005 2007 2009 2011 1.0 2.0 2.1
  10. • not a single specification • a series of modularized

    specifications • refers to everything published after 2.1
  11. compass .example { @include border-radius(3px); } .example { -webkit-border-radius: 3px;

    -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; } input output
  12. @import "foo"; $base-font-color: #333; @mixin button($primary, $secondary) { background-color: $primary;

    color: $secondary; &:hover { color: $secondary; } &:active { background-color: shade($primary, 10%); } } .button { @include button($base-font-color, #fff); &.button--info { @include button($blue, #fff); } &.button--warning { @include button($yellow, #fff); } &.button--danger { @include button($red, #fff); } } imports variables mixins nesting functions
  13. • Utilizes most recent data from Can I Use •

    Uses Browserslist so you can target browsers using queries like “last 2 versions” or “>5%” • Removes Unnecessary Prefixes • W3C Spec rather than specific syntax autoprefixer
  14. autoprefixer .example { border-radius: 3px; } .example { -webkit-border-radius: 3px;

    -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; } input output
  15. autoprefixer .example { border-radius: 3px; } .example { -webkit-border-radius: 3px;

    -moz-border-radius: 3px; -ms-border-radius: 3px; -o-border-radius: 3px; border-radius: 3px; } input output REAL CSS!
  16. 110 Files 21,300 Lines of Code 105 Files 9,800 Lines

    of Code 72 Files 7,900 Lines of Code
  17. 1 File 87 Lines of Code 1 File 128 Lines

    of Code 1 File 179 Lines of Code postcss-nested postcss-simple-vars postcss-mixins
  18. postcss-import @import "normalize.css"; @import "css/foo.css"; @import "css/bar.css"; body { background:

    black; } /* ... ./node_modules/ normalize.css/normalize.css */ /* ... content of foo.css */ /* ... content of bar.css */ body { background: black; } input output
  19. postcss-nested .phone { &_title { width: 500px; @media (max-width: 500px)

    { width: auto; } body.is_dark & { color: white; } } img { display: block; } } .phone_title { width: 500px; } @media (max-width: 500px) { .phone_title { width: auto; } } body.is_dark .phone_title { color: white; } .phone img { display: block; } input output
  20. postcss-simple-vars $dir: top; $blue: #056ef0; $column: 200px; .menu_link { background:

    $blue; width: $column; } .menu { width: calc(4 * $column); margin-$(dir): 10px; } .menu_link { background: #056ef0; width: 200px; } .menu { width: calc(4 * 200px); margin-top: 10px; } input output
  21. postcss-calc :root { --font-size: 16px; } h1 { font-size: calc(

    var(--font-size) * 2 ); height: calc(100px - 2em); margin-bottom: calc( var(--font-size) * 1.5 ); } h1 { font-size: 32px; height: calc(100px - 2em); margin-bottom: 24px } input output
  22. cssgrace .foo { background: rgba(112, 26, 0, .3); } .bar

    { display: inline-block; opacity: 0.5; } .foo { filter: progid:DXImageTransf... } .bar { display: inline-block; *display: inline; *zoom: 1; opacity: 0.5; filter: alpha(opacity=50); } input output
  23. postcss-assets features • URL resolution • Load paths • Base

    path • Base URL • Relative Paths • Cache buster • Image dimensions • Inlining files /* Image dimensions */ .foo { width: width(‘images/foo.png'); height: height(‘images/foo.png'); background-size: size('images/foo.png'); } /* Inlining files */ .foo { background: inline('images/foobar.png'); } examples
  24. postcss-hexrgba .foo { color: rgba(#0fab53, 0.8) } .bar { background:

    linear-gradient( rgba(#fff, 0.1), rgba(#fff, 0.2) ); } .foo { color: rgba(15, 171, 83, 0.8) } .bar { background: linear-gradient( rgba(255, 255, 255, .1), rgba(255, 255, 255, .2) ); } input output
  25. postcss-font-magician body { font-family: "Alice"; } @font-face { font-family: "Alice";

    font-style: normal; font-weight: 400; src: local(“Alice"), local("Alice-Regular"), url(“...eot?#”) format("eot"), url(“...woff2") format("woff2"), url(“...woff") format("woff"), url(“...ttf") format("truetype") } body { font-family: "Alice"; } input output
  26. postcss-apply :root { --toolbar-theme: { background-color: red; color: white; border:

    1px solid green; }; --toolbar-title-theme: { color: green; }; } .toolbar { @apply --toolbar-theme; } .toolbar__title { @apply --toolbar-title-theme; } .toolbar { background-color: red; color: white; border: 1px solid green; } .toolbar__title { color: green; } input output
  27. =