Menu
Coddy logo textTech

タブ

CoddyのHTMLジャーニー「Practical Frontend」セクションの一部 — レッスン 21/35。

タブは、すべてを1つのページに保持しながら、コンテンツを別々のセクションに整理する方法です。長いページをスクロールする代わりに、ユーザーはタブをクリックすることでコンテンツパネルを切り替えることができます。タブは、設定ページ、ダッシュボード、または製品の説明(例:詳細レビューよくある質問)などでよく見かけます。

JavaScriptを使わずに、以下のものを使用してタブを作成できます:

  • ラジオボタン(一度に1つのタブのみをアクティブにできるようにするため)
  • クリック可能なタブボタンとして機能するラベル
  • 適切なコンテンツを表示するためのCSSセレクター(:checked

HTML:

<div class="tabs">
   <input type="radio" id="tab1" name="tab" checked>
   <label for="tab1">Tab 1</label>
   <div id="content1" class="tab-content">This is the content of Tab 1.</div>
   
   <input type="radio" id="tab2" name="tab">
   <label for="tab2">Tab 2</label>
   <div id="content2" class="tab-content">This is the content of Tab 2.</div>
   
   <input type="radio" id="tab3" name="tab">
   <label for="tab3">Tab 3</label>
   <div id="content3" class="tab-content">This is the content of Tab 3.</div>
 </div>

 CSS:

<style>
  /* ラジオボタンを非表示にする */
  input { display: none; }

  /* タブラベル */
  label {
    padding: 5px 10px;
    background: lightgray;
    cursor: pointer;
  }

  /* デフォルトでコンテンツを非表示にする */
  .tab-content { display: none; padding: 10px; border: 1px solid #ccc; }

  /* ラジオボタンがチェックされたときにコンテンツを表示する */
  #tab1:checked ~ #content1,
  #tab2:checked ~ #content2 {
    display: block;
  }

  /* アクティブなタブをハイライトする */
  #tab1:checked + label,
  #tab2:checked + label {
    background: steelblue;
    color: white;
  }
</style>

学生に伝えるための簡単なリマインダーを以下に示します:

  • + (隣接兄弟セレクタ) → 直後の要素を選択します。
  • ~ (一般兄弟セレクタ) → 後続のすべての兄弟要素(直後のものだけでなく)を選択します。
challenge icon

チャレンジ

簡単

2つのタブが既に設定されているスターターHTMLが提供されています。あなたのタスクは、このタブシステムを拡張し、改善することです。

  1. **“Sleep”** 用の別のタブを追加してください:
    • ラベル: Sleep
    • コンテンツ: “Good sleep helps recovery and focus. Aim for 7 to 9 hours per night and keep a regular schedule.”
  2. チェックされたときにタブのコンテンツを表示するCSSセレクターに #tab3:checked ~ #content3 を追加してください。

チートシート

ラジオボタン、ラベル、およびCSSセレクタを使用して、JavaScriptを使わずにタブを作成します:

HTML構造:

<div class="tabs">
   <input type="radio" id="tab1" name="tab" checked>
   <label for="tab1">Tab 1</label>
   <div id="content1" class="tab-content">Content for Tab 1</div>
   
   <input type="radio" id="tab2" name="tab">
   <label for="tab2">Tab 2</label>
   <div id="content2" class="tab-content">Content for Tab 2</div>
</div>

タブ機能のCSS:

/* ラジオボタンを非表示にする */
input { display: none; }

/* タブラベルのスタイル */
label {
  padding: 5px 10px;
  background: lightgray;
  cursor: pointer;
}

/* デフォルトでコンテンツを非表示にする */
.tab-content { 
  display: none; 
  padding: 10px; 
  border: 1px solid #ccc; 
}

/* ラジオボタンがチェックされたときにコンテンツを表示する */
#tab1:checked ~ #content1,
#tab2:checked ~ #content2 {
  display: block;
}

/* アクティブなタブをハイライトする */
#tab1:checked + label,
#tab2:checked + label {
  background: steelblue;
  color: white;
}

CSS兄弟セレクタ:

  • + (隣接兄弟セレクタ) → 直後の要素を選択します
  • ~ (一般兄弟セレクタ) → 以降にあるすべての兄弟要素を選択します

自分で試してみよう

<!DOCTYPE html>
<html>
<head>
  <title>Tabs</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 20px;
      background: #f9f9f9;
    }

    .tabs {
      display: flex;
      flex-direction: column;
      width: 350px;
    }

    /* Hide the radio inputs */
    .tabs input {
      display: none;
    }

    /* Tab labels */
    .tabs label {
      padding: 10px;
      background: #eee;
      cursor: pointer;
      border: 1px solid #ccc;
    }

    .tabs label:hover {
      background: #ddd;
    }

    /* Tab content */
    .tab-content {
      display: none;
      border: 1px solid #ccc;
      padding: 10px;
      background: #fff;
      line-height: 1.5;
    }

    /* Show content when radio is checked */
    #tab1:checked ~ #content1,
    #tab2:checked ~ #content2 {
      display: block;
    }

    /* Highlight active tab */
    #tab1:checked + label,
    #tab2:checked + label,
    #tab3:checked + label {
      background: #4cafef;
      color: white;
    }
  </style>
</head>
<body>
  <div class="tabs">
    <input type="radio" id="tab1" name="tab" checked>
    <label for="tab1">Nutrition</label>
    <div id="content1" class="tab-content">
      <h3>Nutrition</h3>
      <p>A balanced diet fuels your body and mind. Focus on fruits, vegetables, whole grains, and lean proteins. Avoid too much processed food.</p>
    </div>

    <input type="radio" id="tab2" name="tab">
    <label for="tab2">Exercise</label>
    <div id="content2" class="tab-content">
      <h3>Exercise</h3>
      <p>Daily movement strengthens your body and reduces health risks. Even a 30-minute walk or stretching makes a difference.</p>
    </div>

  </div>
</body>
</html>
quiz icon腕試し

このレッスンには短いクイズがあります。レッスンを始めて解答し、進捗を記録しましょう。

Practical Frontendのすべてのレッスン