normal CSS Makes nesting simpler, easier to manage Makes implementation of CSS3 features easier/faster Makes maintenance easier down the road Enables us to be smart and reusable with our code Plus, it's neat!
i d t h : 8 0 % ; h e i g h t : 2 3 p x ; u l { l i s t - s t y l e - t y p e : n o n e ; } l i { f l o a t : l e f t ; a { f o n t - w e i g h t : b o l d ; } } }
w i d t h : 8 0 % ; h e i g h t : 2 3 p x ; } # n a v b a r u l { l i s t - s t y l e - t y p e : n o n e ; } # n a v b a r l i { f l o a t : l e f t ; } # n a v b a r l i a { f o n t - w e i g h t : b o l d ; }
to a SPACE (descendant selector). # s i t e C o n t a i n e r { . b l o c k { } } / * O u t p u t s t o . . . * / # s i t e C o n t a i n e r . b l o c k { }
the space. # s i t e C o n t a i n e r { & . b l o c k { } & : h o v e r { } } / * O u t p u t s t o . . . * / # s i t e C o n t a i n e r . b l o c k { } # s i t e C o n t a i n e r : h o v e r { }
selector. # s i t e C o n t a i n e r { c o l o r : r e d ; . b l u e & { c o l o r : b l u e ; } } / * O u t p u t s t o . . . * / # s i t e C o n t a i n e r { c o l o r : r e d ; } . b l u e # s i t e C o n t a i n e r { c o l o r : b l u e ; }
cases: But what if you want to use the variable as part of the property name? c o l o r : $ g r e e n ; / * d o e s n ' t w o r k * / b o r d e r - $ d i r e c t i o n : 1 p x s o l i d $ g r e e n ; / * d o e s w o r k * / b o r d e r - { $ d i r e c t i o n } : 1 p x s o l i d $ g r e e n ;
Partials usually begin with an underscore, e.g., _base.scss The underscore tells SASS not to compile them to their own stylesheets You can import libraries (like Compass) or your own partials Partials are awesome because they allow to you to split up large files into small pieces and then recombine later @ i m p o r t " p a r t i a l s / f i l e n a m e "
e t h e C o m p a s s f r a m e w o r k * / @ i m p o r t " c o m p a s s " ; / * i n c l u d e a f i l e i n s i d e y o u r f o l d e r , ' p a r t i a l s / _ c o l o r s . s c s s ' * / @ i m p o r t " p a r t i a l s / c o l o r s " ;
k h r e f = " / w e b / c s s - m i n / c o m m o n / c s s / h t m l 5 R e s e t . c s s " r e l = " s t y l e s h e e t " > @ i m p o r t " b l u e p r i n t / r e s e t " ;
u n d e d - c o r n e r { - w e b k i t - b o r d e r - r a d i u s : 4 p x ; - m o z - b o r d e r - r a d i u s : 4 p x ; - k h t m l - b o r d e r - r a d i u s : 4 p x ; b o r d e r - r a d i u s : 4 p x ; } . r o u n d e d - c o r n e r { @ i n c l u d e b o r d e r - r a d i u s ( 4 p x ) ; }
/ / g r i d s e t u p $ b l u e p r i n t - g r i d - c o l u m n s : 9 ; $ b l u e p r i n t - g r i d - m a r g i n : 2 0 p x ; $ b l u e p r i n t - g r i d - w i d t h : 9 0 p x ; # s i t e C o n t a i n e r { @ i n c l u d e c o n t a i n e r ; } # m a i n C o l u m n { @ i n c l u d e c o l u m n ( 6 ) ; } # r i g h t C o l u m n { @ i n c l u d e c o l u m n ( 3 ) ; @ i n c l u d e l a s t ; }
under images 2. Define spacing/orientation 3. Your sprite is generated at compile/build time @ i m p o r t " m y - i c o n s / * . p n g " ; . a c t i o n s { . n e w { @ i n c l u d e m y - i c o n s - s p r i t e ( n e w ) ; } . e d i t { @ i n c l u d e m y - i c o n s - s p r i t e ( e d i t ) ; } }
the image is in the sprite... you can call it by name You can get dimensions/position from mixins Code is more human-readable $ w i d t h : m y - i c o n s - s p r i t e - w i d t h ( n e w ) ;
size) Unusable for retina versions Assumes sprites will be on your local filesystem (vs. Interwoven) No compression If you update and forget to commit an image file, builds will fail
i n g r e e n - m e ( ) { b a c k g r o u n d - c o l o r : # 9 f 9 ; b o r d e r : 2 p x s o l i d # 0 9 6 ; } p { @ i n c l u d e g r e e n - m e ; }
passed into your mixin to change the output. @ m i x i n g r e e n - m e ( $ b o r d e r - s t y l e : s o l i d ) { b a c k g r o u n d - c o l o r : # 9 f 9 ; b o r d e r : 2 p x $ b o r d e r - s t y l e # 0 9 6 ; } p { @ i n c l u d e g r e e n - m e ( $ b o r d e r - s t y l e : d a s h e d ) ; }
(if/else, for loops) in mixins. @ m i x i n c o l o r - m e ( $ c o l o r : b l u e , $ b o r d e r - s t y l e : s o l i d ) { @ i f $ c o l o r = = b l u e { b a c k g r o u n d - c o l o r : # b 2 d e e d ; b o r d e r : 2 p x $ b o r d e r - s t y l e # 0 0 8 b b 4 ; } @ e l s e i f $ c o l o r = = g r e e n { b a c k g r o u n d - c o l o r : # 9 f 9 ; b o r d e r : 2 p x $ b o r d e r - s t y l e # 0 9 6 ; } } p { @ i n c l u d e c o l o r - m e ( $ c o l o r : g r e e n , $ b o r d e r - s t y l e : d a s h e d ) ; }
mixins should be used for shorter chunks of code Partials should be used when there's larger chunks The more mixins you add, the longer it takes to compile The more partials you have, the more confusing your file structure Balance it out somewhere in the middle.
logic to return a value (rather than a set of styles). / * C o m p a s s s p a n f u n c t i o n : r e t u r n s a w i d t h i n c o l u m n s * / @ f u n c t i o n s p a n ( $ n ) { @ r e t u r n $ b l u e p r i n t - g r i d - w i d t h * $ n + $ b l u e p r i n t - g r i d - m a r g i n * ( $ n - 1 ) ; } # m a i n C o l u m n { w i d t h : s p a n ( 6 ) ; }
usually output a property value or other piece of a CSS rule / * t h i s i s t h e c o l u m n m i x i n ( d o e s f l o a t i n g , m a r g i n a n d w i d t h ) * / # m a i n C o l u m n { @ i n c l u d e c o l u m n ( 6 ) ; } / * t h i s i s t h e s p a n f u n c t i o n ( j u s t r e t u r n s w i d t h ) * / # m a i n C o l u m n { w i d t h : s p a n ( 6 ) ; }
management enabled us to use cool CSS3 features with more confidence Grid layout for responsive design Creating responsive & non-responsive versions of stylesheets Reducing upkeep on image.css and secureimage.css Built a reusable style library using mixins
- r e s p o n s i v e . s c s s - - - / * m o b i l e l a y o u t * / @ m e d i a s c r e e n a n d ( m a x - w i d t h : 6 5 3 p x ) { @ i m p o r t " p a r t i a l s / n a r r o w / c o r e " ; } / * t a b l e t p o r t r a i t * / @ m e d i a s c r e e n a n d ( m i n - w i d t h : 6 5 4 p x ) { @ i m p o r t " p a r t i a l s / n o r m a l / c o r e " ; } / * W i d e s c r e e n l a y o u t * / @ m e d i a s c r e e n a n d ( m i n - w i d t h : 9 9 0 p x ) { @ i m p o r t " p a r t i a l s / w i d e / c o r e " ; } / / - - - - c o r e . s c s s - - - @ i m p o r t " p a r t i a l s / n a r r o w / c o r e " ; @ i m p o r t " p a r t i a l s / n o r m a l / c o r e " ; @ i m p o r t " p a r t i a l s / w i d e / c o r e " ;
e . s c s s - - - - $ s e c u r e : f a l s e ; @ i m p o r t " p a r t i a l s / i m a g e " ; / / - - - - s e c u r e i m a g e . s c s s - - - - $ s e c u r e : t r u e ; @ i m p o r t " p a r t i a l s / i m a g e " ; / / - - - - p a r t i a l s / i m a g e . s c s s - - - - $ i m a g e _ p a t h : " h t t p : / / i m a g e s . m e r e d i t h . c o m / p a r e n t s / " ; $ s e c u r e _ i m a g e _ p a t h : " h t t p s : / / s e c u r e . p a r e n t s . c o m / p a r e n t s / " ; @ i f $ s e c u r e { $ i m a g e _ p a t h : $ s e c u r e _ i m a g e _ p a t h ; } # h e a d e r { b a c k g r o u n d : u r l ( " # { $ i m a g e _ p a t h } i m a g e . p n g " ; }
m a g e . c s s - - - - * / # h e a d e r { b a c k g r o u n d : u r l ( " h t t p : / / i m a g e s . m e r e d i t h . c o m / i m a g e . p n g " ; } / * - - - - s e c u r e i m a g e . c s s - - - - * / # h e a d e r { b a c k g r o u n d : u r l ( " h t t p s : / / s e c u r e . p a r e n t s . c o m / i m a g e . p n g " ; }
e { @ i n c l u d e b l o c k - m o r e - l i n k ; } u l { @ i n c l u d e p i n k - b u l l e t s ; } . b l o g M o d u l e { @ i n c l u d e c o n t e n t - b o x ; } . u p { @ i n c l u d e t r i a n g l e ( $ d i r e c t i o n : u p , $ s i z e : 1 0 p x ) ; }