Compass

最終更新:2025年4月24日

作成日:2016年5月30日

Compass の基本的な使い方

Compass は Sass のミックスインや関数など多数の機能を提供してくれるフレームワークです。Compass のサイトには「Compass is an open-source CSS Authoring Framework.」と書かれています。

Compass Official Site

Compass のインストールや設定に関しては検索するか「Sass/Compass のインストールと基本的な環境設定(2014年3月4日の記事なので少し古いですが)」をご参照ください。以下ではすでに Compass のインストール及び設定は完了していることを前提にしています。

現時点(2016年5月)での Compass の最新バージョンは、 Compass 1.0.3 (Polaris) です。コマンドプロンプトで「compass -v」とタイプすると確認できます。

  1. C:\Users\xxxx>compass -v
  2. Compass 1.0.3 (Polaris)
  3. Copyright (c) 2008-2016 Chris Eppstein
  4. Released under the MIT License.

以下は、フォルダ構成の一例です。

Compass Folders

以下は、config.rb の設定例です。

  1. http_path = "/"
  2. css_dir = "css"
  3. sass_dir = "sass"
  4. images_dir = "images"
  5. javascripts_dir = "js"
  6. output_style = :expanded //アウトプットスタイルの指定
  7. relative_assets = true //関数等で使うURLを相対パスに
  8. line_comments = false //Sass ファイルの行数などのコメントを書き出さない
  9. asset_cache_buster :none //画像のクエリ情報をつけない
  10. sourcemap = true //コンパイル時に Source Map の生成

以下は、compass_start.bat の設定例です。

  1. cd /d %~dp0 //このバッチファイルがある場所に移動
  2. compass w //自動監視スタート

Compass のインポート

Compass を利用するには、Compass をインポートする必要があります。

Compass をインポートするには、Sass ファイルに以下のように指定して Compass をインポートします。

  1. //SASS (.scss)
  2. @import "compass";

上記のように指定すると、以下の Compass のコアモジュールがインポートされ、ミックスイン等の機能が使えるようになります。

Compass のコアモジュール

  • CSS3 : ベンダープレフィックスやレガシーブラウザ対応のミックスインなど
  • Typography : テキストカラー、行間などのテキスト系ミックスイン
  • Utilities : 色や Clearfix などのミックスイン

モジュールを個別にインポートすることも可能です。

  1. //SASS (.scss)
  2. @import "compass/css3";
  3. @import "compass/typography";
  4. @import "compass/utilities";

以下の2つのモジュールはコアモジュールでインポートされないので、個別にインポート指定する必要があります。

  • Layout : レイアウト用のモジュール。グリッド背景、ストレッチ・レイアウトなど
  • Reset : リセット CSS のモジュール
  1. //SASS (.scss)
  2. @import "compass/layout";
  3. @import "compass/reset";

Compass のベンダープレフィックス

ベンダープレフィックスの出力を制御するには、いくつかの方法があります。

参考:
Tuning Vendor Prefixes from Compass Stylesheets
Compass Cross-Browser Support Configuration

$graceful-usage-threshold

$graceful-usage-threshold という設定変数があり、デフォルトの値は「0.1」に設定されています。これは、対象のブラウザのユーザーが 0.1% より少なくなると、そのベンダープレフィックスは削除されるようになっています。

この値を大きくすれば、ベンダープレフィックスが付かなくなり、値を小さくすればより多くのベンダープレフィックスが付くようになっています。

  1. //SASS (.scss)
  2. $graceful-usage-threshold: 0.001; //より多くのベンダープレフィックスが付く
  3. $graceful-usage-threshold: 99; //ベンダープレフィックスが付かなくなる

※但し、この設定変数は Compass のインポートの前に設定しておく必要があります。

  1. //SASS (.scss)
  2. $graceful-usage-threshold: 99;
  3. @import "compass";
または、「_setting.scss」などのパーシャルに設定変数を記述しておき、そのパーシャルの後に Compass をインポートします。
  1. //_setting.scss
  2. $graceful-usage-threshold: 5;
  1. //SASS (.scss)
  2. @import "setting";
  3. @import "compass";

モジュールごとの閾値

$graceful-usage-threshold:はシステム全体の閾値の設定で、モジュールごとに個別に閾値を設定できます。

例えば、border-radius() の場合は以下の設定変数で閾値を設定できます。

  1. //SASS (.scss)
  2. $border-radius-threshold: 0.01;
  3.  
  4. .foo {
  5. @include border-radius(5px);
  6. }

モジュールごとの閾値(例えば $border-radius-threshold)は基本的には $graceful-usage-threshold (の値)が設定されています。

個別にベンダープレフィックスを設定

with-prefix() を使うと個別にベンダープレフィックスを設定できます。

  1. //SASS (.scss)
  2. .foo a {
  3. @include with-prefix(-moz) {
  4. @include border-radius(4px);
  5. }
  6. }
  1. /* CSS コンパイル後*/
  2. .foo a {
  3. -moz-border-radius: 4px;
  4. border-radius: 4px;
  5. }

ブラウザサポートの停止

ブラウザサポートを停止するには、以下のように「$supported-browsers」に「reject(browsers())」を使って対象のブラウザを記述します。以下の例は全てのブラウザを対象にしているので、ベンダープレフィックスは出力されなくなります。

  1. //SASS (.scss)
  2. $supported-browsers: reject(browsers(),
  3. android,
  4. blackberry,
  5. chrome,
  6. android-chrome,
  7. firefox,
  8. android-firefox,
  9. ie,
  10. ie-mobile,
  11. safari,
  12. ios-safari,
  13. opera,
  14. opera-mini,
  15. opera-mobile
  16. );

$debug-browser-support

$debug-browser-support を「true」に指定すると、何故そのブラウザサポートがされているのか、またはされていないかの理由がコメントで表示されます。

  1. //SASS (.scss)
  2. $debug-browser-support: true;
  3.  
  4. .foo a {
  5. @include border-radius(4px);
  6. }
  1. /* CSS コンパイル後*/
  2. .foo a {
  3. /* Capability border-radius is prefixed with -moz because 0.25036% of users need it which is more than the threshold of 0.1%. */
  4. /* Creating new -moz context. */
  5. -moz-border-radius: 4px;
  6. /* Capability border-radius is not prefixed with -ms because 0% of users are affected which is less than the threshold of 0.1. */
  7. /* Capability border-radius is not prefixed with -o because 0% of users are affected which is less than the threshold of 0.1. */
  8. /* Capability border-radius is prefixed with -webkit because 0.1583% of users need it which is more than the threshold of 0.1%. */
  9. /* Creating new -webkit context. */
  10. -webkit-border-radius: 4px;
  11. border-radius: 4px;
  12. }

Compass のミックスイン

Compass には便利なミックスインが用意されています。

CSS3 モジュール

CSS3 モジュールは、ベンダープレフィックスや、ブラウザの下位バージョンに対応するプロパティを書き出してくれます。最近ではベンダープレフィックスはだんだん不要になってきているので、無駄なベンダープレフィックスは付けないようにした方が良いでしょう。

以下は、CSS3 モジュールの中のいくつかのミックスインの使用例です。ほとんどのミックスインではパラメータ(引数)に、通常の CSS3 のプロパティの値を指定することができます。

ボックスシャドウ

パラメータは、CSS3 のプロパティの値の書式で指定します。

  1. /* CSS */
  2. box-shadow: 横オフセット 縦オフセット ブラー スプレッド insetキーワード ;

以下は box-shadow() の使用例です。

  1. //SASS (.scss)
  2. .foo {
  3. @include box-shadow();
  4. }
  5.  
  6. .bar {
  7. @include box-shadow(2px 2px 10px red);
  8. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. -moz-box-shadow: 0px 0px 5px #333333;
  4. -webkit-box-shadow: 0px 0px 5px #333333;
  5. box-shadow: 0px 0px 5px #333333;
  6. }
  7.  
  8. .bar {
  9. -moz-box-shadow: 2px 2px 10px red;
  10. -webkit-box-shadow: 2px 2px 10px red;
  11. box-shadow: 2px 2px 10px red;
  12. }

パラメータを指定しない場合は、以下のデフォルトの値が適用されます。

設定変数 意味 デフォルト値
$default-box-shadow-h-offset 横オフセット 0px
$default-box-shadow-v-offset 縦オフセット 0px
$default-box-shadow-blur ブラー 5px
$default-box-shadow-spread スプレッド null
$default-box-shadow-color #333333
$default-box-shadow-inset inset キーワード null

上記の設定変数の値を変更すれば、デフォルトの値を変更することができます。

  1. //SASS (.scss)
  2. $default-box-shadow-h-offset : 2px;
  3. $default-box-shadow-v-offset : 3px;
  4. $default-box-shadow-blur : 5px;
  5. $default-box-shadow-spread : 2px;
  6. $default-box-shadow-color : rgba(#6C7AA6, .3);
  7. $default-box-shadow-inset : null;
  8.  
  9. %box-shadow {
  10. @include box-shadow();
  11. }
  12.  
  13. .foo {
  14. @extend %box-shadow;
  15. }
  16.  
  17. .bar {
  18. @extend %box-shadow;
  19. }
  1. /* CSS コンパイル後*/
  2. .foo, .bar {
  3. -moz-box-shadow: 2px 3px 5px 2px rgba(108, 122, 166, 0.3);
  4. -webkit-box-shadow: 2px 3px 5px 2px rgba(108, 122, 166, 0.3);
  5. box-shadow: 2px 3px 5px 2px rgba(108, 122, 166, 0.3);
  6. }

Sass の rgba($color, $alpha) 関数なども使えます。また、CSS3 同様に複数の影も指定できます。

  1. //SASS (.scss)
  2. .foo {
  3. @include box-shadow(0 2px 4px rgba(#5540CC, .8));
  4. }
  5.  
  6. .bar {
  7. @include box-shadow(0 2px 4px 1px rgba(#000, .3), 5px 5px 10px 3px rgba(orange ,0.1) inset);
  8. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. -moz-box-shadow: 0 2px 4px rgba(85, 64, 204, 0.8);
  4. -webkit-box-shadow: 0 2px 4px rgba(85, 64, 204, 0.8);
  5. box-shadow: 0 2px 4px rgba(85, 64, 204, 0.8);
  6. }
  7.  
  8. .bar {
  9. -moz-box-shadow: 0 2px 4px 1px rgba(0, 0, 0, 0.3), 5px 5px 10px 3px rgba(255, 165, 0, 0.1) inset;
  10. -webkit-box-shadow: 0 2px 4px 1px rgba(0, 0, 0, 0.3), 5px 5px 10px 3px rgba(255, 165, 0, 0.1) inset;
  11. box-shadow: 0 2px 4px 1px rgba(0, 0, 0, 0.3), 5px 5px 10px 3px rgba(255, 165, 0, 0.1) inset;
  12. }

box-shadow() のサンプル

グラデーション

グラデーションは、Compass Images(background または background-image)の ミックスインを利用します。

  1. //SASS (.scss)
  2. @include background(linear-gradient(値));
  3. @include background(radial-gradient(値));
  4.  
  5. @include background-image(linear-gradient(値));
  6. @include background-image(radial-gradient(値));

グラデーションのベンダープレフィックスの出力は $gradient-support-threshold 設定変数の値を調整することで制御できます。デフォルトの値は「0.1」で、値を大きくすると出力されるベンダープレフィックスは少なくなります。

SVG の背景画像の書き出しの制御は、$svg-gradient-shim-threshold 設定変数の値を調整することで制御できます。

グラデーションのサンプル

線形グラデーション linear-gradient()

パラメータは、CSS3 のプロパティの値の書式で指定します。

  1. /* CSS */
  2. linear-gradient( 方向または角度, 開始色 位置, 途中色 位置, 終了色 位置);

以下は linear-gradient() の使用例です。

  1. //SASS (.scss)
  2. .foo {
  3. @include background-image(linear-gradient(to top, #333, #999));
  4. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background-image: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PGxpbmVhckdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJvYmplY3RCb3VuZGluZ0JveCIgeDE9IjAuNSIgeTE9IjEuMCIgeDI9IjAuNSIgeTI9IjAuMCI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iIzMzMzMzMyIvPjxzdG9wIG9mZnNldD0iMTAwJSIgc3RvcC1jb2xvcj0iIzk5OTk5OSIvPjwvbGluZWFyR3JhZGllbnQ+PC9kZWZzPjxyZWN0IHg9IjAiIHk9IjAiIHdpZHRoPSIxMDAlIiBoZWlnaHQ9IjEwMCUiIGZpbGw9InVybCgjZ3JhZCkiIC8+PC9zdmc+IA==');
  4. background-size: 100%;
  5. background-image: -webkit-gradient(linear, 50% 100%, 50% 0%, color-stop(0%, #333333), color-stop(100%, #999999));
  6. background-image: -moz-linear-gradient(bottom, #333333, #999999);
  7. background-image: -webkit-linear-gradient(bottom, #333333, #999999);
  8. background-image: linear-gradient(to top, #333333, #999999);
  9. }

コンパイル後の CSS を見ると SVG の背景画像の書き出しや、ベンダープレフィックスが出力されているのがわかります。また、方向に to を使わない古い書き方をすると現在の書式に自動的に変換してくれます。

SVG の背景画像の書き出しをしないようにするには、$svg-gradient-shim-threshold にある程度以上の数値を指定します。デフォルトは、$graceful-usage-threshold の値(0.1)です。

または、$graceful-usage-threshold の値を調整します。その場合は、他のベンダープレフィックスの出力も影響を受けます。

  1. //SASS (.scss)
  2. $svg-gradient-shim-threshold: 3;
  3.  
  4. .foo {
  5. @include background-image(linear-gradient(#333, #999));
  6. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background-image: -webkit-gradient(linear, 50% 0%, 50% 100%, color-stop(0%, #333333), color-stop(100%, #999999));
  4. background-image: -moz-linear-gradient(#333333, #999999);
  5. background-image: -webkit-linear-gradient(#333333, #999999);
  6. background-image: linear-gradient(#333333, #999999);
  7. }

以下は、カラーヘルパー関数 adjust-lightness() を利用したミックスインの例です。このミックスインは色と明るさの割合を指定すると線形グラデーションを作成します。

また、$gradient-support-threshold の値を変更してベンダープレフィックスの出力を制御しています。

  1. //SASS (.scss)
  2. $svg-gradient-shim-threshold: 3;
  3. $gradient-support-threshold: 10;
  4.  
  5. @mixin linearGradient($color, $amount: 20%, $direction:to bottom) {
  6. @if unitless($amount) {
  7. $amount: 1% * $amount;
  8. }
  9. @include background(linear-gradient($direction, $color, adjust-lightness($color, $amount)));
  10. }
  11.  
  12. .foo {
  13. @include linearGradient(#729a7f);
  14. }
  15.  
  16. .bar {
  17. @include linearGradient(#999, -30%, to left);
  18. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background: -webkit-linear-gradient(top, #729a7f, #adc5b5);
  4. background: linear-gradient(to bottom, #729a7f, #adc5b5);
  5. }
  6.  
  7. .bar {
  8. background: -webkit-linear-gradient(right, #999999, #4d4d4d);
  9. background: linear-gradient(to left, #999999, #4d4d4d);
  10. }
foo
bar

円形グラデーション radial-gradient()

パラメータは、CSS3 のプロパティの値の書式で指定します。

  1. /* CSS */
  2. radial-gradient( 形状 サイズ 中心の位置, 開始色 位置, 途中色 位置, 終了色 位置);

以下は radial-gradient() の使用例です。

  1. //SASS (.scss)
  2. .baz {
  3. @include background(radial-gradient(#B9F1FB 0%, #39A5C0 50%, #057293 100%));
  4. }
  1. /* CSS コンパイル後*/
  2. .baz {
  3. background: url('data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4gPHN2ZyB2ZXJzaW9uPSIxLjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+PGRlZnM+PHJhZGlhbEdyYWRpZW50IGlkPSJncmFkIiBncmFkaWVudFVuaXRzPSJ1c2VyU3BhY2VPblVzZSIgY3g9IjUwJSIgY3k9IjUwJSIgcj0iMTAwJSI+PHN0b3Agb2Zmc2V0PSIwJSIgc3RvcC1jb2xvcj0iI2I5ZjFmYiIvPjxzdG9wIG9mZnNldD0iNTAlIiBzdG9wLWNvbG9yPSIjMzlhNWMwIi8+PHN0b3Agb2Zmc2V0PSIxMDAlIiBzdG9wLWNvbG9yPSIjMDU3MjkzIi8+PC9yYWRpYWxHcmFkaWVudD48L2RlZnM+PHJlY3QgeD0iMCIgeT0iMCIgd2lkdGg9IjEwMCUiIGhlaWdodD0iMTAwJSIgZmlsbD0idXJsKCNncmFkKSIgLz48L3N2Zz4g');
  4. background: -moz-radial-gradient(#b9f1fb 0%, #39a5c0 50%, #057293 100%);
  5. background: -webkit-radial-gradient(#b9f1fb 0%, #39a5c0 50%, #057293 100%);
  6. background: radial-gradient(#b9f1fb 0%, #39a5c0 50%, #057293 100%);
  7. }
baz
変形処理(トランスフォーム)

以下は、CSS3 の transform プロパティの書式です。

  1. /* CSS3 */
  2. transform: トランスフォーム関数();

パラメータは、CSS3 のプロパティの値の書式で指定します。

以下は rotate() 関数の使用例です。

  1. //SASS (.scss)
  2. .foo {
  3. @include rotate(15deg);
  4. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. -moz-transform: rotate(15deg);
  4. -ms-transform: rotate(15deg);
  5. -webkit-transform: rotate(15deg);
  6. transform: rotate(15deg);
  7. }

以下は、transform() の設定変数とその初期値(デフォルト値)です。

設定変数 意味 デフォルト値
$transform-support-threshold ベンダープレフィックスの制御 $graceful-usage-threshold
$default-origin-x transform-origin の x-offset 50%
$default-origin-y transform-origin の y-offset 50%
$default-origin-z transform-origin の z-offset 50%
$default-scale-x X 軸方向の伸縮する比率 1.25
$default-scale-y Y 軸方向の伸縮する比率 $default-scale-x
$default-scale-z Z 軸方向の伸縮する比率 $default-scale-x
$default-rotate 回転角度 45deg
$default-translate-x X 軸方向の移動距離 1em
$default-translate-y Y 軸方向の移動距離 $default-translate-x
$default-translate-z Z 軸方向の移動距離 $default-translate-x
$default-skew-x X 軸の傾斜角度 5deg
$default-skew-y Y 軸の傾斜角度 5deg

以下は、transform() の使用例です。

  1. //SASS (.scss)
  2. .foo {
  3. @include skew(-20deg, -20deg);
  4. }
  5.  
  6. .bar {
  7. @include scale(2);
  8. }
  9.  
  10. .baz {
  11. @include translate(20px, 40px);
  12. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. -moz-transform: skew(-20deg, -20deg);
  4. -ms-transform: skew(-20deg, -20deg);
  5. -webkit-transform: skew(-20deg, -20deg);
  6. transform: skew(-20deg, -20deg);
  7. }
  8.  
  9. .bar {
  10. -moz-transform: scale(2, 2);
  11. -ms-transform: scale(2, 2);
  12. -webkit-transform: scale(2, 2);
  13. transform: scale(2, 2);
  14. }
  15.  
  16. .baz {
  17. -moz-transform: translate(20px, 40px);
  18. -ms-transform: translate(20px, 40px);
  19. -webkit-transform: translate(20px, 40px);
  20. transform: translate(20px, 40px);
  21. }
トランジション transition

Compass Transition

Demo: Transition

以下は、transition() の設定変数とその初期値(デフォルト値)です。

設定変数 意味 デフォルト値
$default-transition-property 適用する対象の CSS プロパティ all
$default-transition-duration アニメーションの継続時間 1s
$default-transition-function イージング false
$default-transition-delay 遅延時間 false

Timing-function ($default-transition-function) と delay はブラウザのデフォルトの値を使用するように false に設定されています (ease, 0s)。

参考:CSS3 transition

以下は、transition を使ったロールオーバーアニメーションの例です。パラメータに何も指定していないので、デフォルトの値が適用されます。

  1. //SASS (.scss)
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. background: #F8DDDD;
  6. margin: 20px;
  7. @include transition();
  8. }
  9.  
  10. .foo:hover {
  11. width: 240px;
  12. background: #F94D50;
  13. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. background: #F8DDDD;
  6. margin: 20px;
  7. -moz-transition: all 1s;
  8. -o-transition: all 1s;
  9. -webkit-transition: all 1s;
  10. transition: all 1s;
  11. }
  12.  
  13. .foo:hover {
  14. width: 240px;
  15. background: #F94D50;
  16. }

transition のプロパティは個別に指定することも、ショートハンドでまとめて指定することもできます。

  1. //SASS (.scss)
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. line-height: 80px;
  6. text-align: center;
  7. margin: 50px 0;
  8. border: 1px solid #BBB232;
  9. background-color: #F7ED91;
  10. color: #ECB94C;
  11. @include transition-property(transform, color);
  12. @include transition-duration(0.5s);
  13. }
  14.  
  15. .foo:hover {
  16. color: #E47609;
  17. @include rotate(360deg);
  18. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. line-height: 80px;
  6. text-align: center;
  7. margin: 50px 0;
  8. border: 1px solid #BBB232;
  9. background-color: #F7ED91;
  10. color: #ECB94C;
  11. -moz-transition-property: -moz-transform, color;
  12. -o-transition-property: -o-transform, color;
  13. -webkit-transition-property: -webkit-transform, color;
  14. transition-property: transform, color;
  15. -moz-transition-duration: 0.5s;
  16. -o-transition-duration: 0.5s;
  17. -webkit-transition-duration: 0.5s;
  18. transition-duration: 0.5s;
  19. }
  20.  
  21. .foo:hover {
  22. color: #E47609;
  23. -moz-transform: rotate(360deg);
  24. -ms-transform: rotate(360deg);
  25. -webkit-transform: rotate(360deg);
  26. transform: rotate(360deg);
  27. }
foo

以下はショートハンドで指定する例です。また、transform-origin: right; で回転軸の位置を指定しています。

  1. //SASS (.scss)
  2. .foo{
  3. margin: 30px 0;
  4. width: 80px;
  5. height: 80px;
  6. line-height: 80px;
  7. text-align: center;
  8. color: #2A94B9;
  9. border: 1px solid #317AAF;
  10. background-color: #B9DFFB;
  11. @include transition(transform 1s, color 0.8s);
  12. transform-origin: right;
  13. }
  14.  
  15. .foo:hover {
  16. color: #E47609;
  17. @include rotateY(180deg);
  18. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. margin: 30px 0;
  4. width: 80px;
  5. height: 80px;
  6. line-height: 80px;
  7. text-align: center;
  8. color: #2A94B9;
  9. border: 1px solid #317AAF;
  10. background-color: #B9DFFB;
  11. -moz-transition: -moz-transform 1s, color 0.8s;
  12. -o-transition: -o-transform 1s, color 0.8s;
  13. -webkit-transition: -webkit-transform 1s, color 0.8s;
  14. transition: transform 1s, color 0.8s;
  15. transform-origin: right;
  16. }
  17.  
  18. .foo:hover {
  19. color: #E47609;
  20. -moz-transform: rotateY(180deg);
  21. -webkit-transform: rotateY(180deg);
  22. transform: rotateY(180deg);
  23. }
foo
イージング Timing-function

transition や animation では、イージングを指定することができます。標準で指定できるのは linear, ease (デフォルト), ease-in, ease-out, ease-in-out の5種類ですが、以下のサイトでいろいろなイージングの中から好きなものを選択して、cubic-bezier 関数の値を取得することができます。

http://easings.net/ja

また、Sass では変数が使えるのでこれらの値を変数に格納してパーシャルファイルに記述しておきインポートして利用すると便利です。以下の値は上記のサイトを参考にしました。

  1. //SASS (.scss)
  2. //timing-function/Easing
  3. $easeInSine: cubic-bezier(0.47, 0, 0.745, 0.715);
  4. $easeOutSine: cubic-bezier(0.39, 0.575, 0.565, 1);
  5. $easeInOutSine: cubic-bezier(0.445, 0.05, 0.55, 0.95);
  6. $easeInQuad: cubic-bezier(0.55, 0.085, 0.68, 0.53);
  7. $easeOutQuad: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  8. $easeInOutQuad: cubic-bezier(0.455, 0.03, 0.515, 0.955);
  9. $easeInCubic: cubic-bezier(0.55, 0.055, 0.675, 0.19);
  10. $easeOutCubic: cubic-bezier(0.215, 0.61, 0.355, 1);
  11. $easeInOutCubic: cubic-bezier(0.645, 0.045, 0.355, 1);
  12. $easeInQuart: cubic-bezier(0.895, 0.03, 0.685, 0.22);
  13. $easeOutQuart: cubic-bezier(0.165, 0.84, 0.44, 1);
  14. $easeInOutQuart: cubic-bezier(0.77, 0, 0.175, 1);
  15. $easeInQuint: cubic-bezier(0.755, 0.05, 0.855, 0.06);
  16. $easeOutQuint: cubic-bezier(0.23, 1, 0.32, 1);
  17. $easeInOutQuint: cubic-bezier(0.86, 0, 0.07, 1);
  18. $easeInExpo: cubic-bezier(0.95, 0.05, 0.795, 0.035);
  19. $easeOutExpo: cubic-bezier(0.19, 1, 0.22, 1);
  20. $easeInOutExpo: cubic-bezier(1, 0, 0, 1);
  21. $easeInCirc: cubic-bezier(0.6, 0.04, 0.98, 0.335);
  22. $easeOutCirc: cubic-bezier(0.075, 0.82, 0.165, 1);
  23. $easeInOutCirc: cubic-bezier(0.785, 0.135, 0.15, 0.86);
  24. $easeInBack: cubic-bezier(0.6, -0.28, 0.735, 0.045);
  25. $easeOutBack: cubic-bezier(0.175, 0.885, 0.32, 1.275);
  26. $easeInOutBack: cubic-bezier(0.68, -0.55, 0.265, 1.55);

以下は、上記変数($easeInOutBack)を使ってイージングを指定した例です。

  1. //SASS (.scss)
  2. .foo{
  3. margin: 30px 0;
  4. width: 80px;
  5. height: 80px;
  6. line-height: 80px;
  7. text-align: center;
  8. background-color: #BBF3D2;
  9. border: 1px solid #7FC392;
  10. color: #43825E;
  11. @include transition(transform 0.7s $easeInOutBack);
  12. }
  13.  
  14. .foo:hover {
  15. @include translate(200px, 0);
  16. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. margin: 30px 0;
  4. width: 80px;
  5. height: 80px;
  6. line-height: 80px;
  7. text-align: center;
  8. background-color: #BBF3D2;
  9. border: 1px solid #7FC392;
  10. color: #43825E;
  11. -moz-transition: -moz-transform 0.7s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  12. -o-transition: -o-transform 0.7s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  13. -webkit-transition: -webkit-transform 0.7s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  14. transition: transform 0.7s cubic-bezier(0.68, -0.55, 0.265, 1.55);
  15. }
  16.  
  17. .foo:hover {
  18. -moz-transform: translate(200px, 0);
  19. -ms-transform: translate(200px, 0);
  20. -webkit-transform: translate(200px, 0);
  21. transform: translate(200px, 0);
  22. }
foo
アニメーション animation

Compass Animation

参考:CSS3 animation

Compass で animation を設定するには、@include animation(animation プロパティ) と @include keyframes(キーフレーム名) のミックスインを使用します。

以下は、背景色を変化させる例です。

  1. //SASS (.scss)
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. margin: 20px;
  6. background: #F8C6C7;
  7. border-radius: 40px;
  8. @include animation(animation01 3s infinite);
  9. }
  10.  
  11. @include keyframes(animation01) {
  12. 0% {
  13. background-color: #F8C6C7;
  14. }
  15. 25% {
  16. background-color: #98D0F4;
  17. }
  18. 50% {
  19. background-color: #B9F0B9;
  20. }
  21. 75% {
  22. background-color: #F9F9C5;
  23. }
  24. 100% {
  25. background-color: #F8C6C7;
  26. }
  27. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. margin: 20px;
  6. background: #F8C6C7;
  7. border-radius: 40px;
  8. -moz-animation: animation01 3s infinite;
  9. -webkit-animation: animation01 3s infinite;
  10. animation: animation01 3s infinite;
  11. }
  12.  
  13. @-moz-keyframes animation01 {
  14. 0% {
  15. background-color: #F8C6C7;
  16. }
  17. 25% {
  18. background-color: #98D0F4;
  19. }
  20. 50% {
  21. background-color: #B9F0B9;
  22. }
  23. 75% {
  24. background-color: #F9F9C5;
  25. }
  26. 100% {
  27. background-color: #F8C6C7;
  28. }
  29. }
  30. @-webkit-keyframes animation01 {
  31. 0% {
  32. background-color: #F8C6C7;
  33. }
  34. 25% {
  35. background-color: #98D0F4;
  36. }
  37. 50% {
  38. background-color: #B9F0B9;
  39. }
  40. 75% {
  41. background-color: #F9F9C5;
  42. }
  43. 100% {
  44. background-color: #F8C6C7;
  45. }
  46. }
  47. @keyframes animation01 {
  48. 0% {
  49. background-color: #F8C6C7;
  50. }
  51. 25% {
  52. background-color: #98D0F4;
  53. }
  54. 50% {
  55. background-color: #B9F0B9;
  56. }
  57. 75% {
  58. background-color: #F9F9C5;
  59. }
  60. 100% {
  61. background-color: #F8C6C7;
  62. }
  63. }

以下は、HSLカラーの彩度(Saturation)と明度(Lightness)を固定し色相(Hue)だけを周期的に変化させるアニメーションの例です。

以下の例では、Sass の繰り返し処理(@for ~ through)を使ってキーフレームを出力しています。

以下は @for ~ through の構文です。

  1. //SASS (.scss)
  2. @for $変数名 from 開始の数値 through 終了の数値 {
  3. 処理を記述
  4. }

参考サイト:SCSSとCompassでおしゃれなCSSパーティクルを作ってみた(ics.media)

  1. //SASS (.scss)
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. margin: 20px;
  6. border-radius: 40px;
  7. @include animation(hueAnimation 8s infinite);
  8. }
  9.  
  10. @include keyframes(hueAnimation) {
  11. $keyFrameLength:10;
  12. @for $i from 0 through $keyFrameLength {
  13. #{(100 / $keyFrameLength) * $i + "%"} {
  14. $h: (360 / $keyFrameLength) * $i;
  15. $color: hsl($h, 80, 54);
  16. background-color: $color;
  17. }
  18. }
  19. }
$keyFrameLength:10;→キーフレームの数 - 1
この例では、10% ごとにキーフレームを作成するので 11個のキーフレームが作成されます。この値を変えるだけで、キーフレームの数や色相(Hue)の値が自動的に調整されます。例えば 25% ごとにするには4にします。
#{(100 / $keyFrameLength) * $i + "%"}
100% をキーフレーム数 -1 で割ってカウント変数を掛けて % を付けます。また個の部分はインターポレーションを使う必要があります。
$h: (360 / $keyFrameLength) * $i;
$color: hsl($h, 80, 54);
各キーフレームにおける色相(Hue)の値を算出しています。また、彩度(Saturation)と明度(Lightness)は固定にしています。

以下はコンパイル結果ですが、キーフレームのベンダープレフィックスは省略しています。

  1. /* CSS コンパイル後*/
  2. .foo {
  3. width: 80px;
  4. height: 80px;
  5. margin: 20px;
  6. border-radius: 40px;
  7. -moz-animation: hueAnimation 8s infinite;
  8. -webkit-animation: hueAnimation 8s infinite;
  9. animation: hueAnimation 8s infinite;
  10. }
  11.  
  12. @keyframes hueAnimation {
  13. 0% {
  14. background-color: #e82c2c;
  15. }
  16. 10% {
  17. background-color: #e89c2c;
  18. }
  19. 20% {
  20. background-color: #c2e82c;
  21. }
  22. 30% {
  23. background-color: #51e82c;
  24. }
  25. 40% {
  26. background-color: #2ce877;
  27. }
  28. 50% {
  29. background-color: #2ce8e8;
  30. }
  31. 60% {
  32. background-color: #2c77e8;
  33. }
  34. 70% {
  35. background-color: #512ce8;
  36. }
  37. 80% {
  38. background-color: #c22ce8;
  39. }
  40. 90% {
  41. background-color: #e82c9c;
  42. }
  43. 100% {
  44. background-color: #e82c2c;
  45. }
  46. }

ヘルパー関数

Compass には色々なヘルパー関数(Compass Helper Functions)があります。

Color Helpers

Sass にも色関連の関数がありますが、Compass ではそれらの関数をより便利にしたものがあります。

Compass Color Helpers

adjust-lightness($color, $amount)
明るさの調整をする関数で、Sass の lighten() と darken() の両方の機能を持ちます。
$color:色
$amount:値(%)。正の値を指定すると明るくなり、負の値を指定すると暗くなります。
  1. //SASS (.scss)
  2. .foo {
  3. background: adjust-lightness(#999, 20%);
  4. }
  5.  
  6. .bar {
  7. background: adjust-lightness(#999, -20%);
  8. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background: #cccccc;
  4. }
  5.  
  6. .bar {
  7. background: #666666;
  8. }
adjust-saturation($color, $amount)
彩度の調整をする関数で、Sass の saturate() と desaturate() の両方の機能を持ちます。
$color:色
$amount:値(%)。正の値を指定すると彩度が高くなり、負の値を指定すると低くなります。
  1. //SASS (.scss)
  2. .foo {
  3. background-color: #54c083;
  4. }
  5.  
  6. .bar {
  7. background-color: adjust-saturation(#54c083, 30%);
  8. }
  9.  
  10. .baz {
  11. background-color: adjust-saturation(#54c083, -30%);
  12. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background-color: #54c083;
  4. }
  5.  
  6. .bar {
  7. background-color: #31e37e;
  8. }
  9.  
  10. .baz {
  11. background-color: #779d88;
  12. }
foo
bar
baz
shade($color, $percentage)
黒を混ぜることにより色を暗くする関数です。
color:色
$percentage:割合(%)。負の値は指定できません。
tint($color, $percentage)
白を混ぜることにより色を明るくする関数です。
color:色
$percentage:割合(%)。負の値は指定できません。
  1. //SASS (.scss)
  2. .foo {
  3. background-color: #54c083;
  4. }
  5.  
  6. .bar {
  7. background-color: shade(#54c083, 30%);
  8. }
  9.  
  10. .baz {
  11. background-color: tint(#54c083, 30%);
  12. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background-color: #54c083;
  4. }
  5.  
  6. .bar {
  7. background-color: #3b865c;
  8. }
  9.  
  10. .baz {
  11. background-color: #87d3a8;
  12. }
foo
bar
baz
その他のヘルパー関数

カラーヘルパー以外にも沢山のヘルパー関数(Compass Helper Functions)があります。以下はそのごく一部です。

画像の幅と高さの取得
image-width($image), image-height($image)
$image:画像ファイル名(プロジェクト内 images フォルダ)
ミックスインなどで使用することが多いと思います。
画像の URL を補完
image-url($path, $only-path, $cache-buster)
$path:画像ファイル名(プロジェクト内 images フォルダ)
$only-path:デフォルトは「false」。「true」を指定すると url() なしのパスを返します。
$cache-buster:デフォルトは「true」。「false」を指定すると cache-buster の機能を使用しません。config.rb の asset_cache_buster の設定にもよります。

以下はミックスイン内で画像の幅と高さ、及び URL を取得する例です。このミックスイン自体はサンプル用で適当なものです。

  1. //SASS (.scss)
  2. @mixin set-icon($icon, $width: image-width($icon), $height: image-height($icon)) {
  3. background: image-url($icon) no-repeat left center;
  4. line-height: $height;
  5. padding: 0 0 0 $width;
  6. }
  7.  
  8. .foo {
  9. @include set-icon('wp-icon.png');
  10. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. background: url('../images/wp-icon.png') no-repeat left center;
  4. line-height: 30px;
  5. padding: 0 0 0 30px;
  6. }
画像をインラインイメージ(base64)に変換
inline-image($image)
画像をインラインイメージ(data uri) に変換する関数です。
$image:画像ファイル名
  1. //SASS (.scss)
  2. .foo {
  3. width: 24px;
  4. height: 24px;
  5. background-image: inline-image('leaf.png');
  6. }

上記で指定した画像は以下のフォルダに保存されています。

images folder

また、config.rb には画像フォルダが「images_dir = "images"」として指定されています。

  1. /* CSS コンパイル後*/
  2. .foo {
  3. width: 24px;
  4. height: 24px;
  5. background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAABx0RVh0U29mdHdhcmUAQWRvYmUgRmlyZXdvcmtzIENTNui8sowAAAAWdEVYdENyZWF0aW9uIFRpbWUAMDUvMDMvMTPeQgfSAAAA7ElEQVRIidWVoQ7CMBCGvxIESJCz4JgEyxss81NYHoLn4A32AnPIIUEOOckskrkitgQCbcOVTfAnTUWv/9feNVeltaZPDXp1B4bSDUmmAiAEZu0A2KWRvnsDkkyNgSWwBiaGkBA4eQGSTK2AGBg5wuSA9tQbnmlwKbAtGAGt+da18U2mtAH2VxQLzJ36ACSZmtIUtBOZbrDoytwGmHv41BKAtWAOXSUAn+IWEoBUNXDuE5Db+lAXgAo4ugJ+AVTA3nV68GjXNDnP00gfvgmWAC5AkUba2DUlgKqdbzTvu0wjXUpMX6X+/k/+f8ADUXUzUlWzbikAAAAASUVORK5CYII=');
  6. }
インラインイメージ
画像を文字列に変換してそのデータを HTML や CSS のソースコード内に埋め込む方法
利点:
HTTPリクエストを減らせる。小さな画像ファイルではメリット有り。
注意点(デメリット):
データサイズが 37% 増加する
古いIEには対応していない。比較的新しいIEでも制限があり注意が必要
データ自体はキャッシュされない
Headings h 要素の出力
headings()
h 要素を出力します。
引数に数値を指定することで特定の h 要素を出力できます。
  1. //SASS (.scss)
  2. #{headings()} {
  3. font-weight: normal;
  4. }
  5.  
  6. #{headings(2)} {
  7. color: green;
  8. }
  9.  
  10. #{headings(3,5)} {
  11. text-decoration: underline;
  12. }
  1. /* CSS コンパイル後*/
  2. h1, h2, h3, h4, h5, h6 {
  3. font-weight: normal;
  4. }
  5.  
  6. h1, h2 {
  7. color: green;
  8. }
  9.  
  10. h3, h4, h5 {
  11. text-decoration: underline;
  12. }
指定した種類の要素を返す
elements-of-type($display)
$display:要素のタイプ(種類)

指定できる要素のタイプには次のようなものがあります。

block, inline, inline-block, table, list-item, table-row-group, table-header-group, table-footer-group, table-row, table-cell, html5-block, html5-inline, html5

これらのタイプを指定したときに返される要素については「Compass Display Helpers」を参照ください。

  1. //SASS (.scss)
  2. #{elements-of-type(html5-block)} {
  3. display: block;
  4. }
  1. /* CSS コンパイル後*/
  2. article, aside, details, figcaption, figure, footer, header, hgroup, main, menu, nav, section, summary {
  3. display: block;
  4. }

Utilities モジュール

ユーティリティ系ミックスインで以下のような種類があります。

  • Links – アンカーのスタイル
  • Lists – リストのスタイル
  • Text – テキストのスタイル
  • Color – 色関連のユーティリティ
  • General – 他に属さない色関連のユーティリティ
  • Sprites – スプライトのミックスイン
  • Tables – テーブルのスタイル

Compass/Utilities

クリアフィックス

Compass には clearfix のミックスインが3種類あります。

  • clearfix(通常?のクリアフィックス)
  • legacy-pie-clearfix(Firefox3.5 以前に対応する場合はこちらを使用)
  • pie-clearfix(overflow: hidden を使用していないクリアフィックス)

最近の傾向としては、pie-clearfix(別名 micro clear fix)が使用されるようです。

また、overflow: hidden; を使用すると問題がある場合は pie-clearfix を使用します。

  1. //SASS (.scss)
  2. .clearfix {
  3. @include clearfix;
  4. }
  5.  
  6. .legacy-pie-clearfix {
  7. @include legacy-pie-clearfix;
  8. }
  9.  
  10. .pie-clearfix {
  11. @include pie-clearfix;
  12. }
  1. /* CSS コンパイル後*/
  2.  
  3. /* clearfix */
  4. .clearfix {
  5. overflow: hidden;
  6. *zoom: 1;
  7. }
  8.  
  9. /* legacy-pie-clearfix */
  10. .legacy-pie-clearfix {
  11. *zoom: 1;
  12. }
  13. .legacy-pie-clearfix:after {
  14. content: "\0020";
  15. display: block;
  16. height: 0;
  17. clear: both;
  18. overflow: hidden;
  19. visibility: hidden;
  20. }
  21.  
  22. /* pie-clearfix */
  23. .pie-clearfix {
  24. *zoom: 1;
  25. }
  26. .pie-clearfix:after {
  27. content: "";
  28. display: table;
  29. clear: both;
  30. }
リストアイコン pretty-bullets

Compass Bullets

pretty-bullets はリストにアイコン画像を付けるミックスインです。以下が書式です。

  1. @include pretty-bullets(画像ファイル名, 幅, 高さ, 行間, 左パディング )

画像ファイル名:必須(プロジェクト内であれば URL は自動で取得)
幅, 高さ, 行間, 左パディング :オプション

  • 画像ファイル名のみを指定すると、自動でサイズを取得してくれます
  • 行間のデフォルトは「18px」、左パディングのデフォルトは「14px」です
  • 必要に応じて幅, 高さ, 行間, 左パディングを指定します(単位は統一する必要があります)
  • ul 要素または ol 要素に対して指定します

画像ファイル名の末尾にクエリ情報が付く場合がありますが、設定ファイル(config.rb)で「asset_cache_buster :none」を指定すれば付かないようになります。

以下はプロジェクト内 images フォルダの 24 x 24 (px) の「leaf.png」という画像を使用する例です。

  1. //SASS (.scss)
  2. ul.foo {
  3. @include pretty-bullets('leaf.png', 24px, 24px, 24px, 24px);
  4. line-height: 24px;
  5. }
  1. /* CSS コンパイル後*/
  2. ul.foo {
  3. margin-left: 0;
  4. line-height: 24px;
  5. }
  6. ul.foo li {
  7. padding-left: 24px;
  8. background: url('../images/leaf.png') no-repeat 0px 0px;
  9. list-style-type: none;
  10. }
  • list item
  • list item
  • list item
  • list item

この例では、line-height を別途指定しています。pretty-bullets() のソースを見ると、引数に渡される行間の値はポジションの算出に使用されていて line-height の出力はないので、必要に応じて指定すると良いと思います。

  1. //pretty-bullets() のソース
  2. @mixin pretty-bullets($bullet-icon, $width: image-width($bullet-icon), $height: image-height($bullet-icon), $line-height: 18px, $padding: 14px) {
  3. margin-left: 0;
  4. li {
  5. padding-left: $padding;
  6. background: image-url($bullet-icon) no-repeat ($padding - $width) / 2 ($line-height - $height) / 2;
  7. list-style-type: none;
  8. }
  9. }

以下は、上記を参考に作成したアイコンを挿入するミックスインです。環境に合わせて margin や padding を書き換える必要があるかと思います。

  1. //SASS (.scss)
  2. @mixin insert-icon($icon, $width: image-width($icon), $height: image-height($icon), $margin: (0 0 0 0), $padding:(0 0 0 image-width($icon)), $line-height: null) {
  3. margin: $margin;
  4. padding: $padding;
  5. background: image-url($icon) no-repeat left center;
  6. line-height: if($line-height, $line-height, $height);
  7. }
  8.  
  9. .foo {
  10. @include insert-icon('wp-icon.png');
  11. }
  12.  
  13. .bar {
  14. @include insert-icon('wp-icon.png', $margin: (30px 0), $line-height: 40px, $padding:(0 0 0 50px));
  15. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. margin: 0 0 0 0;
  4. padding: 0 0 0 30px;
  5. background: url('../images/wp-icon.png') no-repeat left center;
  6. line-height: 30px;
  7. }
  8.  
  9. .bar {
  10. margin: 30px 0;
  11. padding: 0 0 0 50px;
  12. background: url('../images/wp-icon.png') no-repeat left center;
  13. line-height: 40px;
  14. }
  1. <h4 class="bar">見出しにアイコン画像を挿入(サンプル)</h4>

見出しにアイコン画像を挿入(サンプル)

Typography モジュール

Typography モジュールはテキスト関連のモジュールです。

Compass/Typography

画像置換 replace-text-with-dimensions

画像置換のミックスインです。(text-indent プロパティでテキストを飛ばして置換します)

おなじようなミックスインに「replace-text」がありますが、対象の文字列の font-size と画像のサイズが大きく異なる場合などは画像が切れて表示されてしまうことがあります。

replace-text-with-dimensions は画像のサイズを取得して幅と高さを自動で指定してくれます。

  1. replace-text-with-dimensions($img, $x, $y, $inline)
  • img:画像ファイル名(必須)
  • $x:background-position-x (デフォルトは 50%)
  • $y:background-position-y (デフォルトは 50%)
  • $inline:true を指定するとインラインイメージ (data uri)が使用されます(デフォルトは false)

画像ファイル名の末尾にクエリ情報が付く場合がありますが、設定ファイル(config.rb)で「asset_cache_buster :none」を指定すれば付かないようになります。

  1. //SASS (.scss)
  2. .foo {
  3. @include replace-text-with-dimensions('compass-icon.png', 0, 0);
  4. }
  1. /* CSS コンパイル後*/
  2. .foo {
  3. text-indent: -119988px;
  4. overflow: hidden;
  5. text-align: left;
  6. text-transform: capitalize;
  7. background-image: url('../images/compass-icon.png');
  8. background-repeat: no-repeat;
  9. background-position: 0 0;
  10. width: 160px;
  11. height: 42px;
  12. }
  1. <h5 class="foo">Compass</h5>
Compass
バーティカル・リズム Vertical Rhythm

Compass Vertical Rhythm

Vertical Rhythm(バーティカル・リズム)はページの行間サイズを揃えて(1行の高さを1つの単位として)、垂直方向のリズムを作る方法です。

最初に基本のフォントサイズと行の高さを設定します。この例ではフォントサイズの単位を「rem」にするため、変数 $rhythm-unit を使って単位を em から rem に変更します。

そして、設定した基本となるフォントサイズと行の高さを適用するために establish-baseline ミックスインを使って出力します。

  • $base-font-size(基準となるフォントサイズ。デフォルトは 16px)
  • $base-line-height(基準となる行間。デフォルトは 24px)
  1. //SASS (.scss)
  2. $base-font-size: 16px; //ベースフォントサイズ
  3. $base-line-height: 24px; //ベースユニットサイズ
  4. $rhythm-unit: "rem"; //単位を em から rem に変更
  5.  
  6. @include establish-baseline;
  1. /* CSS コンパイル後*/
  2. html {
  3. font-size: 100%;
  4. line-height: 1.5em;
  5. }

また、ブラウザのデフォルトの padding や margin の値で Vertical Rhythm が乱れることもあるので、必要に応じて事前にリセットしておきます。(font-family によってもずれることがあります)

ベースフォントサイズとベースユニットサイズを決めて、それを元にガイドラインを引き、ガイドラインに合うように調整するのが簡単です。ガイドラインは以下の方法で表示することができます。

1つの方法は、Compass の debug-vertical-alignment や baseline-grid-background() ミックスインを使用する方法で、以下のように記述するとガイドラインが表示されます。

Compass Grid Backgrounds

  1. //SASS (.scss)
  2. html {
  3. @include baseline-grid-background($base-line-height, red);
  4. }

または、Basehold.it を利用して以下のように記述します。24px 幅の赤色(ff0000)の baseline grid を表示する場合の例。(2019/12/20 追記)色を指定するとうまくグリッドを表示できないようです。

  1. //SASS (.scss)
  2. html {
  3. background-image: url(//basehold.it/i/24/ff0000);
  4. }

あとはミックスインを使って要素ごとに font-size, margin, padding 等を設定します。

フォントサイズと行の高さの設定

「adjust-font-size-to」ミックスインを使って設定します。

  1. adjust-font-size-to($to-size, $lines, $from-size)
  • $to-size:フォントサイズ。何 px に設定するかを指定(必須)
  • $lines:line-height を基準となる行間の何倍に設定するか。
  • $from-size:基準となるフォントサイズ。デフォルトは $base-font-size。

以下は Heading 要素の設定例です。

  1. //SASS (.scss)
  2. h1 { @include adjust-font-size-to(48px,2); }
  3. h2 { @include adjust-font-size-to(36px,2); }
  4. h3 { @include adjust-font-size-to(24px,1); }
  5. h4 { @include adjust-font-size-to(22px,1); }
  6. h5 { @include adjust-font-size-to(20px,1); }
  7. h6 { @include adjust-font-size-to(18px,1); }
  1. /* CSS コンパイル後*/
  2. h1 {
  3. font-size: 48px;
  4. font-size: 3rem;
  5. line-height: 48px;
  6. line-height: 3rem;
  7. }
  8.  
  9. h2 {
  10. font-size: 36px;
  11. font-size: 2.25rem;
  12. line-height: 48px;
  13. line-height: 3rem;
  14. }
  15.  
  16. h3 {
  17. font-size: 24px;
  18. font-size: 1.5rem;
  19. line-height: 24px;
  20. line-height: 1.5rem;
  21. }
  22. //以下省略

要素の前後の余白(margin, padding)の設定

rhythm-margins() や rhythm-padding() ミックスインを使うと要素の前後の余白をまとめて設定することができます。

  1. rhythm-margins($leader, $trailer, $font-size)
  2. rhythm-padding($padding-leader, $padding-trailer, $font-size)
  • $leader:上部のマージンを基準となる行間の何倍に設定するか。デフォルトは「1」。
  • $trailer:下部のマージンを基準となる行間の何倍に設定するか。
  • $font-size:基準となるフォントサイズ。デフォルトは $base-font-size。

以下は、rhythm-margins() のミックスインのソースです。

  1. //SASS (.scss)
  2. @mixin rhythm-margins($leader: 1, $trailer: $leader, $font-size: $base-font-size) {
  3. @include leader($leader, $font-size);
  4. @include trailer($trailer, $font-size);
  5. }

ミックスインの内部では、leader():上部のマージンと、trailer():下部のマージンを呼び出しているのがわかります。これらを使って個別に指定することもできます。

  1. leader($lines, $font-size, $property)
  • margin-top($property の設定により padding-top)を設定
  • $lines:基準となる行間の何倍にするか。
  • $font-size:基準となるフォントサイズ。デフォルトは $base-font-size
  • $property:margin か padding を指定。デフォルトは margin
  1. trailer($lines, $font-size, $property)
  • margin-bottom($property の設定により padding-bottom)を設定
  • $lines:基準となる行間の何倍にするか。
  • $font-size:基準となるフォントサイズ。デフォルトは $base-font-size
  • $property:margin か padding を指定。デフォルトは margin

以下は、rhythm-margins() を使って前後のマージンを設定する例です。

  1. //SASS (.scss)
  2. h1,h2,h3 { @include rhythm-margins(2,1); }
  3. h4,h5,h6 { @include rhythm-margins(1,1); }
  4. p,ul,ol, dl {@include rhythm-margins(0,1);}
  1. /* CSS コンパイル後*/
  2. h1, h2, h3 {
  3. margin-top: 48px;
  4. margin-top: 3rem;
  5. margin-bottom: 24px;
  6. margin-bottom: 1.5rem;
  7. }
  8.  
  9. h4, h5, h6 {
  10. margin-top: 24px;
  11. margin-top: 1.5rem;
  12. margin-bottom: 24px;
  13. margin-bottom: 1.5rem;
  14. }
  15.  
  16. p, ul, ol, dl {
  17. margin-top: 0px;
  18. margin-top: 0rem;
  19. margin-bottom: 24px;
  20. margin-bottom: 1.5rem;
  21. }

ボーダーの処理

ボーダーを指定した場合、その幅の分ずれる(リズムが狂う)ので rhythm-borders() ミックスインを利用して調整することができます。

  1. rhythm-borders($width, $lines, $font-size, $border-style)
  • $width:ボーダーの幅。デフォルトは $default-rhythm-border-width: 1px
  • $lines:パディングを基準となる行間の何倍にするか。デフォルトは 1。
  • $font-size:基準となるフォントサイズ。デフォルトは $base-font-size
  • $border-style:ボーダースタイル。デフォルトは $default-rhythm-border-style: solid

以下は、rhythm-borders() を使って調整する例です。

  1. //SASS (.scss)
  2. .foo {
  3. @include rhythm-borders(1px, 1);
  4. border-color: #999;
  5. }

パディングとボーダー幅で 24px になるように調整されています。

  1. /* CSS コンパイル後*/
  2. .foo {
  3. border-width: 1px;
  4. border-width: 0.0625rem;
  5. border-style: solid;
  6. padding: 23px;
  7. padding: 1.4375rem;
  8. border-color: #999;
  9. }

leading-border() や trailing-border(), horizontal-borders() を使って個々のボーダーを調整することも可能です。