SlideShare une entreprise Scribd logo
1  sur  21
SASS For The Win! An introduction to SASS Oct. 13, 2011 1 Created for Magma Rails 2011 - www.magmarails.com Slides posted at http://tinyurl.com/magma-haml-sass Sample code from this presentation can be found in the following sample app: https://github.com/jonathandean/SASS-and-HAML-FTW
What is SASS? Syntactically Awesome Stylesheets Smarter CSS Gives you variables and methods (mixins) for CSS Lets you nest declarations Provides selector inheritance Lets you do math with your variable values Works by compiling .sass or .scss files into normal valid .css Commonly used in Ruby on Rails applications but can be used in any web project Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 2
SASS and SCSS Two available syntaxes SASS SCSS HAML-style indentation No brackets or semi-colons, based on indentation Less characters to type Enforced conventions/neatness Semi-colon and bracket syntax Superset of normal CSS Normal CSS is also valid SCSS Newer and recommended Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 3
SASS and SCSS Two available syntaxes SASS SCSS $txt-size: 12px $txt-color: #333 $link-color: #999 #main font-size: $txt-size color: $txt-color a 	color: $link-color $txt-size: 12px; $txt-color: #333; $link-color: #999; #main{ 	font-size: $txt-size; 	color: $txt-color; a{ 		color: $link-color; 	} } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 4
SASS and SCSS Both syntaxes have the same functionality Both of the previous examples compile to: Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 5 #main{ 	font-size: 12px; 	color: #333333; } #main a{ 	color: #999999; } ,[object Object]
Note: Some examples compile using different formatting, I changed them for the slides for readability,[object Object]
Selector inheritance You can also extend other CSS declarations with @extend .error{ color: red; } .seriousError{ @extend .error; font-weight: bold; } Resulting CSS .error, .seriousError{ color: red; } .seriousError{ font-weight: bold; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 7
Mixins Mixins are sets of reusable styles, almost like methods in other languages @mixin awesome-text{ font-size: 24px; font-weight: bold; color: blue; } p{ @include awesome-text; } Resulting CSS p{ font-size: 24px; font-weight: bold; color: blue; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 8
Mixins with parameters Mixins can also take parameters Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 9 SCSS @mixin awesome-text($size){ font-size: $size; font-weight: bold; color: blue; } p{ @include awesome-text(24px); } li{ 	@include awesome-text(18px); } Resulting CSS p{ font-size: 24px; font-weight: bold; color: blue; } li{ font-size: 18px; font-weight: bold; color: blue; }
More advanced mixin example @mixin image-replace($image-url){ &, & a{ 		display: block; 		background: url($image-url) no-repeat; 	} 	a{ 		text-indent: -99999px; 		text-decoration: none; 	} } h1{ @include image-replace(“images/header.gif”); } Resulting CSS h1, h1 a{ 	display: block; background: url(“images/header.gif”) no-repeat; } h1 a{ text-indent: -99999px; text-decoration: none; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 10
Mathematic operations You can do simple math operations with your variable values, even if they have units Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 11 $page-width: 500px; $sidebar-width: 100px; $content-width: $page-width - $sidebar-width; #main{ 	width: $page-width; #sidebar{ 	width: $sidebar-width; } #content{ 	width: $content-width; } } Resulting CSS #main{ 	width: 500px; } #main #sidebar{ width: 100px; } #main #content{ 	width: 400px; }
Mathematic operations Supported operations: +, -, *, /, % The division operator (/) is also valid in normal CSS font: 10px/8px; // stays font: 10px/8px; So it is only used as division in three cases When one of the values is stored in a variable $content-width: 500px; width: $content-width/2; // becomes width: 250px; When surrounded by parenthesis width: (500px/2); // becomes width: 250px; When part of another math expression width: 10px + 500px/2; // becomes width: 260px; To use variables in the CSS version without doing math operations $some-val: 10px; $another-val: 8px; font: #{$some-val}/#{$another-val}; // font: 10px/8px; Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 12
Interpolation You can use variables in selectors and property declarations $class-name: wrapper; $attribute-name: font; div.#{$class-name}{ #{$attribute-name}-size: 12px; } Resulting CSS div.wrapper{ 	font-size: 12px; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 13 Warning: RubyMine may not recognize this syntax and highlight it as an error
Control directives Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 14 @if $type: big; p{ @if $type == big{ font-size: 24px; } } Resulting CSS p{ 	font-size: 24px; } @if / @else $type: small; p{ @if $type == big { font-size: 24px; } @else if $type == medium{ 	font-size: 18px; } @else { 	font-size: 16px; } } Resulting CSS p{ 	font-size: 16px; }
Control directives Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 15 Resulting CSS .item-1{ width: 10px; } .item-2{ width: 20px; } .item-3{ width: 30px; } @for @for $i from 1 through 3 { .item-#{$i} { width: 10px * $i;  } } @each 	@each $item in item1, item2{ 	.#{$item}{ 	width: 500px; 	} 	} .item1{ width: 500px; } .item2{ width: 500px; } @while 	$i: 6; 	@while $i > 0 { 	.item-#{$i} { 	width: 10px * $i; 	} 	$i: $i - 2; 	} .item-6{ width: 60px; } .item-4{ width: 40px; } .item-2{ width: 20px; }
Importing other SASS files Import other .sass or .scss files using @import @import “reset”; @import “reset.css.scss”; // File extension also allowed You can also create partials that will only be imported to other files and not compiled to .css files themselves Just name the partial with an underscore in the front, such as _sample.css.scss Now import the same way: @import “sample”; Handy for organizing styles into multiple files but compiling to only one file for use on the web Note: when using the Rails 3.1 asset pipeline, name your files with .css.scss or .css.sassextentions instead of just .scss or .sass Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 16
Nested properties Simplify the declaration of name-spaced CSS properties Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 17 Resulting CSS SCSS .sassy{ font:{ 	size: 12px; 	weight: bold; } } .sassy{ font-size: 12px; 	font-weight: bold; }
Color operations You can also do mathematic operations on color values color: #010203 + #040506; // color: #050709; Howthisiscomputed #010203 + #040506 = #050709 01 + 04 = 05 02 + 05 = 07 03 + 06 = 09 Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 18
Variable defaults Will only assign the variable if it hasn’t been defined yet Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 19 $page-color: #333; $page-color: #666 !default; $section-color: #999 !default; // won’t be assigned because $page-color has already been defined // will be assigned because $section-color hasn’t been defined yet Handy for when you import a partial in some files but not in all of them and want the value from the partial to take precedence if it has already been defined
Using SASS without Rails gem install sass sass --watch path/to/input.scss:path/to/output.css Sass will auto-compile to output.css each time input.css is modified Use it for any project you have CSS Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 20
Using SASS in a Rails application Rails 3.1 Included by default! Put your filename.css.scss files in app/assets/stylesheets/ The Asset Pipeline will deal with compiling them for you See the sample application! For older versions Add the following to your Gemfile gem “sass” You can put your sass files anywhere, but why not use the new convention introduced by 3.1 Use sass --watch in the command line or another gem such as Compass Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 21

Contenu connexe

Tendances

CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media QueriesRuss Weakley
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid LayoutRachel Andrew
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Tahmina Khatoon
 
CSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignCSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignZoe Gillenwater
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Chris Poteet
 
CSS For Backend Developers
CSS For Backend DevelopersCSS For Backend Developers
CSS For Backend Developers10Clouds
 
Intro to css & sass
Intro to css & sassIntro to css & sass
Intro to css & sassSean Wolfe
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overviewJacob Nelson
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksAmit Tyagi
 
Tailwind CSS - KanpurJS
Tailwind CSS - KanpurJSTailwind CSS - KanpurJS
Tailwind CSS - KanpurJSNaveen Kharwar
 
Cascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) helpCascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) helpcasestudyhelp
 
Front End Best Practices
Front End Best PracticesFront End Best Practices
Front End Best PracticesHolger Bartel
 

Tendances (20)

CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
Flexbox and Grid Layout
Flexbox and Grid LayoutFlexbox and Grid Layout
Flexbox and Grid Layout
 
Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)Syntactically awesome stylesheets (Sass)
Syntactically awesome stylesheets (Sass)
 
CSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive DesignCSS3, Media Queries, and Responsive Design
CSS3, Media Queries, and Responsive Design
 
Sass presentation
Sass presentationSass presentation
Sass presentation
 
CSS Best practice
CSS Best practiceCSS Best practice
CSS Best practice
 
Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)Introduction to Cascading Style Sheets (CSS)
Introduction to Cascading Style Sheets (CSS)
 
CSS For Backend Developers
CSS For Backend DevelopersCSS For Backend Developers
CSS For Backend Developers
 
Intro to css & sass
Intro to css & sassIntro to css & sass
Intro to css & sass
 
Html5 and-css3-overview
Html5 and-css3-overviewHtml5 and-css3-overview
Html5 and-css3-overview
 
How Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) WorksHow Cascading Style Sheets (CSS) Works
How Cascading Style Sheets (CSS) Works
 
Tailwind CSS - KanpurJS
Tailwind CSS - KanpurJSTailwind CSS - KanpurJS
Tailwind CSS - KanpurJS
 
CSS
CSSCSS
CSS
 
Cascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) helpCascading Style Sheets (CSS) help
Cascading Style Sheets (CSS) help
 
CSS
CSSCSS
CSS
 
CSS ppt
CSS pptCSS ppt
CSS ppt
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Introduction to CSS
Introduction to CSSIntroduction to CSS
Introduction to CSS
 
Flexbox
FlexboxFlexbox
Flexbox
 
Front End Best Practices
Front End Best PracticesFront End Best Practices
Front End Best Practices
 

En vedette

Learn Sass and Compass quick
Learn Sass and Compass quickLearn Sass and Compass quick
Learn Sass and Compass quickBilly Shih
 
Sass and compass workshop
Sass and compass workshopSass and compass workshop
Sass and compass workshopShaho Toofani
 
Responsive web design
Responsive web designResponsive web design
Responsive web designSean Wolfe
 
CSS3 Flex Layout
CSS3 Flex LayoutCSS3 Flex Layout
CSS3 Flex LayoutNeha Sharma
 
Front end basics - Sass
Front end basics - SassFront end basics - Sass
Front end basics - SassNadal Soler
 
Using scss-at-noisestreet
Using scss-at-noisestreetUsing scss-at-noisestreet
Using scss-at-noisestreetWei Pin Teo
 
CSS Best Practices
CSS Best PracticesCSS Best Practices
CSS Best Practicesnolly00
 
Getting Started With Sass | WC Philly 2015
Getting Started With Sass | WC Philly 2015Getting Started With Sass | WC Philly 2015
Getting Started With Sass | WC Philly 2015Maura Teal
 
ES2015 and Beyond
ES2015 and BeyondES2015 and Beyond
ES2015 and BeyondJay Phelps
 
Getting SASSy with front end development
Getting SASSy with front end developmentGetting SASSy with front end development
Getting SASSy with front end developmentMatthew Carleton
 
Front End Badassery with Sass
Front End Badassery with SassFront End Badassery with Sass
Front End Badassery with Sassjessabean
 
Turbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & CompassTurbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & CompassAlmog Baku
 
Stylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSSStylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSSsforst
 

En vedette (20)

Intro to SASS CSS
Intro to SASS CSSIntro to SASS CSS
Intro to SASS CSS
 
Sass
SassSass
Sass
 
Sass presentation
Sass presentationSass presentation
Sass presentation
 
Learn Sass and Compass quick
Learn Sass and Compass quickLearn Sass and Compass quick
Learn Sass and Compass quick
 
Sass and compass workshop
Sass and compass workshopSass and compass workshop
Sass and compass workshop
 
Less css
Less cssLess css
Less css
 
Responsive web design
Responsive web designResponsive web design
Responsive web design
 
CSS3 Flex Layout
CSS3 Flex LayoutCSS3 Flex Layout
CSS3 Flex Layout
 
Front end basics - Sass
Front end basics - SassFront end basics - Sass
Front end basics - Sass
 
Using scss-at-noisestreet
Using scss-at-noisestreetUsing scss-at-noisestreet
Using scss-at-noisestreet
 
CSS Best Practices
CSS Best PracticesCSS Best Practices
CSS Best Practices
 
Getting Started With Sass | WC Philly 2015
Getting Started With Sass | WC Philly 2015Getting Started With Sass | WC Philly 2015
Getting Started With Sass | WC Philly 2015
 
ES2015 and Beyond
ES2015 and BeyondES2015 and Beyond
ES2015 and Beyond
 
[Cordova] Empezando con Ionic
[Cordova] Empezando con Ionic[Cordova] Empezando con Ionic
[Cordova] Empezando con Ionic
 
Getting SASSy with front end development
Getting SASSy with front end developmentGetting SASSy with front end development
Getting SASSy with front end development
 
Sass: Introduction
Sass: IntroductionSass: Introduction
Sass: Introduction
 
Front End Badassery with Sass
Front End Badassery with SassFront End Badassery with Sass
Front End Badassery with Sass
 
Theming and Sass
Theming and SassTheming and Sass
Theming and Sass
 
Turbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & CompassTurbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & Compass
 
Stylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSSStylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSS
 

Similaire à Introduction to SASS

Pacific Northwest Drupal Summit: Basic & SASS
Pacific Northwest Drupal Summit: Basic & SASSPacific Northwest Drupal Summit: Basic & SASS
Pacific Northwest Drupal Summit: Basic & SASSSteve Krueger
 
An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)Folio3 Software
 
Modularization css with sass
Modularization css with sassModularization css with sass
Modularization css with sassHuiyi Yan
 
Elegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassElegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassMediacurrent
 
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
 
Fasten RWD Development with Sass
Fasten RWD Development with SassFasten RWD Development with Sass
Fasten RWD Development with SassSven Wolfermann
 
Simple introduction Sass
Simple introduction SassSimple introduction Sass
Simple introduction SassZeeshan Ahmed
 
CSS with LESS for #jd13nl
CSS with LESS for #jd13nlCSS with LESS for #jd13nl
CSS with LESS for #jd13nlHans Kuijpers
 
Authoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & SassAuthoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & Sasschriseppstein
 
CSS előfeldolgozók
CSS előfeldolgozókCSS előfeldolgozók
CSS előfeldolgozókMáté Farkas
 
CSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsCSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsSanjoy Kr. Paul
 
15- Learn CSS Fundamentals / Color
15- Learn CSS Fundamentals / Color15- Learn CSS Fundamentals / Color
15- Learn CSS Fundamentals / ColorIn a Rocket
 
SCSS Implementation
SCSS ImplementationSCSS Implementation
SCSS ImplementationAmey Parab
 
Sassy! Stylesheets with SCSS by Kathryn Rotondo
Sassy! Stylesheets with SCSS by Kathryn RotondoSassy! Stylesheets with SCSS by Kathryn Rotondo
Sassy! Stylesheets with SCSS by Kathryn RotondoCodemotion
 

Similaire à Introduction to SASS (20)

Pacific Northwest Drupal Summit: Basic & SASS
Pacific Northwest Drupal Summit: Basic & SASSPacific Northwest Drupal Summit: Basic & SASS
Pacific Northwest Drupal Summit: Basic & SASS
 
An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)An Introduction to CSS Preprocessors (SASS & LESS)
An Introduction to CSS Preprocessors (SASS & LESS)
 
Modularization css with sass
Modularization css with sassModularization css with sass
Modularization css with sass
 
Big Frontends Made Simple
Big Frontends Made SimpleBig Frontends Made Simple
Big Frontends Made Simple
 
Elegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassElegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs Sass
 
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}}
 
Fasten RWD Development with Sass
Fasten RWD Development with SassFasten RWD Development with Sass
Fasten RWD Development with Sass
 
Simple introduction Sass
Simple introduction SassSimple introduction Sass
Simple introduction Sass
 
Advanced sass
Advanced sassAdvanced sass
Advanced sass
 
CSS with LESS for #jd13nl
CSS with LESS for #jd13nlCSS with LESS for #jd13nl
CSS with LESS for #jd13nl
 
SASS Lecture
SASS Lecture SASS Lecture
SASS Lecture
 
UNIT 3.ppt
UNIT 3.pptUNIT 3.ppt
UNIT 3.ppt
 
Authoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & SassAuthoring Stylesheets with Compass & Sass
Authoring Stylesheets with Compass & Sass
 
CSS előfeldolgozók
CSS előfeldolgozókCSS előfeldolgozók
CSS előfeldolgozók
 
AAVN_Presentation_SASS.pptx
AAVN_Presentation_SASS.pptxAAVN_Presentation_SASS.pptx
AAVN_Presentation_SASS.pptx
 
CSS3
CSS3CSS3
CSS3
 
CSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and ConsCSS Less framework overview, Pros and Cons
CSS Less framework overview, Pros and Cons
 
15- Learn CSS Fundamentals / Color
15- Learn CSS Fundamentals / Color15- Learn CSS Fundamentals / Color
15- Learn CSS Fundamentals / Color
 
SCSS Implementation
SCSS ImplementationSCSS Implementation
SCSS Implementation
 
Sassy! Stylesheets with SCSS by Kathryn Rotondo
Sassy! Stylesheets with SCSS by Kathryn RotondoSassy! Stylesheets with SCSS by Kathryn Rotondo
Sassy! Stylesheets with SCSS by Kathryn Rotondo
 

Dernier

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Dernier (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Introduction to SASS

  • 1. SASS For The Win! An introduction to SASS Oct. 13, 2011 1 Created for Magma Rails 2011 - www.magmarails.com Slides posted at http://tinyurl.com/magma-haml-sass Sample code from this presentation can be found in the following sample app: https://github.com/jonathandean/SASS-and-HAML-FTW
  • 2. What is SASS? Syntactically Awesome Stylesheets Smarter CSS Gives you variables and methods (mixins) for CSS Lets you nest declarations Provides selector inheritance Lets you do math with your variable values Works by compiling .sass or .scss files into normal valid .css Commonly used in Ruby on Rails applications but can be used in any web project Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 2
  • 3. SASS and SCSS Two available syntaxes SASS SCSS HAML-style indentation No brackets or semi-colons, based on indentation Less characters to type Enforced conventions/neatness Semi-colon and bracket syntax Superset of normal CSS Normal CSS is also valid SCSS Newer and recommended Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 3
  • 4. SASS and SCSS Two available syntaxes SASS SCSS $txt-size: 12px $txt-color: #333 $link-color: #999 #main font-size: $txt-size color: $txt-color a color: $link-color $txt-size: 12px; $txt-color: #333; $link-color: #999; #main{ font-size: $txt-size; color: $txt-color; a{ color: $link-color; } } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 4
  • 5.
  • 6.
  • 7. Selector inheritance You can also extend other CSS declarations with @extend .error{ color: red; } .seriousError{ @extend .error; font-weight: bold; } Resulting CSS .error, .seriousError{ color: red; } .seriousError{ font-weight: bold; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 7
  • 8. Mixins Mixins are sets of reusable styles, almost like methods in other languages @mixin awesome-text{ font-size: 24px; font-weight: bold; color: blue; } p{ @include awesome-text; } Resulting CSS p{ font-size: 24px; font-weight: bold; color: blue; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 8
  • 9. Mixins with parameters Mixins can also take parameters Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 9 SCSS @mixin awesome-text($size){ font-size: $size; font-weight: bold; color: blue; } p{ @include awesome-text(24px); } li{ @include awesome-text(18px); } Resulting CSS p{ font-size: 24px; font-weight: bold; color: blue; } li{ font-size: 18px; font-weight: bold; color: blue; }
  • 10. More advanced mixin example @mixin image-replace($image-url){ &, & a{ display: block; background: url($image-url) no-repeat; } a{ text-indent: -99999px; text-decoration: none; } } h1{ @include image-replace(“images/header.gif”); } Resulting CSS h1, h1 a{ display: block; background: url(“images/header.gif”) no-repeat; } h1 a{ text-indent: -99999px; text-decoration: none; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 10
  • 11. Mathematic operations You can do simple math operations with your variable values, even if they have units Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 11 $page-width: 500px; $sidebar-width: 100px; $content-width: $page-width - $sidebar-width; #main{ width: $page-width; #sidebar{ width: $sidebar-width; } #content{ width: $content-width; } } Resulting CSS #main{ width: 500px; } #main #sidebar{ width: 100px; } #main #content{ width: 400px; }
  • 12. Mathematic operations Supported operations: +, -, *, /, % The division operator (/) is also valid in normal CSS font: 10px/8px; // stays font: 10px/8px; So it is only used as division in three cases When one of the values is stored in a variable $content-width: 500px; width: $content-width/2; // becomes width: 250px; When surrounded by parenthesis width: (500px/2); // becomes width: 250px; When part of another math expression width: 10px + 500px/2; // becomes width: 260px; To use variables in the CSS version without doing math operations $some-val: 10px; $another-val: 8px; font: #{$some-val}/#{$another-val}; // font: 10px/8px; Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 12
  • 13. Interpolation You can use variables in selectors and property declarations $class-name: wrapper; $attribute-name: font; div.#{$class-name}{ #{$attribute-name}-size: 12px; } Resulting CSS div.wrapper{ font-size: 12px; } Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 13 Warning: RubyMine may not recognize this syntax and highlight it as an error
  • 14. Control directives Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 14 @if $type: big; p{ @if $type == big{ font-size: 24px; } } Resulting CSS p{ font-size: 24px; } @if / @else $type: small; p{ @if $type == big { font-size: 24px; } @else if $type == medium{ font-size: 18px; } @else { font-size: 16px; } } Resulting CSS p{ font-size: 16px; }
  • 15. Control directives Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 15 Resulting CSS .item-1{ width: 10px; } .item-2{ width: 20px; } .item-3{ width: 30px; } @for @for $i from 1 through 3 { .item-#{$i} { width: 10px * $i; } } @each @each $item in item1, item2{ .#{$item}{ width: 500px; } } .item1{ width: 500px; } .item2{ width: 500px; } @while $i: 6; @while $i > 0 { .item-#{$i} { width: 10px * $i; } $i: $i - 2; } .item-6{ width: 60px; } .item-4{ width: 40px; } .item-2{ width: 20px; }
  • 16. Importing other SASS files Import other .sass or .scss files using @import @import “reset”; @import “reset.css.scss”; // File extension also allowed You can also create partials that will only be imported to other files and not compiled to .css files themselves Just name the partial with an underscore in the front, such as _sample.css.scss Now import the same way: @import “sample”; Handy for organizing styles into multiple files but compiling to only one file for use on the web Note: when using the Rails 3.1 asset pipeline, name your files with .css.scss or .css.sassextentions instead of just .scss or .sass Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 16
  • 17. Nested properties Simplify the declaration of name-spaced CSS properties Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 17 Resulting CSS SCSS .sassy{ font:{ size: 12px; weight: bold; } } .sassy{ font-size: 12px; font-weight: bold; }
  • 18. Color operations You can also do mathematic operations on color values color: #010203 + #040506; // color: #050709; Howthisiscomputed #010203 + #040506 = #050709 01 + 04 = 05 02 + 05 = 07 03 + 06 = 09 Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 18
  • 19. Variable defaults Will only assign the variable if it hasn’t been defined yet Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 19 $page-color: #333; $page-color: #666 !default; $section-color: #999 !default; // won’t be assigned because $page-color has already been defined // will be assigned because $section-color hasn’t been defined yet Handy for when you import a partial in some files but not in all of them and want the value from the partial to take precedence if it has already been defined
  • 20. Using SASS without Rails gem install sass sass --watch path/to/input.scss:path/to/output.css Sass will auto-compile to output.css each time input.css is modified Use it for any project you have CSS Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 20
  • 21. Using SASS in a Rails application Rails 3.1 Included by default! Put your filename.css.scss files in app/assets/stylesheets/ The Asset Pipeline will deal with compiling them for you See the sample application! For older versions Add the following to your Gemfile gem “sass” You can put your sass files anywhere, but why not use the new convention introduced by 3.1 Use sass --watch in the command line or another gem such as Compass Oct. 13, 2011 An Introduction to SASS by Jonathan Dean - www.jonathandean.com 21