How TO - クリック可能なドロップダウン

❮ 前章へ 次章へ ❯

CSS と JavaScript を使用してクリック可能なドロップダウン・メニューの作成方法を学習します。


ドロップダウン

ドロップダウンメニューは、ユーザが定済義リストから1つの値を選択できるトグル可能なメニューです:


クリック可能なドロップダウンの作成

ユーザがボタンをクリックしたときに表示されるドロップダウンメニューを作成します。

ステップ 1) HTML の追加:

<div class="dropdown">
  <button onclick="myFunction()" class="dropbtn">Dropdown</button>
  <div id="myDropdown" class="dropdown-content">
    <a href="#">Link 1</a>
    <a href="#">Link 2</a>
    <a href="#">Link 3</a>
  </div>
</div>

例の説明

ドロップダウン・メニューを開くために、任意の要素を使用します(例:<button>、<a>、<p> 要素)。

コンテナ要素( <div> など)を使用してドロップダウンメニューを作成し、その中にドロップ・ダウン・リンクを追加します。

ボタンの周りを <div> 要素でラップし、<div> を使用してドロップダウンメニューを CSS で正しく配置します。


ステップ 2) CSS の追加:

/* Dropdown Button */
.dropbtn {
    background-color: #4CAF50;
    color: white;
    padding: 16px;
    font-size: 16px;
    border: none;
    cursor: pointer;
}

/* Dropdown button on hover & focus */
.dropbtn:hover, .dropbtn:focus {
    background-color: #3e8e41;
}

/* The container <div> - needed to position the dropdown content */
.dropdown {
    position: relative;
    display: inline-block;
}

/* Dropdown Content (Hidden by Default) */
.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
}

/* Links inside the dropdown */
.dropdown-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

/* Change color of dropdown links on hover */
.dropdown-content a:hover {background-color: #f1f1f1}

/* Show the dropdown menu (use JS to add this class to the .dropdown-content container when the user clicks on the dropdown button) */
.show {display:block;}

例の説明

背景色、パディング、ホバー効果などでドロップダウンボタンのスタイルを設定しました。

.dropdown クラスには、ドロップダウン・コンテンツを(position:absoluteを使用して)ドロップダウン・ボタン のすぐ下に配置するために、position:relative を使用します。

.dropdown-content クラスは、実際のドロップダウンメニューを収容します。 デフォルトでは非表示で、ホバー時に表示されます(下記参照)。min-width は、160px に設定されています。 これは、自由に変更してください。 チップ: ドロップダウン・コンテンツの幅をドロップダウンボタンの幅と同じにするには、 width を 100% に設定します (overflow:auto を指定すると、小さな画面ではスクロールできるようになります)。

ボーダーを使用する代わりに、box-shadow プロパティを使用してドロップダウン・メニューを 「カード」のような外観にしています。


ステップ 3) JavaScript の追加:

/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
Try it Yourself »

右揃えのドロップダウン

Try it Yourself »

ナビゲーションバー内のドロップダウン・メニュー

Try it Yourself »

検索 (フィルタ) ドロップダウン

Try it Yourself »

チップ:ドロップダウンの詳細については、CSS Dropdowns チュートリアルをご覧ください。

チップ:ホバー可能なドロップダウンの詳細については、Hoverable Dropdowns をご覧ください。


❮ 前章へ 次章へ ❯