CSSの入れ子(ネスト)機能について、基本的な使い方から実践的なテクニックまで詳しく解説します。
目次
CSSネストとは?
CSSネストは、CSSルール内に別のCSSルールを記述できる機能です。これにより、HTMLの階層構造に対応したより直感的でメンテナンスしやすいCSSを書くことができます。
従来のCSS
.card { background: white; border-radius: 8px; padding: 20px; } .card h2 { color: #333; margin-bottom: 10px; } .card p { color: #666; line-height: 1.6; } .card .button { background: #007bff; color: white; padding: 10px 20px; }
ネストを使ったCSS
.card { background: white; border-radius: 8px; padding: 20px; h2 { color: #333; margin-bottom: 10px; } p { color: #666; line-height: 1.6; } .button { background: #007bff; color: white; padding: 10px 20px; } }
ブラウザサポート状況
CSSネストは比較的新しい機能で、ブラウザサポート状況は以下の通りです。
参考サイト
| ブラウザ | サポート開始バージョン |
|---|---|
| Chrome | 112+ |
| Firefox | 117+ |
| Safari | 16.5+ |
| Edge | 112+ |
注意: 古いブラウザをサポートする必要がある場合は、PostCSSやSassなどのプリプロセッサーを使用することを推奨します。
基本的な入れ子の書き方
1. 子セレクターの入れ子
.navigation { background: #f8f9fa; ul { list-style: none; padding: 0; li { display: inline-block; margin-right: 20px; a { text-decoration: none; color: #333; font-weight: 500; } } } }
2. クラスセレクターの入れ子
.article { max-width: 800px; margin: 0 auto; .header { border-bottom: 1px solid #eee; padding-bottom: 20px; .title { font-size: 2rem; color: #222; } .meta { color: #666; font-size: 0.9rem; } } .content { padding: 20px 0; line-height: 1.8; } }
3. アンパサンド(&)の使用
アンパサンド(&)は親セレクターを参照します:
.button { background: #007bff; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; /* &は.buttonを参照 */ &:hover { background: #0056b3; } &:active { transform: translateY(1px); } &.disabled { background: #6c757d; cursor: not-allowed; } /* 修飾子クラス */ &--large { padding: 15px 30px; font-size: 1.1rem; } &--small { padding: 5px 10px; font-size: 0.9rem; } }
疑似クラスと疑似要素
疑似クラスの入れ子
.link { color: #007bff; text-decoration: none; transition: all 0.3s ease; &:hover { color: #0056b3; text-decoration: underline; } &:visited { color: #6610f2; } &:active { color: #e83e8c; } &:focus { outline: 2px solid #80bdff; outline-offset: 2px; } }
疑似要素の入れ子
.quote { font-style: italic; position: relative; padding: 20px 40px; &::before { content: '"'; position: absolute; left: 0; top: 0; font-size: 3rem; color: #ddd; } &::after { content: '"'; position: absolute; right: 0; bottom: 0; font-size: 3rem; color: #ddd; } }
メディアクエリの入れ子
CSSネストではメディアクエリも入れ子にできます:
.container { max-width: 1200px; margin: 0 auto; padding: 0 20px; @media (max-width: 768px) { padding: 0 10px; } .header { display: flex; justify-content: space-between; align-items: center; @media (max-width: 768px) { flex-direction: column; gap: 20px; } .logo { font-size: 1.5rem; font-weight: bold; @media (max-width: 768px) { font-size: 1.2rem; } } } }
入れ子のベストプラクティス
1. 適切な深さを保つ
❌ 悪い例(深すぎる入れ子)
.page { .container { .row { .column { .widget { .title { .icon { /* 7層の入れ子は深すぎる */ color: red; } } } } } } }
✅ 良い例(適切な深さ)
.page { .container { max-width: 1200px; margin: 0 auto; } .widget { border: 1px solid #ddd; padding: 20px; .title { font-size: 1.2rem; margin-bottom: 10px; } } }
2. セマンティックな構造を意識
.article { /* 記事のベーススタイル */ max-width: 800px; margin: 0 auto; header { /* セマンティックなHTMLタグを活用 */ border-bottom: 1px solid #eee; padding-bottom: 20px; h1 { font-size: 2rem; color: #222; } .meta { color: #666; font-size: 0.9rem; } } main { padding: 20px 0; p { line-height: 1.8; margin-bottom: 20px; } img { max-width: 100%; height: auto; } } }
3. 条件付きスタイルの整理
.modal { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 30px; border-radius: 8px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3); /* 状態による変化 */ &.is-open { opacity: 1; visibility: visible; } &.is-closed { opacity: 0; visibility: hidden; } /* サイズバリエーション */ &--small { max-width: 400px; } &--large { max-width: 800px; } }
SassとCSS標準の違い
CSS標準のネスト
.card { padding: 20px; /* CSS標準では&が必要な場合が多い */ & h2 { color: #333; } & .button { background: #007bff; } }
Sassのネスト
.card { padding: 20px; // Sassでは&が不要な場合が多い h2 { color: #333; } .button { background: #007bff; } // Sassの高度な機能 @include breakpoint(mobile) { padding: 10px; } }
まとめ
CSSネストは現代のWeb開発において非常に有用な機能です。
主なメリットは以下の通りです。
- 可読性の向上: HTMLの構造に対応した直感的なCSS
- 保守性の向上: 関連するスタイルをグループ化
- 記述量の削減: セレクターの重複を減らせる
- スコープの明確化: 親子関係が明確になる
ただし、以下の点に注意して使用しましょう!
- 適切な深さを保つ(推奨は3〜4層まで)
- ブラウザサポート状況を確認
- パフォーマンスへの影響を考慮
- チーム内でのコーディング規約を統一
CSSネストを適切に活用することで、より効率的で保守性の高いCSSコードを書くことができます。