SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
FROM CSS
TO SASS IN
WORDPRESS
James Steinbach WP Summit 2014
@jdsteinbach #wpsummit .
1
BENEFITS OF SASS
•  Code organization (partials, nesting)
•  Faster styling (mixins, functions)
•  Scalable code (logic, variables)
2
NOT LIKE THIS:
3
MORE LIKE THIS:
4
CONVERT A STYLESHEET TO
SASS
So where do we even start?
5
THE BARE MINIMUM
•  CSS is valid Sass:
–  change style.css to style.scss
–  edit
–  compile back to style.css
6
SETTING UP PARTIALS
•  Copy each “section” of style.css to a separate
.scss file in your /sass/ folder.
•  Name the file after what it does with an
underscore prefix.
–  Example: styles relating to your navigation go to
_navigation.scss
•  Import that partial in your main style.scss file.
–  @import 'navigation';
7
REFACTOR AS NECESSARY
.header .logo {
//styles
}
.header .tagline {
//styles
}
.header .menu {
//styles
}
.header {
.logo {
//styles
}
.tagline {
//styles
}
.menu {
//styles
}
}
8
REFACTOR AS NECESSARY
.menu a {
-webkit-transition: color 0.2s;
-moz-transition: color 0.2s;
transition: color 0.2s;
}
.menu a {
@include transition(color 0.2s);
}
9
COMPILE YOUR SASS FOR
WP
Now that we broke it down, how do we
put it back together?
10
WP REQUIREMENTS
•  Theme root contains style.css with WP
comments
•  All Sass files in a subdirectory to keep theme root
clean
•  Ability to compress output for deployed code
•  Avoid long, complex command line code
11
CLI COMPILE METHODS
•  Sass CLI
–  Compile:
sass style.scss style.css
–  Watch:
sass --watch style.scss style.css
•  Compass CLI
–  Watch:
compass watch
12
GUI COMPILE METHODS
•  Codekit: incident57.com/codekit
•  Koala: koala-app.com
•  Scout: mhs.github.io/scout-app
•  Compass.app: compass.kkbox.com
13
COMPASS WATCH
•  Easiest CLI syntax:
compass watch
•  Config.rb file for options:
–  File path (keep style.css in the right place)
–  Output
expanded
compressed
14
MY COMPASS CONFIG.RB
•  Lives in /theme/sass/
–  style.css goes up a level to theme root
css_dir = '..'
–  sass files live in same /sass/ directory as config.rb
sass_dir = ''
–  development setting
output_style = :expanded
–  deployment setting
output_style = :compressed
15
WP STYLE.CSS COMMENTS
•  style.scss in /theme/sass/
•  Include '!' to preserve comments in :compressed output
/*!
Theme Name: Sassy Theme
Theme URI: http://jamessteinbach.com/
Author: James Steinbach
Author URI: http://jamessteinbach.com
Description: From CSS to Sass
*/
@import 'variables';
@import 'bourbon/bourbon';
//etc all your other imports…
16
ORGANIZE YOUR PARTIALS
Can I make all these new files easier to
maintain?
17
CASCADES STILL MATTER
•  Import your broadest styles first.
•  Import your most specific styles last.
18
USE FOLDERS
•  Group your .scss partials into folders:
–  Base (variables, mixins, reset, typography)
–  Layout (grid, header, footer)
–  Components (buttons, menus, forms, widgets)
–  Pages (home, landing page, portfolio)
–  3rd Party (plugins, vendors, etc)
19
FULL DISCLOSURE
•  My Sass folder structure is not usually this
detailed.
–  /bourbon/
–  /neat/
–  /underscores/ (blank theme styles)
–  All my partials (from _variables.scss to _home.scss)
20
TRY THESE SASS TOOLS
Are there any other tricks for making this
workflow better?
21
BOURBON
•  Advanced Measurement/Color Functions
–  golden-ratio(16px, 1);
returns 25.888px
–  strip-units(12px);
returns 12
–  tint(#330000, 50%);
returns #997f7f;
–  rem(24px);
default $em-base: 16px, returns 1.5rem
22
BOURBON
•  Prefixing Mixins
–  @include animation();
–  @include background-image();
–  @include columns();
–  @include flexbox();
–  @include transform();
–  @include font-feature-settings();
23
BOURBON NEAT
$grid-columns: 12;
$gutter: 1em;
.container {
@include outer-container();
}
.grid-item {
@include span-columns(3);
}
24
MY MIXINS: BREAKPOINTS
$breakpoints: (small: 600px, large: 920px);
@mixin breakpoint($name) {
@if not index(map-keys($breakpoints),
$name) {
@warn "Invalid breakpoint '#{$name}'.";
} @else {
@media (min-width: map-
get($breakpoints, $name)) {
@content;
}
}
}
25
MY MIXINS: FONT SIZES
$font-sizes: (sm: 14, p: 16, bq: 20,
ssh: 24, sh: 30, h: 32, hero: 48);
@each $label, $size in $font-sizes {
%#{$label} {
font-size: $size * 1px;
font-size: rem($size);
}
.page-title { @extend %h; }
26
MY MIXINS: ABS. POSITION
@mixin abs($t,$r,$b,$l) {

position: absolute;
top: $t;
right: $r;

bottom: $b;
left: $l;
}
27
MY MIXINS: FONT-SMOOTHING
@mixin font-smoothing($val) {
@if $val == 'antialiased' or $val ==
'subpixel-antialiased' {
-webkit-font-smoothing: $val;
-moz-osx-font-smoothing: $val;
} @else {

 @warn 'Invalid value.'
}
}
28
RESOURCES
Where do I get more information?
29
ADDITIONAL READING
•  “Compass Compiling and WordPress Themes,” Chris
Coyier - css-tricks.com/compass-compiling-and-
wordpress-themes/
•  “How to Use Sass with WordPress,” Andy Leverenz -
elegantthemes.com/blog/tips-tricks/how-to-use-sass-
with-wordpress-a-step-by-step-guide
•  “Get Your Sass in Line,” Micah Godbolt - godbolt.me/
blog/get-your-sass-in-line.html
•  “Architecture for a Sass Project,” Hugo Giraudel -
sitepoint.com/architecture-sass-project/
30
DOCUMENTATION
•  Sass: sass-lang.com
•  Compass: compass-style.org
•  Bourbon: bourbon.io/docs
•  Bourbon Neat: neat.bourbon.io/docs
•  SassMeister App: sassmeister.com
31
JAMES STEINBACH
Senior Front End Developer
The Idea People in Charlotte, NC
@jdsteinbach | jamessteinbach.com/blog
32

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (11)

Sass and compass workshop
Sass and compass workshopSass and compass workshop
Sass and compass workshop
 
Semantics, less and sarcasm
Semantics, less and sarcasmSemantics, less and sarcasm
Semantics, less and sarcasm
 
Deep dive into sass
Deep dive into sassDeep dive into sass
Deep dive into sass
 
Sass presentation
Sass presentationSass presentation
Sass presentation
 
Why less?
Why less?Why less?
Why less?
 
.Less - CSS done right
.Less - CSS done right.Less - CSS done right
.Less - CSS done right
 
Sass presentation
Sass presentationSass presentation
Sass presentation
 
Authoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & SassAuthoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & Sass
 
Responsive web design
Responsive web designResponsive web design
Responsive web design
 
SASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome StylesheetSASS - Syntactically Awesome Stylesheet
SASS - Syntactically Awesome Stylesheet
 
Cat stand up </div>
Cat stand up </div>Cat stand up </div>
Cat stand up </div>
 

Ähnlich wie From CSS to SASS in WordPress - A Guide to Converting Stylesheets

Create SASSy web parts in SPFx
Create SASSy web parts in SPFxCreate SASSy web parts in SPFx
Create SASSy web parts in SPFxStefan Bauer
 
SASS is more than LESS
SASS is more than LESSSASS is more than LESS
SASS is more than LESSItai Koren
 
SASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, GreensockSASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, GreensockMarco Pinheiro
 
CSS Workflow. Pre & Post
CSS Workflow. Pre & PostCSS Workflow. Pre & Post
CSS Workflow. Pre & PostAnton Dosov
 
Getting Started with Sass & Compass
Getting Started with Sass & CompassGetting Started with Sass & Compass
Getting Started with Sass & CompassRob Davarnia
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Tahmina Khatoon
 
LESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introductionLESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introductionrushi7567
 
Preprocessor presentation
Preprocessor presentationPreprocessor presentation
Preprocessor presentationMario Noble
 
Doing More With Less
Doing More With LessDoing More With Less
Doing More With LessDavid Engel
 
LESS : The dynamic stylesheet language
LESS : The dynamic stylesheet languageLESS : The dynamic stylesheet language
LESS : The dynamic stylesheet languageKatsunori Tanaka
 
Advanced Technology for Web Application Design
Advanced Technology for Web Application DesignAdvanced Technology for Web Application Design
Advanced Technology for Web Application DesignBryce Kerley
 
CSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & CompassCSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & CompassLucien Lee
 
CSS Preprocessors: LESS is more or look SASS-y trying
CSS Preprocessors: LESS is more or look SASS-y tryingCSS Preprocessors: LESS is more or look SASS-y trying
CSS Preprocessors: LESS is more or look SASS-y tryingJames Cryer
 
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}Eric Carlisle
 
Dallas Drupal Days 2012 - Introduction to less sass-compass
Dallas Drupal Days 2012  - Introduction to less sass-compassDallas Drupal Days 2012  - Introduction to less sass-compass
Dallas Drupal Days 2012 - Introduction to less sass-compassChris Lee
 

Ähnlich wie From CSS to SASS in WordPress - A Guide to Converting Stylesheets (20)

Create SASSy web parts in SPFx
Create SASSy web parts in SPFxCreate SASSy web parts in SPFx
Create SASSy web parts in SPFx
 
[Bauer] SASSy web parts with SPFX
[Bauer] SASSy web parts with SPFX[Bauer] SASSy web parts with SPFX
[Bauer] SASSy web parts with SPFX
 
SASS is more than LESS
SASS is more than LESSSASS is more than LESS
SASS is more than LESS
 
SASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, GreensockSASS, Compass, Gulp, Greensock
SASS, Compass, Gulp, Greensock
 
CSS Workflow. Pre & Post
CSS Workflow. Pre & PostCSS Workflow. Pre & Post
CSS Workflow. Pre & Post
 
Getting Started with Sass & Compass
Getting Started with Sass & CompassGetting Started with Sass & Compass
Getting Started with Sass & Compass
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)
 
LESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introductionLESS(CSS Pre Processor) introduction
LESS(CSS Pre Processor) introduction
 
Preprocessor presentation
Preprocessor presentationPreprocessor presentation
Preprocessor presentation
 
UNIT 3.ppt
UNIT 3.pptUNIT 3.ppt
UNIT 3.ppt
 
Doing More With Less
Doing More With LessDoing More With Less
Doing More With Less
 
LESS : The dynamic stylesheet language
LESS : The dynamic stylesheet languageLESS : The dynamic stylesheet language
LESS : The dynamic stylesheet language
 
Advanced Technology for Web Application Design
Advanced Technology for Web Application DesignAdvanced Technology for Web Application Design
Advanced Technology for Web Application Design
 
CSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & CompassCSS 開發加速指南-Sass & Compass
CSS 開發加速指南-Sass & Compass
 
CSS Preprocessors: LESS is more or look SASS-y trying
CSS Preprocessors: LESS is more or look SASS-y tryingCSS Preprocessors: LESS is more or look SASS-y trying
CSS Preprocessors: LESS is more or look SASS-y trying
 
CSS3
CSS3CSS3
CSS3
 
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
The New UI - Staying Strong with Flexbox, SASS, and {{Mustache.js}}
 
CSS Extenders
CSS ExtendersCSS Extenders
CSS Extenders
 
Advanced sass
Advanced sassAdvanced sass
Advanced sass
 
Dallas Drupal Days 2012 - Introduction to less sass-compass
Dallas Drupal Days 2012  - Introduction to less sass-compassDallas Drupal Days 2012  - Introduction to less sass-compass
Dallas Drupal Days 2012 - Introduction to less sass-compass
 

Kürzlich hochgeladen

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 

Kürzlich hochgeladen (20)

Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 

From CSS to SASS in WordPress - A Guide to Converting Stylesheets

  • 1. FROM CSS TO SASS IN WORDPRESS James Steinbach WP Summit 2014 @jdsteinbach #wpsummit . 1
  • 2. BENEFITS OF SASS •  Code organization (partials, nesting) •  Faster styling (mixins, functions) •  Scalable code (logic, variables) 2
  • 5. CONVERT A STYLESHEET TO SASS So where do we even start? 5
  • 6. THE BARE MINIMUM •  CSS is valid Sass: –  change style.css to style.scss –  edit –  compile back to style.css 6
  • 7. SETTING UP PARTIALS •  Copy each “section” of style.css to a separate .scss file in your /sass/ folder. •  Name the file after what it does with an underscore prefix. –  Example: styles relating to your navigation go to _navigation.scss •  Import that partial in your main style.scss file. –  @import 'navigation'; 7
  • 8. REFACTOR AS NECESSARY .header .logo { //styles } .header .tagline { //styles } .header .menu { //styles } .header { .logo { //styles } .tagline { //styles } .menu { //styles } } 8
  • 9. REFACTOR AS NECESSARY .menu a { -webkit-transition: color 0.2s; -moz-transition: color 0.2s; transition: color 0.2s; } .menu a { @include transition(color 0.2s); } 9
  • 10. COMPILE YOUR SASS FOR WP Now that we broke it down, how do we put it back together? 10
  • 11. WP REQUIREMENTS •  Theme root contains style.css with WP comments •  All Sass files in a subdirectory to keep theme root clean •  Ability to compress output for deployed code •  Avoid long, complex command line code 11
  • 12. CLI COMPILE METHODS •  Sass CLI –  Compile: sass style.scss style.css –  Watch: sass --watch style.scss style.css •  Compass CLI –  Watch: compass watch 12
  • 13. GUI COMPILE METHODS •  Codekit: incident57.com/codekit •  Koala: koala-app.com •  Scout: mhs.github.io/scout-app •  Compass.app: compass.kkbox.com 13
  • 14. COMPASS WATCH •  Easiest CLI syntax: compass watch •  Config.rb file for options: –  File path (keep style.css in the right place) –  Output expanded compressed 14
  • 15. MY COMPASS CONFIG.RB •  Lives in /theme/sass/ –  style.css goes up a level to theme root css_dir = '..' –  sass files live in same /sass/ directory as config.rb sass_dir = '' –  development setting output_style = :expanded –  deployment setting output_style = :compressed 15
  • 16. WP STYLE.CSS COMMENTS •  style.scss in /theme/sass/ •  Include '!' to preserve comments in :compressed output /*! Theme Name: Sassy Theme Theme URI: http://jamessteinbach.com/ Author: James Steinbach Author URI: http://jamessteinbach.com Description: From CSS to Sass */ @import 'variables'; @import 'bourbon/bourbon'; //etc all your other imports… 16
  • 17. ORGANIZE YOUR PARTIALS Can I make all these new files easier to maintain? 17
  • 18. CASCADES STILL MATTER •  Import your broadest styles first. •  Import your most specific styles last. 18
  • 19. USE FOLDERS •  Group your .scss partials into folders: –  Base (variables, mixins, reset, typography) –  Layout (grid, header, footer) –  Components (buttons, menus, forms, widgets) –  Pages (home, landing page, portfolio) –  3rd Party (plugins, vendors, etc) 19
  • 20. FULL DISCLOSURE •  My Sass folder structure is not usually this detailed. –  /bourbon/ –  /neat/ –  /underscores/ (blank theme styles) –  All my partials (from _variables.scss to _home.scss) 20
  • 21. TRY THESE SASS TOOLS Are there any other tricks for making this workflow better? 21
  • 22. BOURBON •  Advanced Measurement/Color Functions –  golden-ratio(16px, 1); returns 25.888px –  strip-units(12px); returns 12 –  tint(#330000, 50%); returns #997f7f; –  rem(24px); default $em-base: 16px, returns 1.5rem 22
  • 23. BOURBON •  Prefixing Mixins –  @include animation(); –  @include background-image(); –  @include columns(); –  @include flexbox(); –  @include transform(); –  @include font-feature-settings(); 23
  • 24. BOURBON NEAT $grid-columns: 12; $gutter: 1em; .container { @include outer-container(); } .grid-item { @include span-columns(3); } 24
  • 25. MY MIXINS: BREAKPOINTS $breakpoints: (small: 600px, large: 920px); @mixin breakpoint($name) { @if not index(map-keys($breakpoints), $name) { @warn "Invalid breakpoint '#{$name}'."; } @else { @media (min-width: map- get($breakpoints, $name)) { @content; } } } 25
  • 26. MY MIXINS: FONT SIZES $font-sizes: (sm: 14, p: 16, bq: 20, ssh: 24, sh: 30, h: 32, hero: 48); @each $label, $size in $font-sizes { %#{$label} { font-size: $size * 1px; font-size: rem($size); } .page-title { @extend %h; } 26
  • 27. MY MIXINS: ABS. POSITION @mixin abs($t,$r,$b,$l) { position: absolute; top: $t; right: $r; bottom: $b; left: $l; } 27
  • 28. MY MIXINS: FONT-SMOOTHING @mixin font-smoothing($val) { @if $val == 'antialiased' or $val == 'subpixel-antialiased' { -webkit-font-smoothing: $val; -moz-osx-font-smoothing: $val; } @else { @warn 'Invalid value.' } } 28
  • 29. RESOURCES Where do I get more information? 29
  • 30. ADDITIONAL READING •  “Compass Compiling and WordPress Themes,” Chris Coyier - css-tricks.com/compass-compiling-and- wordpress-themes/ •  “How to Use Sass with WordPress,” Andy Leverenz - elegantthemes.com/blog/tips-tricks/how-to-use-sass- with-wordpress-a-step-by-step-guide •  “Get Your Sass in Line,” Micah Godbolt - godbolt.me/ blog/get-your-sass-in-line.html •  “Architecture for a Sass Project,” Hugo Giraudel - sitepoint.com/architecture-sass-project/ 30
  • 31. DOCUMENTATION •  Sass: sass-lang.com •  Compass: compass-style.org •  Bourbon: bourbon.io/docs •  Bourbon Neat: neat.bourbon.io/docs •  SassMeister App: sassmeister.com 31
  • 32. JAMES STEINBACH Senior Front End Developer The Idea People in Charlotte, NC @jdsteinbach | jamessteinbach.com/blog 32