概要と目標
Sassの高度な機能を覚えて、
さらに効率よくSassを書けるようになろう。
ExtendやMixinといった、Sassの高度な機能を使えば、得られるメリットが大きい。
これらの使い方を習得し使えるようになろう。
ExtendやMixinといった、Sassの高度な機能を使えば、得られるメリットが大きい。
これらの使い方を習得し使えるようになろう。
同じスタイルを何度も使うときは、
「@extend」という機能で、スタイルを継承できる。
セレクタ {
@extend 継承するセレクタ;
}
以下の「.button-praimary」と「「.button-secondary」は、
全て「.button」と同じプロパティが含まれている。
こういう時は@extendを使うと便利。
.button {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
}
.button-primary {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
border: 3px solid #9DBF4A;
}
.button-secondary {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
border: 3px solid #2C2C31;
}
// 継承元のスタイル
.button {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
}
.button-primary {
@extend .button;
border: 3px solid #9DBF4A;
}
.button-secondary {
@extend .button;
border: 3px solid #2C2C31;
}
.button, .button-primary, .button-secondary {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
}
.button-primary {
border: 3px solid #9DBF4A;
}
.button-secondary {
border: 3px solid #2C2C31;
}
「@extend」はスタイルを継承する便利な起動だが、必ず継承元のスタイルも生成される。
継承元のセレクタの先頭に「%」という@extendのプレースホルダーを利用すると、
継承元のスタイルを生成されない。
// 継承元のスタイル
%button {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
}
.button-primary {
@extend %button;
border: 3px solid #9DBF4A;
}
.button-secondary {
@extend %button;
border: 3px solid #2C2C31;
}
.button-primary, .button-secondary {
display: block;
padding: 1em;
font-size: 1em;
cursor: pointer;
}
.button-primary {
border: 3px solid #9DBF4A;
}
.button-secondary {
border: 3px solid #2C2C31;
}
「@mixin」を使えば、スタイルの集まりを登録しておき、
いつでも使えるようにすることができる。
@mixin Mixin名(引数) {
// Mixinとして登録するスタイル
}
引数は「,(半角カンマ)」区切りで複数指定可能(必要ない場合は省略も可能)
セレクタ{
@include 呼び出すMixin名(引数);
}
例えば、以下のCSSのように「マウスが乗った時、その要素が半透明になる」というスタイルを、ボタンやバナーなど、様々な要素で使いたいとする。
ただ、要素によっては不透明度を変えたり、アニメーションのスピードを変えたりしたいこともある。こういう時、@mixinを使うと便利。
.banner {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.banner:hover {
opacity: 0.5;
}
.button {
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.button:hover {
opacity: 0.7;
}
.link {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.link:hover {
opacity: 0.7;
}
// Mixinの登録
@mixin fadeLink ($speed, $opacity) {
transition: all $speed ease-in-out;
&:hover {
opacity: $opacity;
}
}
// Mixinの呼び出し
.banner {
@include fadeLink(.2s, .5);
}
.button {
@include fadeLink(.3s, .7);
}
.link {
@include fadeLink(.2s, .7);
}
.banner {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.banner:hover {
opacity: 0.5;
}
.button {
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.button:hover {
opacity: 0.7;
}
.link {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.link:hover {
opacity: 0.7;
}
例えばアニメーションのスピードや、不透明度が多くの箇所で同じ場合は、引数に初期値を決めておくと便利。
@mixin Mixin名(引数: 初期値) {
// Mixinとして登録するスタイル
}
引数は「,(半角カンマ)」区切りで複数指定可能(必要ない場合は省略も可能)
// Mixinの登録
@mixin fadeLink ($speed: .3s, $opacity: .7) {
transition: all $speed ease-in-out;
&:hover {
opacity: $opacity;
}
}
// Mixinの呼び出し
.banner {
@include fadeLink(.2s, .5);
}
.button {
@include fadeLink();
}
.link {
@include fadeLink(.2s);
}
.banner {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.banner:hover {
opacity: 0.5;
}
.button {
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.button:hover {
opacity: 0.7;
}
.link {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.link:hover {
opacity: 0.7;
}
@mixin translate($val) {
transform: translate($val);
}
.translate {
@include translate(50%, 0);
}
@mixin translate($val...) {
transform: translate($val);
}
.translate {
@include translate(50%, 0);
}
@mixinはスタイルを登録し、それを呼び出すことが機能だが、
「@content」を使うと、ルールのブロックをMixinに渡すことができる。
// Mixinの登録
@mixin fadeLink ($speed: .3s, $opacity: .7) {
transition: all $speed ease-in-out;
&:hover {
opacity: $opacity;
}
}
@mixin mq($breakpoint) {
@media screen and (min-width: $breakpoint) {
@content;
}
}
// ブレークポイントの設定
$tab: 768px;
$lap: 960px;
// Mixinの呼び出し
.banner {
@include fadeLink(.2s, .5);
}
.button {
@include fadeLink();
}
.link {
@include fadeLink(.2s);
}
@include mq($tab) {
.link {
font-size: 1.25em;
}
}
@include mq($lap) {
.link {
font-size: 2em;
}
}
.banner {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.banner:hover {
opacity: 0.5;
}
.button {
-webkit-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
.button:hover {
opacity: 0.7;
}
.link {
-webkit-transition: all 0.2s ease-in-out;
transition: all 0.2s ease-in-out;
}
.link:hover {
opacity: 0.7;
}
@media screen and (min-width: 768px) {
.link {
font-size: 1.25em;
}
}
@media screen and (min-width: 960px) {
.link {
font-size: 2em;
}
}
Sassには数値に関する関数や、色に関する関数、文字に関する関数など、様々な便利な関数が用意されている。
ここではそのいくつかを紹介する。
percentage() ・・・ パーセントに変換percentage(小数点)
$content-width: 960px;
// 数値に関する関数
.l-main {
// パーセントに計算
width: percentage( 690px/$content-width );
}
.l-sidebar {
width: percentage( 240px/$content-width );
}
.l-main {
width: 71.875%;
}
.l-sidebar {
width: 25%;
}
lighten() ・・・ 指定した色コードを明るくするlighten(色, 明るくする割合)
darken() ・・・ 指定した色コードを暗くするdarken(色, 暗くする割合)
$content-width: 960px;
$main-color: #9DBF4A;
// 数値に関する関数
.l-main {
// パーセントに計算
width: percentage( 690px/$content-width );
}
.l-sidebar {
width: percentage( 240px/$content-width );
}
// 色に関する関数
.button {
background-color: $main-color;
&:hover {
// 指定した色の明度を上げる
background-color: lighten($main-color, 10%);
}
// 指定した色の明度を下げる
&:active {
background-color: darken($main-color, 10%);
}
}
.l-main {
width: 71.875%;
}
.l-sidebar {
width: 25%;
}
.button {
background-color: #9DBF4A;
}
.button:hover {
background-color: #b1cc70;
}
.button:active {
background-color: #809e38;
}
Sassには、スタイルの継承する@extendや、
スタイルを定義する@mixinなどが使える。