SlideShare ist ein Scribd-Unternehmen logo
1 von 35
Downloaden Sie, um offline zu lesen
Sass(SCSS)について
HTML5 勉強会@福岡 - 第16回




                      1
@kazu69

 paperboy&co.




                2
Today’s Agenda




• Sassとは?
• Sassの特徴
• Sassを使った例
• まとめ


                         3
Sass(SCSS)とは?




                4
Sass is

Sass - Syntactically awesome style sheet
Hampton Catlinが発案した
Rubyで書かれたCSS Preprocessorsの一
つ。
CSSファイルにするためにはコンパイル (.sass
や.scss を .css に変換) が必要。
HTMLのPreprocessorsにHamlから派生が
したものである。
Sass記法とSCSS(Sassy sass)記法がある。


                                           5
Sass is



  CSSを速く、きれいに

DRY(Don't Repeat Yourself) に

    書くことができる




                               6
Sass(SCSS)の特徴




                7
mixin
よく使うコードを再利用


//mixin                  .color1 {
@mixin color {             font-size: 1em;
  color: #F00;             color: #F00; }
}
.color1 {                .color2 {
  font-size: 1em;          font-size: .5em;
  @include color;          color: #F00; }
}
.color2 {
  font-size: .5em;
  @include color;
}




                                              8
extend
デザインを拡張する
//@extend                      .box,
.box {                         .box-red,
  display: inline-block;       .box-fixHeight {
  width: 20%;                    display: inline-block;
  height: 100%;                  width: 20%;
}                                height: 100%; }

.box-red {                     .box-red {
  @extend .box;                  background-color: #F00; }
  background-color: #F00;
}                              .box-fixHeight {
                                 border: 2px solid #CCC; }
.box-fixHeight {
  @extend .box;
  border: 2px solid #CCC;
}

        同じ作業をしないのでスピードアップ
                                                             9
functions
数値、文字列などで便利な関数が豊富
//function
body {
  width: percentage(100px/200px);
  opacity: rgba(#333, 0.2);
  color: lighten(#333, 2);
  &:before {
    content: quote(hogemoge);
  }
}



body {
  width: 50%;
  opacity: rgba(51, 51, 51, 0.2);
  color: #383838; }
  body:before {
    content: "hogemoge"; }


                                    10
custom function
なければ作ることもできる
//custom function
@function addSass($string) {
  @return 'Sass' + $string;
}

.sass {
  content: addSass('(Scss)');
}




.sass {
  content: "Sass(Scss)"; }



          案件に応じて柔軟な対応ができる

                                   11
interpolation
変数を柔軟に使うことができる
//interpolation
@mixin context($prop, $val) {
  #{$prop}: $val;
}
.class {
  @include context(color, black);
  @include context(width,100px);
}




.class {
  color: black;
  width: 100px; }




                                    12
iterator
for / while / each がつかえる
//@for                         .list-1 {
@for $i from 1 through 3 {       color: #363636; }
  .list-#{$i} {                .list-2 {
    color: lighten(#333, $i)     color: #383838; }
  }                            .list-3 {
}                                color: #3b3b3b; }



//@each                        .boo {
@each $key in boo,foo,moo {      background-image:
  .#{$key} {                   url("boo.png"); }
    background-image:          .foo {
url('#{$key}.png');              background-image:
  }                            url("foo.png"); }
}                              .moo {
                                 background-image:
                               url("moo.png"); }

                                                     13
iterator
for / while / each がつかえる
//@while                      .col-3 {
$i: 3;                          width: 0px; }
@while $i > 0 {               .col-2 {
  .col-#{$i} { width: 3px       width: 1px; }
- $i; }                       .col-1 {
  $i: $i - 1;                   width: 2px; }
}




                                                14
conditional
if~else がつかえる
//@if                       /* $i==1 */
$i: 1;                      .col-1 {
@if $i == 0 {                 color: blue; }
  .col-#{$i} {
      color: red;
  }
}
@else {
  .col-#{$i} {
      color: blue;
  }
}




                                               15
CSS3 prefix
面倒だったCSS3のprefixの処理も簡単にできる
                                  .style {
@mixin addPrefix($pref, $prop,
                                    @include addPrefix($p, $r, .2em);
$val:0) {
                                    @include addPrefix($p, $a, button);
  @if 1 < length($pref) {
                                  }
    @each $k in $pref {
       #{$k}#{$prop}: $val;
     }
  }
  @else {
    #{$pref}#{$prop}: $val;       .style {
  }                                 -webkit-border-radius: 0.2em;
}                                   -moz-border-radius: 0.2em;
                                    -ms-border-radius: 0.2em;
$p: -webkit- -moz- -ms- -o- '';     -o-border-radius: 0.2em;
$r: border-radius;                  border-radius: 0.2em;
$a: appearance;
                                    -webkit-appearance: button;
                                    -moz-appearance: button;
                                    -ms-appearance: button;
                                    -o-appearance: button;
                                    appearance: button; }


                                                                          16
debug , error
debugなど標準出力もできる
//@debug                        DEBUG: 2px
@debug 1px + 1px;
//@warn                         WARNING: 2pxになる
@warn ‘2pxになる’;
$x: 1px+1px;

正しい構文でないとエラーとなりコンパイルできない
body {                          error test.scss (Line 3: Invalid
    font-weight: bold           CSS after "    color": expected
                                ";", was ": #333;")
    color: #333;}


単位付きの計算で正確な値を返してくれる
p {                             p {
  width: 1cm + 8mm;}              width: 1.8cm; }
                                /* Lessだと9cmとなる */
単位ごとに計算できないときはエラーになる

                                                                   17
partial
サイトのパフォーマンスを最適化



 application.scss   _reset.scss   _global.scss   _layout.scss   _thema.scss



             @import “reset”,”global”,”layout”,”thema”




 application.css     reset.css     global.css     layout.css     thema.css



   複数のファイルを一つにまとめてコンパイル
                    HTTPリクエストを減らせる

                                                                              18
compile option
ファイルの編集を監視できる
                     ファイル単位


    hoge.scss                            hoge.css

         $sass --watch hoge.scss:hoge.css



                    ディレクトリ単位



      sass                                css

                $sass --watch sass:css


  ファイルが編集されると自動でコンパイル
                                                    19
minify


                   20KB                                                15KB
     $sass --style compressed style.scss:style.css
body{                                               body{background:#fff;font:62.5% palatino, "times
  background: #fff;                                 new roman", serif;color:#333}strong{font-
  font: 62.5% palatino, "times new roman", serif;   weight:bold}a:link,a:visited{color:#9C8A6A;text-
  color: #333;                                      decoration:none
}
strong{
  font-weight: bold;
}
a:link,
a:visited{
  color: #9C8A6A;
}
a:hover,
a:active{
  color: #3E372B;
}



       コードを圧縮(minify)してコンパイルできる
                          ファイルサイズを減らせる
                                                                                                       20
output style
その他の出力形式
$sass --style nested style.scss:style.css
#main {
  color: #fff;
  background-color: #000; }
  #main p {
    width: 10em; }



$sass --style expanded style.scss:style.css
#main {
  color: #fff;
  background-color: #000;
}
#main p {
  width: 10em;
}


$sass --style compact style.scss:style.css
#main { color: #fff; background-color: #000; }
#main p { width: 10em; }


                                                 21
convert




    style.css                  style.scss

   $ sass-convert style.css style.scss


     CSSからSassやSCSSに変換できる

運用中のサイトをSassに置き換えることもできるかも


                                            22
convert

                lessからSCSSに変換できる?



           style.less                              style.scss

           $ sass-convert syle.less style.scss

* NOTE: Sass and Less are different languages, and they work differently.
* I'll do my best to translate, but some features -- especially mixins --
* should be checked by hand.
undefined method `build' for module `Less::StyleSheet::Mixin4' (NameError)




                                                                             23
Fire-Sass
Firefoxには便利なアドオン




   $ sass —watch home.scss:test1.css —debug-info



                                                   24
compass
強力なCSSフレームワークがある




      便利なmixinが準備されている

      $gem install compass
      $compass create <myproject>
      $cd <myproject>
      $compass watch



                                    25
Sass3.2~
メディアクエリが使えるようになる
break-small: 320px;           profile-pic {
$break-large: 1200px;           float: left;
                                width: 250px;
.profile-pic {                }
  float: left;                @media screen and (max-
  width: 250px;               width: 320px) {
  @media screen and (max-       .profile-pic {
width: $break-small) {            width: 100px;
    width: 100px;                 float: none;
    float: none;                }
  }                           }
  @media screen and (min-     @media screen and (min-
width: $break-large) {        width: 1200px) {
    float: right;               .profile-pic {
  }                               float: right;
}                               }
                              }


                                                        26
Sass3.2~
placeholderも使える
$break-small: 320px;
@mixin respond-to($media) {
  @media only screen and (max-width: $break-small)
{ @content; }
}
.profile-pic {
  width: 250px;
  @include respond-to(handhelds) { width: 100%;}
}




.profile-pic {
  width: 250px;}
@media only screen and (max-width: 320px) {
  .profile-pic {
    width: 100%;}
}

                                                     27
Sass(SCSS)の弱点




                28
disadvantages


  コンパイルにはRuby環境が必要
      windowsはRuby installから‥


  ターミナル操作(黒い画面)が必要



GUIのアプリでターミナル操作は不要




                                29
Sass(SCSS)を使った例




                  30
choose reason Sass
Sassを選んだ理由

・CSSに近い感覚(記述)でつかえる

・できるだけ簡単に使える

・情報量が豊富で実績がある

・強力なフレームワークがある

・Lessよりも柔軟に使える

・コンパイルしたCSSをリリースするので安心




                             31
operation sample
                                                               _import.scss
                                   _import.scss                _color.scss
  sass                  core                        mixins
                                  配下のファイルを全て                   _font.scss
                                                                    ・
                                    importする                        ・
                                                                    ・
                                                               _page1.scss
                                                               _page2.scss
                                                   contents
                                                                   ・
             出力する    style.scss                                    ・
                                                                   ・
            ディレクトリ
  css                                                          _const.scss
                                                               _reset.scss
                                    コンパイルされた        globals
                                   ファイルをキャッシュ                  _layout.scss
                     .sass-cache
                                                                _nav.scss

style.css               +                                       _footer.scss
                                                  components    _btn.scss
                                                                _form.scss
                                                                   ・
                                                                   ・
                                                                   ・

                                                                               32
まとめ




      33
conclusion
・SCSS記法だと記法習得コストは高くない

・@mixinと@extendだけでも効率的になる

・GUIツールを使えばターミナルが苦手でも十分使える

・小規模な開発から導入してみるとわかりやすい
・運用しつつノウハウをためて、mixinなどを充実させると
 より効率的になる
・複数人で開発するときも役割分担しやすい
・IE6,7,8のセレクター上限数(selector limit4096)には注意

・Lessのように動的な変更ができない

・CSSの癖が出にくく、クオリティも一定に保つことができる
                                            34
Let’s Happy Sass Life




                        35

Weitere ähnliche Inhalte

Andere mochten auch

Introduction to SASS
Introduction to SASSIntroduction to SASS
Introduction to SASS
Jon Dean
 

Andere mochten auch (20)

Frontend-Entwicklung mit SASS & Compass
Frontend-Entwicklung mit SASS & CompassFrontend-Entwicklung mit SASS & Compass
Frontend-Entwicklung mit SASS & Compass
 
Save time by using SASS/SCSS
Save time by using SASS/SCSSSave time by using SASS/SCSS
Save time by using SASS/SCSS
 
Google App Engine and Social Apps
Google App Engine and Social AppsGoogle App Engine and Social Apps
Google App Engine and Social Apps
 
Running with emmet and scss
Running with emmet  and scssRunning with emmet  and scss
Running with emmet and scss
 
Cloud Computing Bootcamp On The Google App Engine [v1.1]
Cloud Computing Bootcamp On The Google App Engine [v1.1]Cloud Computing Bootcamp On The Google App Engine [v1.1]
Cloud Computing Bootcamp On The Google App Engine [v1.1]
 
Sassy Stylesheets with SCSS
Sassy Stylesheets with SCSSSassy Stylesheets with SCSS
Sassy Stylesheets with SCSS
 
Css to-scss
Css to-scssCss to-scss
Css to-scss
 
Scalable Apps with Google App Engine
Scalable Apps with Google App EngineScalable Apps with Google App Engine
Scalable Apps with Google App Engine
 
GDD Brazil 2010 - What's new in Google App Engine and Google App Engine For B...
GDD Brazil 2010 - What's new in Google App Engine and Google App Engine For B...GDD Brazil 2010 - What's new in Google App Engine and Google App Engine For B...
GDD Brazil 2010 - What's new in Google App Engine and Google App Engine For B...
 
Developing Java Web Applications In Google App Engine
Developing Java Web Applications In Google App EngineDeveloping Java Web Applications In Google App Engine
Developing Java Web Applications In Google App Engine
 
Turbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & CompassTurbo theming: Introduction to Sass & Compass
Turbo theming: Introduction to Sass & Compass
 
Theming and Sass
Theming and SassTheming and Sass
Theming and Sass
 
Front End Badassery with Sass
Front End Badassery with SassFront End Badassery with Sass
Front End Badassery with Sass
 
Stylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSSStylesheet Wrangling with SCSS
Stylesheet Wrangling with SCSS
 
Preprocessor presentation
Preprocessor presentationPreprocessor presentation
Preprocessor presentation
 
Performance front end language
Performance front end languagePerformance front end language
Performance front end language
 
Getting Started with Sass & Compass
Getting Started with Sass & CompassGetting Started with Sass & Compass
Getting Started with Sass & Compass
 
老成的Sass&Compass
老成的Sass&Compass老成的Sass&Compass
老成的Sass&Compass
 
Introduction to SASS
Introduction to SASSIntroduction to SASS
Introduction to SASS
 
Learn Sass and Compass quick
Learn Sass and Compass quickLearn Sass and Compass quick
Learn Sass and Compass quick
 

Ähnlich wie Sass(SCSS)について

Sass
SassSass
Sass
Su Ga
 
Css拡張言語のコトハジメ
Css拡張言語のコトハジメCss拡張言語のコトハジメ
Css拡張言語のコトハジメ
regret raym
 
WebデザイナーのためのSass/Compass入門
WebデザイナーのためのSass/Compass入門WebデザイナーのためのSass/Compass入門
WebデザイナーのためのSass/Compass入門
Koji Ishimoto
 
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
Yoshinori Kobayashi
 

Ähnlich wie Sass(SCSS)について (20)

Sass
SassSass
Sass
 
Less - first step
Less - first stepLess - first step
Less - first step
 
First sass
First sassFirst sass
First sass
 
Css preprocessorの始めかた
Css preprocessorの始めかたCss preprocessorの始めかた
Css preprocessorの始めかた
 
Css拡張言語のコトハジメ
Css拡張言語のコトハジメCss拡張言語のコトハジメ
Css拡張言語のコトハジメ
 
Sassをはじめからていねいに<概要−基礎編>
Sassをはじめからていねいに<概要−基礎編>Sassをはじめからていねいに<概要−基礎編>
Sassをはじめからていねいに<概要−基礎編>
 
compassで簡単! CSS3を手軽に利用する
compassで簡単!  CSS3を手軽に利用するcompassで簡単!  CSS3を手軽に利用する
compassで簡単! CSS3を手軽に利用する
 
SCSS + COMPASS 入門
SCSS + COMPASS 入門SCSS + COMPASS 入門
SCSS + COMPASS 入門
 
CSS3 Design Recipe
CSS3 Design RecipeCSS3 Design Recipe
CSS3 Design Recipe
 
HTML/CSSを効率的にする メタ言語とツールのアレコレ
HTML/CSSを効率的にする メタ言語とツールのアレコレHTML/CSSを効率的にする メタ言語とツールのアレコレ
HTML/CSSを効率的にする メタ言語とツールのアレコレ
 
イマドキのコーダー環境構築2016
イマドキのコーダー環境構築2016イマドキのコーダー環境構築2016
イマドキのコーダー環境構築2016
 
WebデザイナーのためのSass/Compass入門
WebデザイナーのためのSass/Compass入門WebデザイナーのためのSass/Compass入門
WebデザイナーのためのSass/Compass入門
 
gulp + sass で目指せ倍速コーディング(東区フロントエンド勉強会 2015年 第1回) 本編
gulp + sass で目指せ倍速コーディング(東区フロントエンド勉強会 2015年 第1回) 本編gulp + sass で目指せ倍速コーディング(東区フロントエンド勉強会 2015年 第1回) 本編
gulp + sass で目指せ倍速コーディング(東区フロントエンド勉強会 2015年 第1回) 本編
 
Sass/Compass講習会
Sass/Compass講習会Sass/Compass講習会
Sass/Compass講習会
 
React で CSS カプセル化の可能性を考える
React で CSS カプセル化の可能性を考えるReact で CSS カプセル化の可能性を考える
React で CSS カプセル化の可能性を考える
 
㉗HTML5+jQueryでお絵かき
㉗HTML5+jQueryでお絵かき㉗HTML5+jQueryでお絵かき
㉗HTML5+jQueryでお絵かき
 
CSS勉強会
CSS勉強会CSS勉強会
CSS勉強会
 
Rake
RakeRake
Rake
 
今日から使える! Sass/compass ゆるめ勉強会
今日から使える! Sass/compass ゆるめ勉強会今日から使える! Sass/compass ゆるめ勉強会
今日から使える! Sass/compass ゆるめ勉強会
 
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
Sassについてゼロから解説 -基本の理解だけでも効率アップ-|第14回ゼロから始めるWordPress勉強会
 

Sass(SCSS)について