PORTFOLIO & WEB DESIGN MEMORANDOM BLOG   
PORTFOLIO & WEB DESIGN MEMORANDOM BLOG   

【CSS】SCSSの基礎

タグ :

SCSSの入れ子構造

入れ子構造(ネスト構造) とは、親要素の中に子要素のスタイルを書くことで、
階層構造を視覚的にわかりやすくする SCSSの特徴です。

例:ネストあり

  1. .card {
  2.   background: #fff;
  3.   padding: 20px;
  4.   .title {
  5.     font-size: 18px;
  6.     font-weight: bold;
  7.   }
  8.   .text {
  9.     color: #555;
  10.   }
  11. }

↑をCSSに変換

  1. .card {
  2.   background: #fff;
  3.   padding: 20px;
  4. }
  5. .card .title {
  6.   font-size: 18px;
  7.   font-weight: bold;
  8. }
  9. .card .text {
  10.   color: #555;
  11. }

◆ネストの利点(メリット)

1. 親子関係が見やすい
• .card の中の .title や .text が直感的にわかる。

2. 構造が自然に反映される
• HTMLの階層と同じようにSCSSが書ける。

3. 記述が短くなる
• 親クラスを何度も書かなくてよい。

ネストの注意点・デメリット

•ネストが深くなりすぎると危険

  1. .header {
  2.   .nav {
  3.     ul {
  4.       li {
  5.         a {
  6.           span {
  7.             color: red;
  8.           }
  9.         }
  10.       }
  11.     }
  12.   }
  13. }

↑をCSSに変換

  1. .header .nav ul li a span {
  2.   color: red;
  3. }

保守性が悪くなり、セレクタも重く なります。

◆ 推奨ルール

• ネストは 2〜3階層まで に抑える。
• コンポーネント単位で分けて、責任を分離する。

よく使うネスト構文

& アンパサンド(親セレクタの再利用)

  1. .button {
  2.   color: #000;
  3.   &:hover {
  4.     color: #fff;
  5.     background: #000;
  6.   }
  7.   &.is-active {
  8.     border: 1px solid blue;
  9.   }
  10. }

↑をCSSに変換

  1. .button {
  2.   color: #000;
  3. }
  4. .button:hover {
  5.   color: #fff;
  6.   background: #000;
  7. }
  8. .button.is-active {
  9.   border: 1px solid blue;
  10. }

SCSS(Sass)の変数設定

SCSS(Sass)の変数は、値を再利用可能にして、スタイルの保守性を高めるための非常に便利な機能です。
色、フォントサイズ、余白、ブレークポイントなど、さまざまな値を変数として定義できます。

基本構文

  1. $変数名: 値;

  1. $primary-color: #3498db;
  2. $font-stack: 'Helvetica Neue', sans-serif;
  3. $padding-base: 16px;

使用方法

  1. body {
  2.   font-family: $font-stack;
  3.   background-color: $primary-color;
  4.   padding: $padding-base;
  5. }

よく使う変数の例

  1. // カラーパレット
  2. $color-primary: #1abc9c;
  3. $color-secondary: #2ecc71;
  4. $color-accent: #e74c3c;
  5. $color-text: #333;
  6. $color-bg: #f9f9f9;
  7. // フォントサイズ
  8. $font-size-base: 16px;
  9. $font-size-large: 20px;
  10. $font-size-small: 12px;
  11. // スペーシング
  12. $spacing-sm: 8px;
  13. $spacing-md: 16px;
  14. $spacing-lg: 32px;
  15. // ブレークポイント
  16. $breakpoint-sm: 576px;
  17. $breakpoint-md: 768px;
  18. $breakpoint-lg: 992px;
  19. $breakpoint-xl: 1200px;

実用サンプル

  1. $color-primary: #007bff;
  2. $font-size-base: 16px;
  3. $padding-base: 1rem;
  4. .button {
  5.   background-color: $color-primary;
  6.   color: white;
  7.   font-size: $font-size-base;
  8.   padding: $padding-base;
  9.   border: none;
  10.   border-radius: 4px;
  11.   &:hover {
  12.     background-color: darken($color-primary, 10%);
  13.   }
  14. }

SCSS変数のメリット

1.保守性の向上
一箇所の変更で全体に影響を与えられる

2.デザインの一貫性
色やフォントなどを統一できる

3.チーム開発で便利
命名規則に沿えば他の人でも直感的に理解しやすい

FLOCSS や BEM と組み合わせた SCSS の変数管理方法

FLOCSS(ファイル構成ルール)とは

FLOCSS(Folder + Layout + Object + Component + Project)では、次のようにファイルを分けて管理します。

  1. /scss/
  2.   ├── foundation/ // 基本設定(変数やmixinなど)
  3.   ├── layout/ // ヘッダーやフッターなどのレイアウトパーツ
  4.   ├── object/
  5.   │ ├── component/ // UIコンポーネント(ボタンなど)
  6.   │ ├── project/ // プロジェクト固有のモジュール
  7.   │ └── utility/ // ヘルパークラス(margin調整など)
  8.   └── style.scss // メインの読み込みファイル

BEM(命名規則)について

BEMは、クラス名を以下の形式で管理します。

BEMにおける –(ダブルハイフン)と __(ダブルアンダースコア)は、
それぞれ役割がまったく違うので、しっかり区別することが重要です。

◆ __(ダブルアンダースコア):Element(要素)

• 「Blockの中の構成要素」を表します。
• Blockが親で、Elementはその部分的なパーツ。
• ElementはBlockがなければ存在できないものです。

  1. .card {} // Block
  2. .card__title {} // Element(カードの中のタイトル)
  3. .card__image {} // Element(画像)

◆ –(ダブルハイフン):Modifier(修飾子)

• BlockやElementの状態やバリエーションを表します。
• 色違い・サイズ違い・アクティブ状態など。
• 基本構造に意味のある変化を加えるものです。

  1. .button {} // Block
  2. .button--large {} // Modifier(大きいサイズのバリエーション)
  3. .button--secondary {} // Modifier(色の違い)

◆ 組み合わせた例

  1. <div class="card card--featured">
  2.   <h2 class="card__title">タイトル</h2>
  3.   <img class="card__image" src="..." />
  4. </div>

• .card → Block
• .card__title → Element(タイトル部分)
• .card–featured → Modifier(特別な装飾付き)

◆ 覚え方のコツ

__アンダースコア・・・部品(構成要素)

–ハイフン・・・バリエーション(状態変更)

  1. .card__title--large {} // ElementにもModifierをつけられる

FLOCSS + BEM における変数の管理方法

1. foundation/_variables.scss にすべての変数をまとめる

  1. // colors
  2. $color-primary: #3498db;
  3. $color-secondary: #2ecc71;
  4. $color-text: #333;
  5. $color-white: #fff;
  6. $color-bg: #f5f5f5;
  7. // font sizes
  8. $font-size-base: 16px;
  9. $font-size-sm: 14px;
  10. $font-size-lg: 20px;
  11. // spacing
  12. $space-xs: 4px;
  13. $space-sm: 8px;
  14. $space-md: 16px;
  15. $space-lg: 32px;
  16. // breakpoints
  17. $breakpoint-sm: 576px;
  18. $breakpoint-md: 768px;
  19. $breakpoint-lg: 992px;

2. foundation/_mixin.scss で共通処理を定義(例:メディアクエリ)

  1. // colors
  2. @mixin mq($breakpoint) {
  3.   @if $breakpoint == sm {
  4.     @media (min-width: $breakpoint-sm) { @content; }
  5.   } @else if $breakpoint == md {
  6.     @media (min-width: $breakpoint-md) { @content; }
  7.   } @else if $breakpoint == lg {
  8.     @media (min-width: $breakpoint-lg) { @content; }
  9.   }
  10. }

3. _button.scss(component)で変数を使ったBEMスタイル

  1. // object/component/_button.scss
  2. .button {
  3.   display: inline-block;
  4.   font-size: $font-size-base;
  5.   padding: $space-sm $space-md;
  6.   background-color: $color-primary;
  7.   color: $color-white;
  8.   border-radius: 4px;
  9.   text-align: center;
  10.   &--secondary {
  11.     background-color: $color-secondary;
  12.   }
  13.   &--large {
  14.     font-size: $font-size-lg;
  15.     padding: $space-md $space-lg;
  16.   }
  17.   &:hover {
  18.     opacity: 0.8;
  19.   }
  20. }

4. style.scss ですべてを読み込む

  1. // style.scss
  2. @import "foundation/variables";
  3. @import "foundation/mixin";
  4. @import "layout/header";
  5. @import "layout/footer";
  6. @import "object/component/button";
  7. @import "object/project/home";

命名&変数のルール

◆ 色
命名ルール例:$color-primary
補足:役割に基づく命名が基本

◆ サイズ
命名ルール例:$font-size-base
補足:_base, _sm, _lg など

◆ 余白
命名ルール例:$space-sm
補足:スケール単位で統一

◆ BEMクラス名
命名ルール例:.block__element–mod
補足:FLOCSS内の各componentで使用

SCSSの@importとは?

  1. @import 'filename';

•拡張子 .scss や _(アンダースコア)は省略可能。
•SCSSファイルを分割して管理でき、1つにまとめて読み込めます。

◆ ファイル構成

  1. /scss
  2.   ├── _variables.scss
  3.   ├── _mixins.scss
  4.   └── main.scss

  1. // main.scss
  2. @import 'variables';
  3. @import 'mixins';
  4. body {
  5.   background-color: $main-bg;
  6. }

★ 今までの@importはもう古い!これからは@use使う!

これまでは@importでファイルを読み込んでいましたが、Sass公式では今後使わないように!と言っています。

@useの使い方

SCSSファイルを読み込む新しい書き方です。

  1. @use 'ファイル名';

読み込んだファイルの中身を「ファイル名.変数名」で使う

  1. // _variables.scss
  2. $main-color: #ff6600;

  1. // main.scss
  2. @use 'variables';
  3. body {
  4.   color: variables.$main-color;
  5. }

variables.$main-color みたいに「どのファイルの変数か」分かるように書くのがルールです。

@forwardも使う!

これは、「他のファイルの中身をまとめて渡す」ために使います。

FLOCSSのように、たくさんファイルがあるときに便利!

ファイルの構成

  1. scss/
  2. ├── foundation/
  3. │ ├── _variables.scss
  4. │ ├── _reset.scss
  5. │ └── _index.scss ← ここにまとめる!
  6. └── main.scss

① _variables.scss

  1. $main-color: #007acc;

② _reset.scss

  1. * {
  2.   margin: 0;
  3.   padding: 0;
  4. }

③ _index.scss ← ここに@forwardを使ってまとめる!

  1. @forward 'variables';
  2. @forward 'reset';

④ main.scss ← ここで@useするだけでOK!

  1. @use './foundation/index' as foundation;
  2. body {
  3.   color: foundation.$main-color;
  4. }

「foundation」という名前でまとめて読み込める。

まとめ

機能:@use
目的:読み込む
書き方:@use ‘ファイル名’;
ポイント:名前空間が必要!

機能:@forward
目的:まとめて渡す
書き方:@forward ‘ファイル名’;
ポイント:インデックスファイルで使う

このやり方で書く利点

•ファイルがスッキリ整理できる
•変数の場所が分かりやすい
•エラーが減る!

SCSSのリスト機能とループ処理(@each)を使って、同じパターンのクラスを自動生成する方法

  1. $colors: red, blue, green;
  2. @each $color in $colors {
  3.   .text-#{$color} {
  4.     color: $color;
  5.   }
  6. }

↑をCSSに変換

  1. .text-red {
  2.   color: red;
  3. }
  4. .text-blue {
  5.   color: blue;
  6. }
  7. .text-green {
  8.   color: green;
  9. }

解説

① $colors: red, blue, green;

これはSassのリスト(配列のようなもの)です。
変数 $colors に red, blue, green の3つの色を格納しています。

② @each $color in $colors { … }

これはループ構文です。
リストの中身を1つずつ $color に代入して、ブロックの中の処理を繰り返します。

③ .text-#{$color}

ここで使われている #{$color} は「変数の値を展開する(=文字列の中に埋め込む)」という意味です。

.text-#{$color}は、それぞれ:
• .text-red
• .text-blue
• .text-green

というクラス名に変換されます。

④ color: $color;

これも $color に代入された値を使って、color: red; などに展開します。

Sass(SCSS)の @mixin と @includeについて

@mixinとは?

  1. @mixin border-radius {
  2.   border-radius: 8px;
  3. }

これは「共通して使いたいスタイルをまとめておく場所」です。
HTMLでいう「部品の型」みたいなものです。

@includeとは?

「@mixinを呼び出して使う」ものです。

  1. .box {
  2.   @include border-radius;
  3. }

これは、さきほど作った @mixin の中身(border-radius: 8px;)を、
.box の中で呼び出して使うことを意味します。

つまりこういう関係

  1. @mixin → 型(レシピ)
  2. @include → 呼び出し(料理する)

実用的な例:引数を使っての応用

  1. // 1. 使い回したいスタイル(mixin定義)
  2. @mixin font($size, $weight) {
  3.   font-size: $size;
  4.   font-weight: $weight;
  5. }
  6. // 2. 必要な場所で呼び出し(include)
  7. .title {
  8.   @include font(24px, bold);
  9. }

↑をCSSに変換

  1. .title {
  2.   font-size: 24px;
  3.   font-weight: bold;
  4. }

@mixin と @includeの利便性

• 同じスタイルを何度も書かなくてよくなる
• 値を引数で変えられるので柔軟に再利用できる
• 管理しやすく、修正も一か所で済む(メンテ性アップ)

よく使われる @mixin サンプル

① ボーダー・角丸

  1. @mixin rounded($radius: 4px) {
  2.   border-radius: $radius;
  3. }

  1. .card {
  2.   @include rounded(8px);
  3. }

ここで ($radius: 4px) と書いてありますが、
これは、「引数が渡されなかったときは 4px を使うよ」
という意味です。

↑をCSSに変換

  1. .card {
  2.   border-radius: 8px;
  3. }

② フレックスボックス(中央寄せ)

  1. @mixin flex-center {
  2.   display: flex;
  3.   justify-content: center;
  4.   align-items: center;
  5. }

  1. .container {
  2.   @include flex-center;
  3. }

③ フォントスタイル

  1. @mixin font($size, $weight: normal, $line: 1.5) {
  2.   font-size: $size;
  3.   font-weight: $weight;
  4.   line-height: $line;
  5. }

  1. .title {
  2.   @include font(24px, bold);
  3. }

④ メディアクエリ(レスポンシブ対応)

  1. @mixin sp {
  2.   @media (max-width: 767px) {
  3.     @content;
  4.   }
  5. }

  1. .header {
  2.   font-size: 18px;
  3.   @include sp {
  4.     font-size: 14px;
  5.   }
  6. }

@content は、mixinの中に「中身のCSSをはめ込む」特殊な機能です。

プロジェクト設計での使いどころ

FLOCSSでの活用例

• Foundation層に基本mixinをまとめる(例:_mixin.scss)
• **Object層(Componentなど)**で@includeして再利用
• Utility層と競合しないように、明確に役割を分ける

  1. // Foundation/_mixin.scss
  2. @mixin clearfix {
  3.   &::after {
  4.     content: "";
  5.     display: table;
  6.     clear: both;
  7.   }
  8. }
  9. // Object/Component/_media.scss
  10. .media {
  11.   @include clearfix;
  12. }

プロジェクトでよくある構成

  1. scss/
  2. ├── foundation/
  3. │ └── _mixin.scss ← mixinをまとめて定義
  4. ├── object/
  5. │ └── component/
  6. │ └── _button.scss ← includeして使う
  7. └── style.scss ← 全体の読み込み

  1. // style.scss
  2. @use "foundation/mixin";
  3. @use "object/component/button";

MILMONA   MILMONA   MILMONA   MILMONA   MILMONA   
MILMONA   MILMONA   MILMONA   MILMONA   MILMONA